LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 03-28-2010, 01:37 PM   #1
elfoozo
Member
 
Registered: Feb 2004
Location: Washington, USA
Distribution: Debian
Posts: 265

Rep: Reputation: 32
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?
 
Old 03-28-2010, 01:40 PM   #2
Tinkster
Moderator
 
Registered: Apr 2002
Location: earth
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928
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
 
1 members found this post helpful.
Old 03-28-2010, 03:13 PM   #3
elfoozo
Member
 
Registered: Feb 2004
Location: Washington, USA
Distribution: Debian
Posts: 265

Original Poster
Rep: Reputation: 32
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.
 
Old 03-28-2010, 04:07 PM   #4
Tinkster
Moderator
 
Registered: Apr 2002
Location: earth
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928
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

Last edited by Tinkster; 03-28-2010 at 04:42 PM. Reason: missing comma
 
1 members found this post helpful.
Old 03-28-2010, 04:36 PM   #5
elfoozo
Member
 
Registered: Feb 2004
Location: Washington, USA
Distribution: Debian
Posts: 265

Original Poster
Rep: Reputation: 32
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!
 
Old 03-28-2010, 04:43 PM   #6
Tinkster
Moderator
 
Registered: Apr 2002
Location: earth
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928
Most welcome :}
 
1 members found this post helpful.
Old 03-28-2010, 08:02 PM   #7
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
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
 
1 members found this post helpful.
Old 03-28-2010, 08:30 PM   #8
Tinkster
Moderator
 
Registered: Apr 2002
Location: earth
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928
Good point :}
 
1 members found this post helpful.
Old 08-17-2010, 07:59 AM   #9
elfoozo
Member
 
Registered: Feb 2004
Location: Washington, USA
Distribution: Debian
Posts: 265

Original Poster
Rep: Reputation: 32
Question

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?

Last edited by elfoozo; 08-17-2010 at 08:26 AM.
 
Old 08-17-2010, 08:54 AM   #10
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192
Seems to work as expected for me ... are you positive it is contained in the first field?
 
Old 08-17-2010, 10:25 AM   #11
elfoozo
Member
 
Registered: Feb 2004
Location: Washington, USA
Distribution: Debian
Posts: 265

Original Poster
Rep: Reputation: 32
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?

Last edited by elfoozo; 08-17-2010 at 12:42 PM.
 
Old 08-17-2010, 12:10 PM   #12
elfoozo
Member
 
Registered: Feb 2004
Location: Washington, USA
Distribution: Debian
Posts: 265

Original Poster
Rep: Reputation: 32
dupe

Last edited by elfoozo; 08-17-2010 at 12:42 PM. Reason: dupe
 
Old 08-18-2010, 03:52 AM   #13
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192
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.
 
  


Reply



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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
awk printing from Nth field to last field sebelk Programming 2 01-08-2010 09:39 AM
print FS (field separator) in awk wakatana Programming 5 11-05-2009 08:17 AM
How to parameterize which field awk should print? dbland07666 Linux - Newbie 2 10-29-2007 03:49 PM
AWK: print field to end, and character count? ridertech Linux - Newbie 1 05-07-2004 05:07 PM
Two field seperators in awk?? Astro Programming 2 11-09-2003 10:12 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

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

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration