LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   how to add "pipes" to a sed program file (https://www.linuxquestions.org/questions/linux-newbie-8/how-to-add-pipes-to-a-sed-program-file-590149/)

new_2_unix 10-07-2007 08:35 PM

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!

jschiwal 10-07-2007 09:02 PM

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.

homey 10-07-2007 09:09 PM

This may work for you...
Code:

/The/{/third/!p}


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