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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Parsing a file - there's got to be a better way! 4

Status
Not open for further replies.

630111

MIS
Oct 30, 2001
127
US
I have a file called ~failures.txt that contains both Production and Non Production backup failures, one line per server. I'd like to parse this file and put the Production failures in one file, and the Non-Production failures in another file.
The best way I can come up with is to cat the file one time for each search against the second field in the line.

# cat $WORKINGDIR/~failures.txt | awk '$2 ~ /^PROD/' > $WORKINGDIR/~prodfailures.txt
# cat $WORKINGDIR/~failures.txt | awk '$2 ~ /^NON_PROD/' > $WORKINGDIR/~nonprodfailures.txt
# cat $WORKINGDIR/~failures.txt | awk '$2 ~ /^THIRD_POL/' >> $WORKINGDIR/~prodfailures.txt
# cat $WORKINGDIR/~failures.txt | awk '$2 ~ /^FOURTH_POL/' >> $WORKINGDIR/~prodfailures.txt
# cat $WORKINGDIR/~failures.txt | awk '$2 ~ /^ARMSSW/' >> $WORKINGDIR/~prodfailures.txt

Is there a more efficient/elegant way to do this?

Thanks, in advance!
630111
 
think you might be looking for an or condition in your regex

[tt]# cat $WORKINGDIR/~failures.txt | awk '$2 ~ /^PROD|NON_PROD|THIRD_POL|FOURTH_POL|ARMSSW/' > $WORKINGDIR/~prodfailures.txt[/tt]

this is almost certainly not the correct syntax but should be along the lines of what you require

in Perl it would be (PROD|NON_PROD|THIRD_POL...) for example - the pipes creating the options


Kind Regards
Duncan
 
Code:
awk '$2 !~ /^NON_PROD/ {print >> nonprod.txt; next} {print > prod.txt}'

vlad
+----------------------------+
| #include<disclaimer.h> |
+----------------------------+
 
sorry 'bout that:

Code:
awk '$2 !~ /^NON_PROD/ {print >> nonprod.txt; next} {print >> prod.txt}' $WORKINGDIR/~failures.txt

vlad
+----------------------------+
| #include<disclaimer.h> |
+----------------------------+
 
And what about this ?
cd $WORKINGDIR
awk '
$2~/^NON_PROD/{print >"~nonprodfailures.txt";next}
{print >"~prodfailures.txt"}
' ~failures.txt

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
Yet another way...

awk '
{
if ( $2 ~ /^NON_PROD/ )
print > "'$WORKINGDIR/~nonprodfailures.txt'"
else
if ( $2 ~ /^(PROD|THIRD_POL|FOURTH_POL|ARMSSW)/ )
print > "'$WORKINGDIR/~prodfailures.txt'"
}
' $WORKINGDIR/~failures.txt
 
You have all been a great help - not only did I get the answer to my question, but I learned a bunch about AWK as well!

Thanks so much!
630111
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top