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! |
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. |
This may work for you...
Code:
/The/{/third/!p} |
All times are GMT -5. The time now is 12:55 AM. |