Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

filtering info. from a file

Status
Not open for further replies.

busterhi

Technical User
Oct 26, 2002
4
US
I’m trying to us the gawk command in Cygwin on Windows XP and I’m having a little trouble getting the exact results that I want. I was hoping I could get a little help. I’m searching for just POWERTRAIN CONTROL MODULE and I’m getting that and several other lines as well. I would like to get the row with just POWERTRAIN CONTROL MODULE in it. The information below is the command that I’m using and the results that I got.

gawk ‘/POWERTRAIN CONTROL MODULE/ {print $0}’ trans.txt>>output.txt

The following are the results that I’m getting:

4283 POWERTRAIN CONTROL MODULE
4284 POWERTRAIN CONTROL MODULE (DIESEL)
4285 POWERTRAIN CONTROL MODULE - C1
4286 POWERTRAIN CONTROL MODULE - C2
4287 POWERTRAIN CONTROL MODULE - C3
4288 POWERTRAIN CONTROL MODULE - C4
4289 POWERTRAIN CONTROL MODULE-C1
4290 POWERTRAIN CONTROL MODULE-C2
7308 POWERTRAIN CONTROL MODULE C1
8824 POWERTRAIN CONTROL MODULE RELAY CONTROL
8825 POWERTRAIN CONTROL MODULE RELAY OUTPUT

All I want is the first line (4283 POWERTRAIN CONTROL MODULE) and nothing else. If somebody could help, it would be greatly appreciated.

Thanks in advance.
happy.gif
 
How about

gawk ‘/4283 *POWERTRAIN CONTROL MODULE/ {print $0}’ trans.txt>>output.txt CaKiwi
 
gawk ' {if ($2 == "POWERTRAIN CONTROL MODULE"); {print}}'
 
I tried the following command:

$ gawk ' {if ($2 == "POWERTRAIN CONTROL MODULE"); {print}}' trans.txt>output
.txt

and

$ gawk ' {if ($2 == "POWERTRAIN CONTROL MODULE"); {print $0}}' trans.txt>output
.txt


and I'm getting the complete trans.txt file in my output.txt file and I don't know why. Your instructions look like they should work. I've change it around a little and still can't get it to give what I need. I'm I doing something wrong?

busterHi
 
This is a hack.

The problem is with the FS, but I can't figure out
a clean regexp way right now.

if (x = split($0,arr) == 4) {
if ($0 !~ /[A-Z]-.*/) {
print
}
}
}
 
I'm a little unclear about the criteria you want to use to select a line. Is it POWER, TRAIN & MODULE in the 2nd, 3rd & 4th fields and exactly 4 fields? If so, try

gawk 'NF==4 && $2=="POWER" && $3=="TRAIN" && $4=="MODULE"' trans.txt > output.txt
CaKiwi
 
gawk '/POWERTRAIN CONTROL MODULE/ && $NF == "MODULE"' trans.txt vlad
+---------------------------+
|#include<disclaimer.h> |
+---------------------------+
 
I have several columns with different language translations for the word in the second column (POWERTRAIN CONTROL MODULE). I’m looking for an exact combination of words “POWERTRAIN CONTROL MODULE” only in the second column. Then I need the complete row of information piped into another file. I have tried the these commands with the following results.

gawk '/POWERTRAIN CONTROL MODULE/ && $NF == &quot;MODULE&quot;' trans.txt>output.txt
results = no data in output.txt file.

gawk 'NF==4 && $2==&quot;POWER&quot; && $3==&quot;TRAIN&quot; && $4==&quot;MODULE&quot;' trans.txt > output.txt
results = no data in output.txt file.


if (x = split($0,arr) == 4) {
if ($0 !~ /[A-Z]-.*/) {
print
}
}
}

Results =bash: syntax error near unexpected token `('


I really appreciate everybody’s help with this but I still can’t get it to work. Any other ideas, or am I doing something wrong?

Thanks again for all your help.

BusterHi
 
Doesn't surprise me at all..

Did you try:
gawk ' {
if (x = split($0,arr) == 4) {
if ($0 !~ /[A-Z]-.*/) {
print
}
}
}' yourfilename?
 
I finally got it to work!! thank you very much for all the help.

Busterhi
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top