LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
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!

Notices


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

Rep: Reputation: 0
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

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

Original Poster
Rep: Reputation: 0
It Works

Thanks a lot. That works great.
 
  


Reply


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

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

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

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