using sed but want to drop last line


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting using sed but want to drop last line
# 1  
Old 01-20-2009
Question using sed but want to drop last line

Howdy all. I have some scripts that read a text file looking for a keyword, then returning all the text until another keyword and puts it into a new file. Problem is, sed returns the entire last line that contains the 2nd keyword, and I don't want it! Here's an example of the sed script line:

sed -n "/WX: /,/[0-2][0-9][0-5][0-9] TMU/P " 15.txt > test1.txt

and here's what it returns:

1911 TMU WX: 1900 1/15-2300 1/15 PAZA SIGMET INDIA 9 FOR SEVERE HD
TURBULENCE BELOW 100 WI AN AREA 20NM NW BGQ TO 40NM E BGQ TO
50NM E ENA TO 10NM W ENA TO 20NM NW BGQ. FWD: DCC,A11
2000 TMU Rick Bartow (IR) On duty position TMU

The blue line is what I don't want. It is a separate entry that does not apply to the previous entry. I can't use grep, because it only gives me the first line of the entry, and each entry can vary in length, so I can't use a line count. These scripts are on Red Hat Enterprise 3 workstations, and I am not allowed to install anything that isn't already on them, so whatever scripting language is already there is all I can use.Smilie

The next entry will always start with the date (4 digit), 4 spaces, then the word TMU. There is no other constant for the next entry. Is there any switch to sed or perhaps another command that will get the text I want but stop on the 2nd keyword, or some other way to strip the last line?

I run the same script line multiple times with different beginning keywords so I can group them together into a single report.

I should also mention that each file I am searching could have multiple entries with the same keyword. sed pulls them all in perfectly, except for the extra line from each entry. Any ideas???

Thanks!!! Smilie
# 2  
Old 01-21-2009

Code:
sed -n -e '/SOMETHING/d' -e "/WX: /,/[0-2][0-9][0-5][0-9] TMU/P " 15.txt > test1.txt

...where SOMETHING is a regexp that will match the line you don't want.
# 3  
Old 01-21-2009
Didn't work

Quote:
Originally Posted by cfajohnson
Code:
sed -n -e '/SOMETHING/d' -e "/WX: /,/[0-2][0-9][0-5][0-9] TMU/P " 15.txt > test1.txt

...where SOMETHING is a regexp that will match the line you don't want.
thanks for the quick response. I entered this:

Code:
sed -n -e "/[0-2][0-9][0-5][0-9]    TMU/d" -e "/WX: /,/[0-2][0-9][0-5][0-9]    TMU/p " 15.txt > test1.txt

since I want to remove the line that starts with the date and TMU stamp. However, it is returning nothing. By the way, the double quotes are because I'm testing this on a Windows PC Smilie, and the single quotes don't work here. Once I get something that works, I'll go upstairs and test it on the Linux box. If you think that might cause me a problem, I'll test it upstairs now.

Thanks again!Smilie
# 4  
Old 01-21-2009
Quote:
Originally Posted by atc98092
thanks for the quick response. I entered this:

Code:
sed -n -e "/[0-2][0-9][0-5][0-9]    TMU/d" -e "/WX: /,/[0-2][0-9][0-5][0-9]    TMU/p " 15.txt > test1.txt

since I want to remove the line that starts with the date and TMU stamp. However, it is returning nothing.

You must use a pattern that doesn't match the line that you DO want.
Quote:
By the way, the double quotes are because I'm testing this on a Windows PC Smilie, and the single quotes don't work here. Once I get something that works, I'll go upstairs and test it on the Linux box. If you think that might cause me a problem, I'll test it upstairs now.

It probably doesn't make a difference, but I wouldn't trust anything on a Windows PC.
# 5  
Old 01-21-2009
Quote:
Originally Posted by cfajohnson
You must use a pattern that doesn't match the line that you DO want.
It probably doesn't make a difference, but I wouldn't trust anything on a Windows PC.
Well, I tried that on the Linux box, and it stripped out the time entry on the line I did want, so gonna have to try something else. Also, funny thing. The script that worked perfectly on my Windows box (the original I posted) doesn't work on the Linux box. It gives me the entire log with some lines duplicated. The only change I made was changing the double quote to single and specifying the full path to the files. Weird!Smilie
# 6  
Old 01-21-2009
Quote:
Originally Posted by atc98092
Well, I tried that on the Linux box,

What did you try?
Quote:
and it stripped out the time entry on the line I did want, so gonna have to try something else.

As I said, the first command must not match any lines that you do want.
Quote:
Also, funny thing. The script that worked perfectly on my Windows box (the original I posted) doesn't work on the Linux box.

