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
  #15 (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'
  #16 (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!
  #17 (permalink)  
Old 02-03-2009
atc98092 atc98092 is offline
Registered User
  
 

Join Date: Jan 2009
Location: Auburn WA
Posts: 10
I may have it!

Back from Hawaii, leave for Florida tomorrow. Jacksonville is as cold as Seattle right now!

I did some testing today (on Linux), and I think I have what I need. This is what I did:

Code:

cat 15.txt | tr -s '\012' ' ' > tmp.txt

echo WEATHER: > tmp3.txt
echo
cat tmp.txt | sed 's/ \([0-2][0-9][0-5][0-9] TMU\)/\
\1/g' | sed -n '/[0-2][0-9][0-5][0-9] TMU [WX:]/P' >> tmp3.txt
echo EQUIPMENT: >> tmp3.txt
echo
cat tmp.txt | sed 's/ \([0-2][0-9][0-5][0-9] TMU\)/\
\1/g' | sed -n '/[0-2][0-9][0-5][0-9] TMU [EQ:]/P' >> tmp3.txt
echo METERING: >> tmp3.txt
echo
sed -e 's/ \([0-2][0-9][0-5][0-9] TMU\)/\
\1/g' tmp.txt | sed -n -e '/[0-2][0-9][0-5][0-9] TMU [MET][EX]/P' >> tmp3.txt
rm tmp.txt
If I am following the flow correctly, after the first cat changes the newlines to spaces and saves into a temp file, I can then scan for my keywords to retrieve the individual entries required. On the first two I scan for WX: and EQ: and it returns exactly what I want .

The 3rd time through looking for METERING: I have problems. For some reason it returns the metering lines, plus more that don't match the string. However, I add in the [EX] and then it works! That confuses me, since I have a few more keywords to search for, and I don't want to have to ask for help every time

Thanks again for the help. I'll be working on this again next Monday when I return from Florida.

To bad all this travel is work related and I can't spend some time looking around!
  #18 (permalink)  
Old 02-03-2009
quirkasaurus's Avatar
quirkasaurus quirkasaurus is offline
Registered User
  
 

Join Date: Jan 2009
Location: canton, michigan
Posts: 373
I think this'll work:

Code:
more +/WX: file_in |
sed -e 1p -e '/^.....TMU/d'
  #19 (permalink)  
Old 02-03-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
Back from Hawaii, leave for Florida tomorrow. Jacksonville is as cold as Seattle right now!

You mean it's tolerable?
Quote:
I did some testing today (on Linux), and I think I have what I need. This is what I did:

Code:

cat 15.txt | tr -s '\012' ' ' > tmp.txt

echo WEATHER: > tmp3.txt
echo
cat tmp.txt | sed 's/ \([0-2][0-9][0-5][0-9] TMU\)/\
\1/g' | sed -n '/[0-2][0-9][0-5][0-9] TMU [WX:]/P' >> tmp3.txt
echo EQUIPMENT: >> tmp3.txt
echo
cat tmp.txt | sed 's/ \([0-2][0-9][0-5][0-9] TMU\)/\
\1/g' | sed -n '/[0-2][0-9][0-5][0-9] TMU [EQ:]/P' >> tmp3.txt
echo METERING: >> tmp3.txt
echo
sed -e 's/ \([0-2][0-9][0-5][0-9] TMU\)/\
\1/g' tmp.txt | sed -n -e '/[0-2][0-9][0-5][0-9] TMU [MET][EX]/P' >> tmp3.txt
rm tmp.txt
If I am following the flow correctly, after the first cat changes the newlines to spaces and saves into a temp file,

cat doesn't change anything; it is useless in this context.

Bloated code is hard to read. First, get rid of all instances of cat.

Then get rid of all the >> tmp3.txt and redirect an entire block, e.g.:

Code:
{
  sed ....
  echo ...
  sed ...
  ...
} > tmp3.txt
That way, you can easily comment out the redirection when testing, and you will see the output in your terminal.
Quote:
I can then scan for my keywords to retrieve the individual entries required. On the first two I scan for WX: and EQ: and it returns exactly what I want .

The 3rd time through looking for METERING: I have problems. For some reason it returns the metering lines, plus more that don't match the string.

There is no search for METERING in your code.

