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 Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 01-22-2009
atc98092 atc98092 is offline
Registered User
  
 

Join Date: Jan 2009
Location: Auburn WA
Posts: 10
Quote:
Originally Posted by rwuerth View Post

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'
Yeah, I was thinking of the FWD, but looking at complete log files I see that not every entry is forwarded to someone else. Unfortunately, the only constant between entries is that the next entry starts with the 4 digit time and position (which for this facility is always TMU). The only other constant is the keyword that starts the entry, such as WX: or METERING:. Even to colon isn't constant after the keyword.

I've asked the development team that works on the program to add keyword search to the reports we can generate from the command line, but even if they do it, it'll take over a year to get it in place. The government moves extremely slow when tinkering with stuff they consider "operational" to the National Airspace System, even when it doesn't have any effect on controlling aircraft!

Thanks again for the suggestions. I'll keep tinkering, but scripting isn't my strong point. I can program in Visual Basic, but that won't work on Linux
  #2 (permalink)  
Old 01-22-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
I'll keep tinkering, but scripting isn't my strong point. I can program in Visual Basic, but that won't work on Linux

If you can do it in VB, you can do it in Linux. It will probably best be done with awk, rather than as a sed one-liner.

What algorithm would you use in VB?
  #3 (permalink)  
Old 01-22-2009
atc98092 atc98092 is offline
Registered User
  
 

Join Date: Jan 2009
Location: Auburn WA
Posts: 10
Quote:
Originally Posted by cfajohnson View Post
If you can do it in VB, you can do it in Linux. It will probably best be done with awk, rather than as a sed one-liner.

What algorithm would you use in VB?
Hadn't given it too much thought. Since each line in the entry is a fixed length (including spaces, it looks like it's 80) maybe I could save it into a variable, then trim so many characters from the end. With sed I don't know how to trim the end of each individual return, and then copy the variable into the new file and continue the sed. Wish I understood this better!
  #4 (permalink)  
Old 01-22-2009
rwuerth rwuerth is offline
Registered User
  
 

Join Date: Jan 2009
Location: Va. Beach
Posts: 64
If you can play with the input file a little, I can do this in ksh.


Code:
#!/bin/ksh

FILE1=$1         
FILE2=tmp.txt

cat $FILE1 | tr -s '\012' ' ' > $FILE2

print >> $FILE2

cat $FILE2 | sed 's/ \([0-2][0-9][0-5][0-9] TMU\)/\
\1/g' | sed -n '/[0-2][0-9][0-5][0-9] TMU [MW][EX]/P'

rm $FILE2
usage:

thisscript.sh 15.txt

Notes: I'm commenting here to keep the clutter in the code down.

Script takes your input file as parameter '1' and asigns that to FILE1.
FILE2 is a temp file we'll use for output.

cat FILE1 to 'tr' to change all newlines to spaces and store the result in FILE2

'print' then appends a newline to the end of FILE2. This is necessary or 'sed' will ignore the input of FILE2 as it must see a newline.

cat FILE2 into two distinct sed process. The first inserts a new line before the specified pattern of ' #### TMU' (I use # instead of the actual numerical pattern for brevity here). Note there is a space before the first number.

In the first sed process note that there is a REQUIRED newline after the '/\' so that the rest of the command resumes on the next line with "\1/g' "

Now you have records that are separated by a newline w/o any newlines in the records themselves as there was before.

So the second sed process can now identify each record you want printed with only 1 address, and print the full record, w/o printing the record(s) you don't want.

The [WM][EX] will, unfortunately find 'MX' and 'WE' as well as 'WX' and 'ME', so you may have to play with this depending upon your actual data.

Last edited by vgersh99; 01-22-2009 at 01:55 PM.. Reason: fixed code tags
  #5 (permalink)  
Old 01-22-2009
vgersh99's Avatar
vgersh99 vgersh99 is online now Forum Staff  
Moderator
  
 

Join Date: Feb 2005
Location: Boston, MA
Posts: 5,119
why exactly do you need to 'cat' into 'tr' and 'sed'?
  #6 (permalink)  
Old 01-22-2009
rwuerth rwuerth is offline
Registered User
  
 

Join Date: Jan 2009
Location: Va. Beach
Posts: 64
Quote:
Originally Posted by vgersh99 View Post
why exactly do you need to 'cat' into 'tr' and 'sed'?
Thanks for challenging that. As you already know (since I've seen you issue this challenge before ;-) ), I don't have to cat into either command, when I can use redirect for 'tr' and sed will work on a specified file. Saves a couple of processes.

Also, thanks for fixing my code tags before. I was trying to do that, and then saw it was already done.

So the 'tr' line can be changed to:

Code:
 
tr -s '\012' ' ' < $FILE1 > $FILE2
and the sed line can be changed to:

Code:
 
sed -e 's/ \([0-2][0-9][0-5][0-9] TMU\)/\
\1/g' $FILE2 | sed -n -e '/[0-2][0-9][0-5][0-9] TMU [MW][EX]/P'
  #7 (permalink)  
Old 01-22-2009
atc98092 atc98092 is offline
Registered User
  
 

Join Date: Jan 2009
Location: Auburn WA
Posts: 10
I don't want anyone to think I'm ignoring posts, but I got pulled into an all-day meeting and it may last through tomorrow. Next week I'm traveling (Hawaii, yeah!) so won't be able to test anything on a Linux box. I will still try things on my Windows laptop, but as I've already found, what works here may not work there.

I really appreciate all the help, and I'm sure we'll come up with a solution. I'm excited to try using awk instead of sed, and I have it on my laptop as well. Please keep the suggestions coming!
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 09:34 AM.


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