LinuxQuestions.org
Help answer threads with 0 replies.
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 10-07-2007, 08:35 PM   #1
new_2_unix
LQ Newbie
 
Registered: Oct 2007
Posts: 26

Rep: Reputation: 15
how to add "pipes" to a sed program file


hi,

i have a file named 'myfile' with the following contents:

Line one.
The second line.
The third.

i want to use sed to find the lines that contain the word 'The'.

This should result in:
The second line.
The third.

i then want to filter the above output to print the lines that do NOT contain the word 'third'.

This should result in:
The second line.

From the command line, I can do this with the command:
> sed -n '/The/p' myfile2 | sed -n '/third/!p'

My question is:
How do I do the same thing by creating a sed script file which will contain the sed commands above and allow me to run get the same output by executing the following:

> sed -n -f mySedScript myfile2

If I simply create a file 'mySedScript' and add the following to it:
/The/p
/third/!p

it does not work and gives me the following output which is wrong:
Line one.
The second line.
The second line.
The third.

Any ideas where I may be wrong? Thanks!
 
Old 10-07-2007, 09:02 PM   #2
jschiwal
LQ Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682
egrep might be the better choice for a program. Anyway, the second sed example could be done this way:
sed -n '/third/!p' inputfile >outputfile
The test is for lines that do not contain the word third, however, since you might want to exclude both 'third' and 'Third', this might work better:
sed '/third/d;/Third/d' inputfile >outputfile

There is a logical relationship that you might want to keep in mind, However, I'm not certain how to express it in a forum:
NOT ( A AND B ) = NOT A OR NOT B
Not A would be expressed on paper with a bar over the A.
 
Old 10-07-2007, 09:09 PM   #3
homey
Senior Member
 
Registered: Oct 2003
Posts: 3,057

Rep: Reputation: 61
This may work for you...
Code:
/The/{/third/!p}
 
  


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
error message "add remove program" wesam2004 Fedora 8 02-06-2007 03:54 PM
Replacing "function(x)" with "x" using sed/awk/smth Griffon26 Linux - General 3 11-22-2006 10:47 AM
add "Artist" and "Album Title" to mp3 files powah Linux - Software 2 04-05-2005 03:04 AM
How to add Plugin in the form of "C" file ? suriyamohan Linux - Software 0 08-30-2003 01:52 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

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