Did you make sure there are no carriage returns in the script?
Quote:
It gives me the entire log with some lines duplicated. The only change I made was changing the double quote to single and specifying the full path to the files. Weird!Smilie
# 7  
Old 01-21-2009
Quote:
Originally Posted by cfajohnson
What did you try?
As I said, the first command must not match any lines that you do want.
Did you make sure there are no carriage returns in the script?
I tried the same script that I was testing on Windows on the Linux box, with the double quote changes.

I saw that about not what you want after I hit enter. Sorry, my bad.Smilie

Carrage returns, that's a good idea to check. I've had that problem with scripts edited on Windows then moved to Linux. Durn, I should have remembered that!

Leaving for a doctor appt, so I'll try again tomorrow. Thanks for the help.Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Replace values in script reading line by line using sed

Hi all, Let's say I have a script calling for the two variables PA_VALUE and PB_VALUE. for pa in PA_VALUE blah blah do for pb in PB_VALUE blah blah do I have a text file with two columns of values for PA and PB. 14.5 16.7 7.8 9.5 5.6 3.6 etc etc I would like to read this... (7 Replies)
Discussion started by: crimsonengineer
7 Replies

2. Shell Programming and Scripting

sed command to replace a line in a file using line number from the output of a pipe.

Sed command to replace a line in a file using line number from the output of a pipe. Is it possible to replace a whole line piped from someother command into a file at paritcular line... here is some basic execution flow.. the line number is 412 lineNo=412 Now i have a line... (1 Reply)
Discussion started by: vivek d r
1 Replies

3. Shell Programming and Scripting

Multiple line search, replace second line, using awk or sed

All, I appreciate any help you can offer here as this is well beyond my grasp of awk/sed... I have an input file similar to: &LOG &LOG Part: "@DB/TC10000021855/--F" &LOG &LOG &LOG Part: "@DB/TC10000021852/--F" &LOG Cloning_Action: RETAIN &LOG Part: "@DB/TCCP000010713/--A" &LOG &LOG... (5 Replies)
Discussion started by: KarmaPoliceT2
5 Replies

4. Shell Programming and Scripting

sed and awk giving error ./sample.sh: line 13: sed: command not found

Hi, I am running a script sample.sh in bash environment .In the script i am using sed and awk commands which when executed individually from terminal they are getting executed normally but when i give these sed and awk commands in the script it is giving the below errors :- ./sample.sh: line... (12 Replies)
Discussion started by: satishmallidi
12 Replies

5. Shell Programming and Scripting

sed command to grep multiple pattern present in single line and delete that line

here is what i want to achieve.. i have a file with below contents cat fileName blah blah blah . .DROP this REJECT that . --sport 7800 -j REJECT --reject-with icmp-port-unreachable --dport 7800 -j REJECT --reject-with icmp-port-unreachable . . . more blah blah blah --dport 3306... (14 Replies)
Discussion started by: vivek d r
14 Replies

6. Shell Programming and Scripting

sed command to replace a line at a specific line number with some other line

my requirement is, consider a file output cat output blah sdjfhjkd jsdfhjksdh sdfs 23423 sdfsdf sdf"sdfsdf"sdfsdf"""""dsf hellow there this doesnt look good et cetc etc etcetera i want to replace a line of line number 4 ("this doesnt look good") with some other line ... (3 Replies)
Discussion started by: vivek d r
3 Replies

7. Shell Programming and Scripting

I need to know how to replace a line after a pattern match with an empty line using SED

Hi How Are you? I am doing fine! I need to go now? I will see you tomorrow! Basically I need to replace the entire line containing "doing" with a blank line: I need to the following output: Hi How Are you? I need to go now? I will see you tomorrow! Thanks in advance.... (1 Reply)
Discussion started by: sags007_99
1 Replies

8. Shell Programming and Scripting

Sed Comparing Parenthesized Values In Previous Line To Current Line

I am trying to delete lines in archived Apache httpd logs Each line has the pattern: <ip-address> - - <date-time> <document-request-URL> <http-response> <size-of-req'd-doc> <referring-document-URL> This pattern is shown in the example of 6 lines from the log in the code box below. These 6... (1 Reply)
Discussion started by: Proteomist
1 Replies

9. Shell Programming and Scripting

print option - drop 1 line

Hi, Is there a print option to drop the data 1 line? Basically the page is too close to the top of the page and I would like to drop it one line automatically, editing the actual data is complicated. (8 Replies)
Discussion started by: mcclunyboy
8 Replies

10. Shell Programming and Scripting

Sed or Grep to delete line containing patter plus extra line

I'm new to using sed and grep commands, but have found them extremely useful. However I am having a hard time figuring this one out: Delete every line containing the word CEN and the next line as well. ie. test.txt blue 324 CEN green red blue 324 CEN green red blue to produce:... (2 Replies)
Discussion started by: rocketman88
2 Replies
Login or Register to Ask a Question