LinuxQuestions.org
LinuxAnswers - the LQ Linux tutorial section.
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 02-24-2004, 01:22 PM   #1
linuxdev
Member
 
Registered: Feb 2004
Posts: 34

Rep: Reputation: 15
sed command


I am using this command:
sed '2i\add it where ever it is supposed to be' auto.test

to insert the line at 2nd line(position) in auto.test

although I can see the correct results on screen but could not see
the results if I open auto.test using

vi auto.test
 
Old 02-24-2004, 01:39 PM   #2
linuxdev
Member
 
Registered: Feb 2004
Posts: 34

Original Poster
Rep: Reputation: 15
Re: sed command

Quote:
Originally posted by linuxdev
I am using this command:
sed '2i\add it where ever it is supposed to be' auto.test

to insert the line at 2nd line(position) in auto.test

although I can see the correct results on screen but could not see
the results if I open auto.test using

vi auto.test


Resloved it!!!!!!!!!!THANKS
 
Old 02-24-2004, 01:44 PM   #3
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374
Sed's output is to stdout, you need to redirect the output to a file (do not use the same filename!!!).

$ sed '2i\add' auto.test > auto.test.out
$ cat auto.test.out

If file is ok:
$ mv auto.test.out auto.test

Do not use the same name for the infile and outfile (sed 4.x has a parameter for this).

Hope this helps.
 
Old 02-24-2004, 01:50 PM   #4
linuxdev
Member
 
Registered: Feb 2004
Posts: 34

Original Poster
Rep: Reputation: 15
Quote:
Originally posted by druuna
Sed's output is to stdout, you need to redirect the output to a file (do not use the same filename!!!).

$ sed '2i\add' auto.test > auto.test.out
$ cat auto.test.out

If file is ok:
$ mv auto.test.out auto.test

Do not use the same name for the infile and outfile (sed 4.x has a parameter for this).

Hope this helps.
-..........................>>>>>>>>>>>>>>>

Thank you for your reply.....now this is a inserting operation if we hard code the
string in sed statement but what if I would like to change string dynamically
depending on users request...

I hope I am clear here....otherwise I will just repeat myself..

All I am trying is ...how can I insert a string(that can be different each time
user types in) dynamically into a file....I realized that using sed command that
string has to be hard coded...so I am looking to insert dynamically....
 
Old 02-24-2004, 02:16 PM   #5
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374
You need to use the appropriate quotes and escape the $:

$ THIS_LINE="Whatever you want"
$ echo $THIS_LINE
Whatever you want
$ sed "2i\\$THIS_LINE" auto.test
line 01
Whatever you want
line 02
line 03
line 04
line 05

Hope this is what you wanted to know.
 
Old 02-24-2004, 03:05 PM   #6
linuxdev
Member
 
Registered: Feb 2004
Posts: 34

Original Poster
Rep: Reputation: 15
Quote:
Originally posted by druuna
You need to use the appropriate quotes and escape the $:

$ THIS_LINE="Whatever you want"
$ echo $THIS_LINE
Whatever you want
$ sed "2i\\$THIS_LINE" auto.test
line 01
Whatever you want
line 02
line 03
line 04
line 05

Hope this is what you wanted to know.


Thank you very much.. It worked but..
I tired following for dynamically substituting text:
sed "s\\replace\$THIS_LINE" auto.test >auto1.test

but it is not working?
How can I attain dymanic substitution?
 
Old 02-24-2004, 03:20 PM   #7
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 23,005
Blog Entries: 11

Rep: Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903
sed "s\\replace\`echo $THIS_LINE`" auto.test >auto1.test


Cheers,
Tink
 
Old 02-24-2004, 03:34 PM   #8
linuxdev
Member
 
Registered: Feb 2004
Posts: 34

Original Poster
Rep: Reputation: 15
Quote:
Originally posted by Tinkster
sed "s\\replace\`echo $THIS_LINE`" auto.test >auto1.test


Cheers,
Tink

----------->>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
Thank you for your response..

I tired the command:

sed "s\\addation\`echo $command`" auto3.test >auto4.test

and got this error message:
./auto2.test: line 5: unexpected EOF while looking for matching ``'
./auto2.test: line 7: syntax error: unexpected end of file
 
Old 02-24-2004, 03:54 PM   #9
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 23,005
Blog Entries: 11

Rep: Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903
Sorry, I just copied your faulty line and put my
stuff into it .... thry this one
Code:
sed "s/slack/`echo $THIS_LINE`/" auto3.test >auto4.test

Cheers,
Tink
 
Old 02-24-2004, 05:50 PM   #10
homey
Senior Member
 
Registered: Oct 2003
Posts: 3,057

Rep: Reputation: 56
Here is something which may work for you...
The extra quotes are needed around the $info so sed will process a line with spaces in it.
The $num should not have that problem as it is an actual line number.

Code:
#!/bin/bash

echo
echo ""
echo "Enter the line number: "
read num
echo ""
echo "Enter the information: "
read info
cat somefile.txt | sed -e ''$num'i\'"$info"'' > somefile.txt

Last edited by homey; 02-24-2004 at 05:51 PM.
 
  


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
sed command Kalyani1 Linux - Software 28 12-07-2005 12:48 PM
sed command rharris72 Programming 6 11-20-2005 09:26 PM
sed command pazvant Linux - Software 2 05-09-2004 01:58 PM
sed Command linuxdev Linux - Newbie 3 02-09-2004 12:27 PM
sed command kwigibo Linux - General 3 04-21-2002 05:11 PM


All times are GMT -5. The time now is 03:48 AM.

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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration