pdubois
pdubois

Reputation: 7800

How to add a string to every line in a file with Bash

I have a file (myfile.txt) that contains these lines:

foo
bar
qux

In a bash script I'd like to add a value on the string resulting in

foo 1000
bar 1000
qux 1000

I tried this but failed.

#!/usr/bin/bash
MYVAL="1000"
cat myfile.txt | xargs -I '{}' echo {} & echo $MYVAL

which only prints 1000 on one line.

Upvotes: 2

Views: 1473

Answers (2)

George Vasiliou
George Vasiliou

Reputation: 6345

Your attempt is almost there:

$ xargs -I{} echo {} 1000 < file6
boo djhg 1000
bio jdjjf 1000
dgdhd bgo 1000
ghhh 1000
baao biologico bata 1000
baa beto biologic 1000
bojo bija 1000

Upvotes: 1

codeforester
codeforester

Reputation: 43089

You can use sed and you don't really need a pipe for this:

myval="1000"
sed "s/$/ $myval/" file > file.modified

for in-place editing:

sed -i "s/$/ $myval/" file
  • s/$/ $myval/ - places a space followed by the value of $myval at the end of each line

More about sed here: http://grymoire.com/Unix/Sed.html

Upvotes: 4

Related Questions