Review your favorite Linux distribution.
Go Back > Forums > Linux Forums > Linux - Newbie
User Name
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!


  Search this Thread
Old 10-19-2007, 11:34 AM   #1
LQ Newbie
Registered: Oct 2005
Posts: 8

Rep: Reputation: 0
Append variable string(s) at end of each line


Thanks in advance and appreciate any inputs.

Please see the examples for better understanding of my issue

#file1 Contents


#file2 COntents


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

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

Last edited by schaganti; 10-19-2007 at 11:40 AM. Reason: Clarifying with example
Old 10-19-2007, 01:18 PM   #2
spirit receiver
Registered: May 2006
Location: Frankfurt, Germany
Distribution: SUSE 10.2
Posts: 424

Rep: Reputation: 33
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:
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
Old 10-19-2007, 01:31 PM   #3
LQ Newbie
Registered: Oct 2005
Posts: 8

Original Poster
Rep: Reputation: 0
It Works

Thanks a lot. That works great.


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
bash programming-append single line to end of file pheasand Linux - General 4 02-28-2014 09:41 AM
How can I append text to a string in a file HGeneAnthony Linux - Newbie 4 03-01-2007 12:16 PM
bash: append string to end of line khairil Programming 6 02-27-2007 05:09 AM
batch append string to the end of a determined line in text files osio Programming 6 06-30-2005 09:28 AM
Wierd String appended to end of line lel800 General 8 12-28-2004 03:55 PM > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 03:47 PM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration