LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   sed command (https://www.linuxquestions.org/questions/linux-newbie-8/sed-command-149995/)

linuxdev 02-24-2004 12:22 PM

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

linuxdev 02-24-2004 12:39 PM

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

druuna 02-24-2004 12:44 PM

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.

linuxdev 02-24-2004 12:50 PM

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....

druuna 02-24-2004 01:16 PM

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.

linuxdev 02-24-2004 02:05 PM

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?

Tinkster 02-24-2004 02:20 PM

sed "s\\replace\`echo $THIS_LINE`" auto.test >auto1.test


Cheers,
Tink

linuxdev 02-24-2004 02:34 PM

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

Tinkster 02-24-2004 02:54 PM

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

homey 02-24-2004 04:50 PM

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



All times are GMT -5. The time now is 10:55 PM.