LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Append variable string(s) at end of each line (https://www.linuxquestions.org/questions/linux-newbie-8/append-variable-string-s-at-end-of-each-line-593046/)

schaganti 10-19-2007 11:34 AM

Append variable string(s) at end of each line
 
Hi

Thanks in advance and appreciate any inputs.

Please see the examples for better understanding of my issue

#file1 Contents

1
2
3


#file2 COntents

1,E1,F1,G1,E2,F2,G2
2,H1,I1,J1,H2,I2,J2
3,K1,L1,M1,K2,L2,M2

create file3

cat file1.txt | grep "1" | sed 's/$/,E1,F1,G1,E2,F2,G2/'
output is
1,E1,F1,G1,E2,F2,G2 (correct)

But if I try to do it using
holder="1"
string=",E1,F1,G1,E2,F2,G2"

cat file1.txt | grep "$holder" | sed 's/$/$string/'
OUtput is:
1,$string ( NOT Correct)

Whereas I was hoping to get 1,E1,F1,G1,E2,F2,G2


Sorry for long post. Thanks for your time

spirit receiver 10-19-2007 01:18 PM

Variables don't get expanded in single quotes. You can use double quotes instead, but then you should escape the '$' that should not be modified:
Code:

ada@barnabas:~> string="lala"
ada@barnabas:~> echo '\$string $string'
\$string $string
ada@barnabas:~> echo "\$string $string"
$string lala
ada@barnabas:~> echo -e "line 1\nline 2\nline 3" | sed -e "/^line 2/{s/$/ $string/}"
line 1
line 2 lala
line 3
ada@barnabas:~>


schaganti 10-19-2007 01:31 PM

It Works
 
Thanks a lot. That works great.


All times are GMT -5. The time now is 08:13 PM.