LinuxQuestions.org
Support LQ: Use code LQ3 and save $3 on Domain Registration
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 01-22-2007, 05:00 PM   #1
Mangenius
Member
 
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, 05:04 PM   #2
frob23
Senior Member
 
Registered: Jan 2004
Location: Roughly 29.467N / 81.206W
Distribution: OpenBSD, Ubuntu, FreeBSD
Posts: 1,449

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, 05:35 PM   #3
Mangenius
Member
 
Registered: Jan 2007
Posts: 30

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

sed 's/Interesting ports on/ /'; '/Port/,/https/d'; '1,3d'; 172.25.24.16_111_8th_VPN_netlist_nmap_scan.txt > 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 05:42 PM.
 
Old 01-22-2007, 06:11 PM   #4
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 23,066
Blog Entries: 11

Rep: Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910
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).


Cheers,
Tink
 
  


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


All times are GMT -5. The time now is 02:30 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration