LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 10-21-2009, 04:20 AM   #1
onesikgypo
Member
 
Registered: Jun 2008
Posts: 56

Rep: Reputation: 15
Issue with Sed When Line Number And Text Are Both Variables


Hi,

im trying to insert some info at a particular line, where the line and the text are both held in variables

In trying to do the following:

Code:
sed ""$ln"i\"$addline"" $file1 > $file2
i get:

unexpected EOF while looking for matching `"'

if i try:

Code:
sed "$ln"i\"$addline" $file1 > $file2
sed just errors on every word in "$addline" saying file not found


Any help would be greatly appreciated

Thanks.
 
Old 10-21-2009, 04:32 AM   #2
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
Hi,

This seems to work for me:

sed ""$ln"i"$addline"" infile

Hope this helps.
 
Old 10-21-2009, 04:38 AM   #3
onesikgypo
Member
 
Registered: Jun 2008
Posts: 56

Original Poster
Rep: Reputation: 15
That erroed out on me, see for example the following script:

Code:
#!/bin/bash
ln=5
addline="this is a test"
file1=file1.txt
file2=file2.txt

sed ""$ln"i"$addline"" $file >> $file
produces the error:

Code:
sed: can't read is: No such file or directory
sed: can't read a: No such file or directory
 
Old 10-21-2009, 05:05 AM   #4
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
Hi,

I didn't realize it would be part of a script:

Code:
#!/bin/bash
ln=5
addline="this is a test"
file1=file1.txt
file2=file2.txt

sed "
$ln i\
$addline
" $file1 >> $file2
This is tested and works.

Hope it helps.
 
Old 10-21-2009, 06:10 AM   #5
onesikgypo
Member
 
Registered: Jun 2008
Posts: 56

Original Poster
Rep: Reputation: 15
Hi, thanks for the reply

you're right with the example i gave it did work, however in mya ctual, more complex script, my variable has several leading spaces that must be in the final output file, with the current script the leading spaces are not there (however the tailing spaces are), so perhaps a more appropriat ebeginning to the script i should've given is:

Code:
#!/bin/bash
ln=5
addline="           this is a test                  "
file1=file1.txt
file2=file2.txt
 
Old 10-21-2009, 06:30 AM   #6
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
Hi again,

Next time be a bit more elaborate when describing your problem.......

Replace:
Code:
addline="           this is a test                  "
with:
Code:
addline="\          this is a test                  "
 
  


Reply



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
Sed append text to end of line if line contains specific text? How can this be done? helptonewbie Linux - Newbie 4 10-23-2013 01:48 PM
bash : read every line from text file starting at given line number quadmore Programming 4 02-20-2009 12:29 PM
Specifying line number with sed teiben Linux - Newbie 8 06-27-2008 05:54 AM
SED - display text on specific line of text file 3saul Linux - Software 3 12-29-2005 04:32 PM
Insert character into a line with sed? & variables in sed? jago25_98 Programming 5 03-11-2004 06:12 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 11:15 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