|
|||||||
| Forums | Search Forums | Register | Forum Rules | Man Pages | Albums | FAQ | Members | Calendar | Search | Today's Posts | Mark Forums Read |
| Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here. |
|
|
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
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
|
||||
|
||||
|
Quote:
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
|
|||
|
|||
|
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
|
|||
|
|||
|
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 CSee the TWO < < ? |
| The Following User Says Thank You to RudiC For This Useful Post: | ||
CiCa (02-22-2013) | ||
| Sponsored Links | |
|
|
#5
|
||||
|
||||
|
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
|
|||
|
|||
|
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
|
|||
|
|||
|
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 | ||
|
![]() |
| Thread Tools | Search this Thread |
| 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 |
|
|