It's very difficult to debug code that you don't show.
Quote:
However, I add in the [EX] and then it works! That confuses me, since I have a few more keywords to search for, and I don't want to have to ask for help every time
  #20 (permalink)  
Old 02-03-2009
rwuerth rwuerth is offline
Registered User
  
 

Join Date: Jan 2009
Location: Va. Beach
Posts: 64
Quote:
Originally Posted by atc98092 View Post
Back from Hawaii, leave for Florida tomorrow. Jacksonville is as cold as Seattle right now!
Yeah, but you were in Hawaii ...

Quote:
I did some testing today (on Linux), and I think I have what I need. This is what I did:

Code:
cat 15.txt | tr -s '\012' ' ' > tmp.txt
 
echo WEATHER: > tmp3.txt
echo
cat tmp.txt | sed 's/ \([0-2][0-9][0-5][0-9] TMU\)/\
\1/g' | sed -n '/[0-2][0-9][0-5][0-9] TMU [WX:]/P' >> tmp3.txt
echo EQUIPMENT: >> tmp3.txt
echo
cat tmp.txt | sed 's/ \([0-2][0-9][0-5][0-9] TMU\)/\
\1/g' | sed -n '/[0-2][0-9][0-5][0-9] TMU [EQ:]/P' >> tmp3.txt
echo METERING: >> tmp3.txt
echo
sed -e 's/ \([0-2][0-9][0-5][0-9] TMU\)/\
\1/g' tmp.txt | sed -n -e '/[0-2][0-9][0-5][0-9] TMU [MET][EX]/P' >> tmp3.txt
rm tmp.txt
If I am following the flow correctly, after the first cat changes the newlines to spaces and saves into a temp file, I can then scan for my keywords to retrieve the individual entries required. On the first two I scan for WX: and EQ: and it returns exactly what I want .

The 3rd time through looking for METERING: I have problems.
Yeah, but you were in Hawaii ... oh I said that already!

Quote:
For some reason it returns the metering lines, plus more that don't match the string.
That's because you're using the bracket expression incorrectly. You've put 'MET' in the bracket expression thinking that will give you lines with 'METERING' in there, but in reality it will give you lines that have an 'M' 'E' OR 'T' in that character position. So you'll get 'Metering' or 'METERING' but you'll also get 'Equipment' and 'EQUIPMENT' and if you have words like 'Time' or 'TUNDRA' or 'The' or you get the picture!

Quote:
However, I add in the [EX] and then it works! That confuses me, since I have a few more keywords to search for, and I don't want to have to ask for help every time
Because the second bracket expression is for the second character position in that word, so you still could conceivably get things other than just 'METERING' but if you have '[MET][EX]' your combinations for those two character positions are:

ME
MX
EE
EX
TE
TX

If the word in that position doesn't start with the above you don't get it. If it does, you do.

Quote:
Thanks again for the help. I'll be working on this again next Monday when I return from Florida.

To bad all this travel is work related and I can't spend some time looking around!
Yeah, but you were in ... oh forget it!

Last edited by rwuerth; 02-03-2009 at 06:05 PM.. Reason: (hopefully) clarified some statements.
  #21 (permalink)  
Old 02-03-2009
atc98092 atc98092 is offline
Registered User
  
 

Join Date: Jan 2009
Location: Auburn WA
Posts: 10
Quote:
Originally Posted by rwuerth View Post
Yeah, but you were in Hawaii ...

Yeah, but you were in Hawaii ... oh I said that already!

Yeah, but you were in ... oh forget it!
I needed a good chuckle today! Thanks!

And thanks for the explanation for what those nasty brackets were doing. That means the first two scans were just lucky that nothing else matched that request. I got the flick now!

Quote:
Originally Posted by cfajohnson View Post
You mean it's tolerable?
Mr Johnson:

Tolerable is in the mind of the beholder !

You're right, my code is really sloppy. I like the idea of blocking the code, didn't even occur to me . Also letting the results return to the terminal will help a lot. Where I thought I was looking for the METERING keyword [MET] I now understand was only looking for words with the first character matching any of those three letters. That explains why I got the results I did.

As soon as I get back Monday I'll clean it up and use your suggestions. I see now I'm almost there, with all the great advice here. Thanks!

Last edited by atc98092; 02-03-2009 at 06:19 PM..
Sponsored Links
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:02 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