1834882 Members
2271 Online
110071 Solutions
New Discussion

Re: awk

 
SOLVED
Go to solution
Gary Price
Occasional Contributor

awk

I am trying to use awk to print fields of a log file {print $5 $6}. The area where I need assistance is printing the remaining record/entry. The log file is dynamic and I need to display fields 5,6 and everything after six.

Thanks,
Gary
6 REPLIES 6
Scott D. Allen
Regular Advisor

Re: awk

use NF to give yourself the number of fields in each record and loop it or something like that.
"Sometimes the devil you know is better than the devil you don't know."
Kofi ARTHIABAH
Honored Contributor

Re: awk

I forget the awk construct for this but this should do the same:

# cut -d delimiter -f 5,6- filename

where delimiter is the field delimiter and filename is the file you want to process.

Good luck
nothing wrong with me that a few lines of code cannot fix!
James R. Ferguson
Acclaimed Contributor
Solution

Re: awk

Gary:

Use:

awk '{for (i=5; i <= NF; i++1) print $i}'

where i is initialized to the first field you want!

...JRF...
Anthony deRito
Respected Contributor

Re: awk

Gary, I am guessing that you want to set some variables here.

VAR1=[field 5],
VAR2=[field 6],
VAR3=[everything_after_six]

You can use field specifiers to process each record like this:

cut -d "delimiter" -f 5,6

Use this to set VAR1 and VAR2

VAR3 can be set also by using cut as well but use the "-" range specifier like this:

cat -d "delimiter" 7-

This will cut positions 7 through the end of the record and thus can use it to set VAR3.


Tony
Anthony deRito
Respected Contributor

Re: awk

Gary, sorry for the typo, the last command should be

cut -d "delimiter" 7-

not

cat -d "delimiter" 7-

Tony

Kofi ARTHIABAH
Honored Contributor

Re: awk

Gary:

Just out of curiosity, could you run "time" against the two versions - I am curious to find out which does the job quicker - cut or awk.

Thanks

nothing wrong with me that a few lines of code cannot fix!