1821249 Members
2850 Online
109632 Solutions
New Discussion юеВ

Re: awk question

 
Matt Shaffer_1
Regular Advisor

awk question

Let me first state that I am a novice scripter. That being said, I've used awk to print fiels but is there a switch I can use to skip fields? I want to skip $1 and doing print $2... won't work for me? Is this possible. Any advise would be much appreciated.

thanks
Matt
10 REPLIES 10
Peter Godron
Honored Contributor

Re: awk question

Matt,
could you give an example, otherwise it is:
echo "a b" | awk -F' ' '{print $2}'
This will print "b", which is the second field.

Please also read:
http://forums1.itrc.hp.com/service/forums/helptips.do?#33 on how to reward any useful answers given to your questions.
Hein van den Heuvel
Honored Contributor

Re: awk question

The fields $1, $2, ... $NF are be default prepared by splitting the input line $0 based on white-space.
You can set a different seperator in the script with IFS, or on the command line with -F. Check the man page.

And... 'skipping fields' works fine for all of us, so it is somethign you are not doing correctly.

With that out of the way....

What does you input look like ?
(attach .txt file if need be, or click 'Retain format')

What should the output look like?
What are the triggers, filters, replaces?

Hope this helps some,
Hein.
Steven E. Protter
Exalted Contributor

Re: awk question

Shalom Matt,

Quite possible.

cat input | awk '{print $3 $4}'

This approach assumes the data file is consistent.

Otherwise you are best to read the data into variables and make decisions with logic after the data is read.
# read into variable called DATALINE
datavar1=$(echo $DATALINE | awk '{print $1}')

This puts the awk results in $datavar1 after wich logic and decision trees can be used.

SEP
Steven E Protter
Owner of ISN Corporation
http://isnamerica.com
http://hpuxconsulting.com
Sponsor: http://hpux.ws
Twitter: http://twitter.com/hpuxlinux
Founder http://newdatacloud.com
Matt Shaffer_1
Regular Advisor

Re: awk question

To answer the question about the input...

when I run bdf, the output of some mounts roll over to 2 lines kind of like below. This makes it hard to pull out the data that I want so I want to say remove all $1 fields that start with /. What I've been doing so far is bdf -l | grep ^/ and then I want to "remove" $1 so that all lines are equat. Does that make sense? Thanks

/dev/central03/lvol8
26% /CENTRAL-BKP/backup
A. Clay Stephenson
Acclaimed Contributor

Re: awk question

In that case, don't reinvent the wheel. Look for one of Bill Hassell's scripts called "bdfmegs". It already takes care of the spillover and produces nice one-line by combining the split lines.
If it ain't broke, I can fix that.
James R. Ferguson
Acclaimed Contributor

Re: awk question

Hi Matt:

There is no need to 'grep' with 'awk'. Awk has regular expression pattern matching built-in.

Consider:

# awk '{if ($1~/#/) {next} else {print}}' /etc/hosts

...would skip all comments in '/etc/hosts' by matching the "#" in the first field.

Regards!

...JRF...
Hein van den Heuvel
Honored Contributor

Re: awk question

>> when I run bdf, the output of some mounts roll over to 2 lines kind of like below.

Do you really think you are the first one to tackle this!?
Just google for 'awk bdf' and you'll find oodles of solutions. No point re-inventing the wheel!
http://forums1.itrc.hp.com/service/forums/questionanswer.do?threadId=1024204

>> What I've been doing so far is bdf -l | grep ^/ and then I want to "remove" $1 so that all lines are equat. Does that make sense?

Since you asked... No, this makes not sense.

Why would you go through grep when awk will happily do any filter work for you right there?

Why not add the extra segment to the partial to recreate a single full line and have all information?

Best regards,
Hein.

btw... do you now see why I suggested to checkmark the box before 'Retain format' just above the [submit]
john korterman
Honored Contributor

Re: awk question

Hi Matt,

if you want to print from $2 and the rest of the fields of each line - without worrying about the number of fields, you can try this:

$ awk '$1="";{print $0}' infile

(awk connoisseurs will probably find it ugly..)


regards,
John K.
it would be nice if you always got a second chance
Sandman!
Honored Contributor

Re: awk question

As Clay said look at Bill Hassell's script link below:

http://forums1.itrc.hp.com/service/forums/questionanswer.do?threadId=1048509

quick and dirty way to print equal number of fields from the output of bdf -l try the awk construct below:

# bdf -l | awk '{if(NF==1) getline l;gsub(" +"," ",l);print $0l}'
Matt Shaffer_1
Regular Advisor

Re: awk question

These guys gave me a lot of good feedback and things to try. None of them have worked yet but I'm getting closer.