LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (http://www.linuxquestions.org/questions/programming-9/)
-   -   awk: how to print a field when field position is unknown? (http://www.linuxquestions.org/questions/programming-9/awk-how-to-print-a-field-when-field-position-is-unknown-798471/)

elfoozo 03-28-2010 02:37 PM

awk: how to print a field when field position is unknown?
 
Hello,

I'm trying to display fields from flat files where the first 8 fields are always the same. Fields 9 - n are varied but will contain specific patterns I'm after. I'm using this so far because "mySearch" is on each line I want to examine.
Code:

awk '/mySearch/ { print $1 " " $2 " " $3 " " $8 }' myFile.txt
How would you pattern match and include 2 additional fields above field $9 but change field position from line to line?

Tinkster 03-28-2010 02:40 PM

Code:

awk '/mySearch/ { print $1 " " $2 " " $3 " " $8;for(i=9;i<=NF;i++){if($i ~ /match/ || $i ~ /match2/ ){print $i}} }' myFile.txt
I'll leave the formatting with printf to you as a puzzle ;}


Cheers,
Tink

elfoozo 03-28-2010 04:13 PM

And what a puzzle it is! I've been poking at this non-stop since you posted and am failing to get all of the output on one line. Thanks though, I at least have the output I was looking for.

Tinkster 03-28-2010 05:07 PM

Ok, I'll help you, then ;}

Code:

awk '/mySearch/ { printf "%s %s %s %s", $1,$2,$3,$8;for(i=9;i<=NF;i++){if($i ~ /match/ || $i ~ /match2/ ){printf " %s", $i}};printf "\n" }' myFile.txt

The trick is not to print a newline till all the fields are
printed, and you do achieve that quite easily with printf.


Cheers,
Tink

elfoozo 03-28-2010 05:36 PM

doh! I was at least on the right track with %s. I would've been spinning my wheels a long time though because I didn't realize it needed multiples. Also, there was a comma missing from the 2nd printf of your example... now works perfect for me here. And I learned a lot - Thank you!

Tinkster 03-28-2010 05:43 PM

Most welcome :}

ghostdog74 03-28-2010 09:02 PM

a little refinement
Code:

awk '/mySearch/{ printf "%s %s %s %s", $1,$2,$3,$8;for(i=9;i<=NF;i++){if($i ~ /match|match2/){printf " %s", $i}};printf "\n" }' myFile.txt

Tinkster 03-28-2010 09:30 PM

Good point :}

elfoozo 08-17-2010 08:59 AM

I've discovered that /mySearch/ is a variant of alpha case per each line. E.g. Thomas or THOMAS.

I've tried the "tolower" function but I don't seem to understand how to make tolower operate on the very first regex in the command. I've searched many examples but they end up being just the basic tolower($1) ~ /foo/ examples.

Can tolower work with that command or is there a better mechanism to bypass case of the pattern I'm trying to match?

grail 08-17-2010 09:54 AM

Seems to work as expected for me ... are you positive it is contained in the first field?

elfoozo 08-17-2010 11:25 AM

Nope, I'm not positive of anything, I'm learning.

I think I got it:

Code:

#!/bin/sh
uname=username
dom=mydomain.tld
awk 'tolower($0) ~ /to=<'$uname@$dom'> / { printf "%s %s %s %s %s", $1,$2,$3,$7,$10;for(i=8;i<=NF;i++){if($i ~ /from=</ || $i ~ /to=</ ){printf " %s", $i}};printf "\n" }' /var/log/mail.log >> /var/www/myOutput.txt

It seems to be returning what I want. Would what I have be considered inefficient?

elfoozo 08-17-2010 01:10 PM

dupe

grail 08-18-2010 04:52 AM

Quote:

It seems to be returning what I want. Would what I have be considered inefficient?
Well you probably could have done it all as awk script and not worried about layering it inside a shell script.


All times are GMT -5. The time now is 07:23 AM.