The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
read and drop files shantanuo UNIX for Dummies Questions & Answers 1 09-26-2008 03:00 AM
Drop a Column from a File Raamc UNIX for Dummies Questions & Answers 4 01-09-2008 10:36 AM
Dynamic Drop down boxes garric Shell Programming and Scripting 13 10-18-2007 11:54 AM
Drop records with non-numerics in field X akxeman Shell Programming and Scripting 3 08-15-2007 12:55 AM
Drop Users trfrye UNIX for Dummies Questions & Answers 2 08-31-2005 03:39 PM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 01-20-2009
atc98092 atc98092 is offline
Registered User
  
 

Join Date: Jan 2009
Location: Auburn WA
Posts: 10
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.

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!!!
  #2 (permalink)  
Old 01-21-2009
cfajohnson's Avatar
cfajohnson cfajohnson is offline Forum Advisor  
Shell programmer, author
  
 

Join Date: Mar 2007
Location: Toronto, Canada
Posts: 2,361

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 (permalink)  
Old 01-21-2009
atc98092 atc98092 is offline
Registered User
  
 

Join Date: Jan 2009
Location: Auburn WA
Posts: 10
Didn't work

Quote:
Originally Posted by cfajohnson View Post
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 , 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!
  #4 (permalink)  
Old 01-21-2009
cfajohnson's Avatar
cfajohnson cfajohnson is offline Forum Advisor  
Shell programmer, author
  
 

Join Date: Mar 2007
Location: Toronto, Canada
Posts: 2,361
Quote:
Originally Posted by atc98092 View Post
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 , 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 (permalink)  
Old 01-21-2009
atc98092 atc98092 is offline
Registered User
  
 

Join Date: Jan 2009
Location: Auburn WA
Posts: 10
Quote:
Originally Posted by cfajohnson View Post
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!
  #6 (permalink)  
Old 01-21-2009
cfajohnson's Avatar
cfajohnson cfajohnson is offline Forum Advisor  
Shell programmer, author
  
 

Join Date: Mar 2007
Location: Toronto, Canada
Posts: 2,361
Quote:
Originally Posted by atc98092 View Post
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!
  #7 (permalink)  
Old 01-21-2009
rwuerth rwuerth is offline
Registered User
  
 

Join Date: Jan 2009
Location: Va. Beach
Posts: 64
Quote:
Originally Posted by cfajohnson View Post
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.
try putting '[0-2][0-9][0-5][0-9] TMU [^W][^X][^:]' (no quotes) as the 'SOMETHING' in the delete function of sed.

This worked in my tests.

Basically you eliminate the four digits followed by the 'TMU' unless it follows that up with 'WX:'

[edit]

Well scratch that, "duh" moment for me, doesn't work if you have more than one entry to search for. It will eventually pull in another '1911 TMP WX:' line as the last line to the previous entry.

Is there nothing in the actual last line (with the 'FWD:' in it) that you can key in on to make that the last line of your sed command?

Otherwise I think but have not tested, that you could pipe the output from what I've done above through 'uniq -d'

Last edited by rwuerth; 01-21-2009 at 02:01 PM.. Reason: Correction to my testing
Closed Thread

Bookmarks

Tags
awk, awk trim, trim, trim awk

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 05:22 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0