Latest LQ Deal: Linux Power User Bundle
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 01-22-2007, 04:00 PM   #1
Registered: Jan 2007
Posts: 30

Rep: Reputation: 15
need help with sed

I'm trying to edit a file and save its contents into a new one. Compounding it further, I need to string multiple commands in order to successfully edit the file to my choosing. Why? Well, here's what I'm doing:

sed 's/(place text here)/(replaced text) /' input_file > output_file

sed '/Start/,/End/d' input_file > output_file

sed '1,3d' input_file > output_file

So, the above commands are all being executed and saved into a file called output_file. The first command executes and saves fine (for the record, the order the commands are executed doesn't matter). Now if I run the second command, the contents of output_file are blank! All I want to do is make more than one change to an input file and save said changes into the same new file. There is something called awk, right? Would I have to use that in conjunction with sed? Can someone please help me?

Thank you.
Old 01-22-2007, 04:04 PM   #2
Senior Member
Registered: Jan 2004
Location: Roughly 29.467N / 81.206W
Distribution: OpenBSD, Debian, FreeBSD
Posts: 1,450

Rep: Reputation: 48
sed '/Start/,/End/d;1,3d;s/(place text here)/(replaced text) /' input_file > output_file

You can string the commands together with a ; between them.
Old 01-22-2007, 04:35 PM   #3
Registered: Jan 2007
Posts: 30

Original Poster
Rep: Reputation: 15
oh, like this:

sed 's/Interesting ports on/ /'; '/Port/,/https/d'; '1,3d'; > 24.16_temp.txt

I tried the above and it just hangs, like it's expecting me to type more text. Am I doing something wrong or does it depend on what you're running? I'm running gentoo with 2.6 kernel.

I managed to string multiple commands using the -e command. I typed in another command like this (after executing the above sed command with the -e option):

sed -e '1d' 24.16_temp.txt > 24.16_temp.txt

Unfortunately, it erases the entire contents of 24.16_temp.txt. I'm reading in from the file I just changed and want to rewrite back to it. Is the above the correct procedure for doing so? Am I supposed to use pipe instead of the greater-than symbol, like this:

sed -e '1d' 24.16_temp.txt | 24.16_temp.txt ???

Thanks you very much.

Last edited by Mangenius; 01-22-2007 at 04:42 PM.
Old 01-22-2007, 05:11 PM   #4
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 911Reputation: 911Reputation: 911Reputation: 911Reputation: 911Reputation: 911Reputation: 911Reputation: 911
You'll need to use sed -i (in place) if that's what you require ... personally
(until you're 100% certain that it does what you want, anyway) I'd go with a
second, temporary file (or if it's not too big just look at the result on screen).



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 script with grep and sed: sed getting filenames from grep odysseus.lost Programming 1 07-17-2006 11:36 AM
[sed] "Advanced" sed question(s) G00fy Programming 2 03-20-2006 12:34 AM
sed and escaping & in something like: echo $y | sed 's/&/_/g' prx Programming 7 02-03-2005 11:00 PM
Sed Help twantrd Programming 4 11-02-2004 12:36 AM
Insert character into a line with sed? & variables in sed? jago25_98 Programming 5 03-11-2004 06:12 AM > Forums > Linux Forums > Linux - Newbie

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

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