Help with awk. | Unix Linux Forums | Shell Programming and Scripting

  Go Back    


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

Help with awk.

Shell Programming and Scripting


Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 02-22-2013
CiCa CiCa is offline
Registered User
 
Join Date: Jun 2012
Last Activity: 7 June 2013, 8:42 AM EDT
Location: UK
Posts: 27
Thanks: 10
Thanked 0 Times in 0 Posts
Help with awk.

Hi,

Is it possible to assign awk results into variables inside the '{}'

e.g.


Code:
 ... ... ... | awk -F"/" '{print $1 = day,$2 = month 2,$3 = year}'

Basically up to the awk point the input is the date in dd/mm/yy format and rather than run the command three times and assign day, month year in three separate commands, I was hoping to do it all in one - something similar to the above. Is this possible?

Thanks in advance.

Rgds
Ciaran
Sponsored Links
    #2  
Old 02-22-2013
balajesuri's Avatar
balajesuri balajesuri is offline Forum Advisor  
#! /bin/bash
 
Join Date: Apr 2009
Last Activity: 21 July 2014, 11:56 PM EDT
Location: India
Posts: 1,816
Thanks: 24
Thanked 519 Times in 498 Posts
Quote:
Originally Posted by CiCa View Post
Basically up to the awk point the input is the date in dd/mm/yy format and rather than run the command three times and assign day, month year in three separate commands, I was hoping to do it all in one - something similar to the above. Is this possible?
I really don't understand the highlighted statement. Did you mean, assign day, month and year in three "separate variables"? If so, why would you want to assign them to different variables? You can use them as $1, $2, $3...

And variable assignment is always "variable = value", not the other way round.
$1 = day is same as doing 5 = x in algebra.
Sponsored Links
    #3  
Old 02-22-2013
CiCa CiCa is offline
Registered User
 
Join Date: Jun 2012
Last Activity: 7 June 2013, 8:42 AM EDT
Location: UK
Posts: 27
Thanks: 10
Thanked 0 Times in 0 Posts
OK - to try and explain a little better...

I have to read and check dates on multiple line of output and I need to read the day, month and year into three separate variables, so I can test/compare against them, so...

Rather than doing:


Code:
day = `pntadm -P 111.222.333.444 | awk '{print $5}' | egrep -v "Forever|Zero" | awk -F"/" '{print $1}'`
month = `pntadm -P 111.222.333.444 | awk '{print $5}' | egrep -v "Forever|Zero" | awk -F"/" '{print $2}'`
year = `pntadm -P 111.222.333.444 | awk '{print $5}' | egrep -v "Forever|Zero" | awk -F"/" '{print $3}'`

I had hoped to assign the variables in one line, something similar to:

Code:
pntadm -P 111.222.333.444 | awk '{print $5}' | egrep -v "Forever|Zero" | awk -F"/" '{print day=$1, month=$2, year=$3}'

I know the syntax in the above statement is probably wrong, so please excuse this and this is the part I'm struggling with and hope to get assistance.

Hopefully this explains things a little better?

Thanks in advance!
    #4  
Old 02-22-2013
RudiC RudiC is offline Forum Advisor  
Registered User
 
Join Date: Jul 2012
Last Activity: 25 July 2014, 12:44 PM EDT
Location: Aachen, Germany
Posts: 3,886
Thanks: 62
Thanked 922 Times in 875 Posts
If your shell (bash, ksh do) supports this, you can use a combination of process substitution and redirection:
Code:
$ read X Y Z < <( awk 'BEGIN {print "A B C"}' )
$ echo $X $Y $Z
A B C

See the TWO < < ?
The Following User Says Thank You to RudiC For This Useful Post:
CiCa (02-22-2013)
Sponsored Links
    #5  
Old 02-22-2013
balajesuri's Avatar
balajesuri balajesuri is offline Forum Advisor  
#! /bin/bash
 
Join Date: Apr 2009
Last Activity: 21 July 2014, 11:56 PM EDT
Location: India
Posts: 1,816
Thanks: 24
Thanked 519 Times in 498 Posts
Or you could put them in an array:

Code:
x=( $(pntadm -P 111.222.333.444 | awk '{print $5}' | egrep -v "Forever|Zero" | awk -F"/" '{print $1,$2,$3}') )
echo ${x[0]} ## Day
echo ${x[1]} ## Month
echo ${x[2]} ## Year

Sponsored Links
    #6  
Old 02-22-2013
CiCa CiCa is offline
Registered User
 
Join Date: Jun 2012
Last Activity: 7 June 2013, 8:42 AM EDT
Location: UK
Posts: 27
Thanks: 10
Thanked 0 Times in 0 Posts
Thanks RudiC,

Seen your last post and I'm trying variations of:


Code:
read day month year < `pntadm -P 111.222.333.444 | awk '{print $5}' | egrep -v "Forever|Zero" | awk -F"/" '{print $1,$2,$3}'`;echo $day $month $year;

But it's just not happening...
Sponsored Links
    #7  
Old 02-22-2013
RudiC RudiC is offline Forum Advisor  
Registered User
 
Join Date: Jul 2012
Last Activity: 25 July 2014, 12:44 PM EDT
Location: Aachen, Germany
Posts: 3,886
Thanks: 62
Thanked 922 Times in 875 Posts
See the TWO < < ? Read the shell's man page on process substitution, and please use exactly the syntax provided : < < ( ... ) . NO backtics etc.

BTW - your lenghty pipe could be way more efficient (untested):
Code:
pntadm -P 111.222.333.444 | awk -F"/"  '!/Forever|Zero/ {print $....}'

The Following User Says Thank You to RudiC For This Useful Post:
CiCa (02-22-2013)
Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Passing awk variable argument to a script which is being called inside awk vivek d r Shell Programming and Scripting 2 02-11-2013 06:37 AM
HELP with AWK one-liner. Need to employ an If condition inside AWK to check for array variable ? shell_boy23 Shell Programming and Scripting 6 07-10-2012 05:20 AM
awk command to compare a file with set of files in a directory using 'awk' anandek Shell Programming and Scripting 10 05-30-2012 09:21 AM
scripting/awk help : awk sum output is not comming in regular format. Pls advise. rveri Shell Programming and Scripting 3 11-06-2009 07:47 PM
Awk problem: How to express the single quote(') by using awk print function patrick87 Shell Programming and Scripting 4 10-06-2009 07:36 AM



All times are GMT -4. The time now is 03:54 PM.