variables in awk statement


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers variables in awk statement
# 1  
Old 01-10-2012
variables in awk statement

Hi,
i can print 2nd, 4th and 6th columns in a file using the shell command:

awk -F "|" '{print $2 $4 $6}' file1.txt

Now, the position and number of columns can be variable and from a variable POSLIST
So let's say
POSLIST='$2 $4 $6'

when i use the command:

a)
awk -F "|" '{print $POSLIST}' file1.txt

it throws the error:
awk: Field $() is not correct.

b)
awk -v VAR=$POSLIST -F "|" '{print $POSLIST}' file1.txt
or
awk -v VAR="'"$POSLIST"'" -F "|" '{print $POSLIST}' file1.txt

error:
awk: Cannot find or open file $2.

c)
awk -F "|" '{print `echo $POSLIST`}' file1.txt

error:
The error context is
{print >>> ` <<<
awk: The statement cannot be correctly parsed.


None of the above syntax worked.
How to pass variable containing column positions to the print statement?

Thanks,
-srinivas yelamanchili
# 2  
Old 01-10-2012
Try in this way ..
Code:
$ cat infile
1|2|3|4|5|6|7
$
$ a='$2 $3 $4'
$
$ echo $a
$2 $3 $4
$
$ echo "awk -F\| '{print $a}' infile" | sh
234
$

This User Gave Thanks to jayan_jay For This Post:
# 3  
Old 01-10-2012
Hi ysrini,

Other way:
Code:
$ awk -F\| -v vara="$a" '{ gsub( /\$/, "", vara ); split( vara, arr, /\s+/ ); for ( i in arr ) { print $arr[i] } }' infile

Regards,
Birei
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Convert Update statement into Insert statement in UNIX using awk, sed....

Hi folks, I have a scenario to convert the update statements into insert statements using shell script (awk, sed...) or in database using regex. I have a bunch of update statements with all columns in a file which I need to convert into insert statements. UPDATE TABLE_A SET COL1=1 WHERE... (0 Replies)
Discussion started by: dev123
0 Replies

2. Shell Programming and Scripting

Difficulty with incrementing Variables and using the results in a If/else statement

Environment: BASH version: GNU bash, version 3.2.51(1)-release (sparc-sun-solaris2.10) Copyright (C) 2007 Free Software Foundation, Inc. OS: Oracle Solaris 10 9/10 s10s_u9wos_14a SPARC Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved. ... (4 Replies)
Discussion started by: os2mac
4 Replies

3. Shell Programming and Scripting

comparing variables in an if statement

#!/bin/bash #timetest TIMENOW="$(date)" T1=12:00:00 echo $TIMENOW >timenow cat timenow |cut -f4 -d' ' >time1 T2=$(sed -n "${1}p" time1) echo "T1 = " $T1 echo "T2 = " $T2 if then echo $T1 else echo $T2 fi I thought scripting was simple! So why does this script result in: T1 =... (4 Replies)
Discussion started by: habuchas
4 Replies

4. UNIX for Dummies Questions & Answers

Initializing multiple variables in one statement

HI, I have 5 variables var1, var2, var3, var4 and var5 I need to initialize all of them to zero. Is there a way to do it in a single line something like this var1=var2=var3=var4=var5=0. I am unable to achieve this. What is going wrong? (2 Replies)
Discussion started by: paku
2 Replies

5. Shell Programming and Scripting

using variables in case statement

is it possible to call a variable in a case statement, for example lsmonth=Jan|Feb l |while read ans do mymonth=`echo $ans |awk '{print $6}'` case $mymonth in $lsmonth) echo do something ;; *) echo do something else ;; esac done I want to use $lsmonth... (8 Replies)
Discussion started by: gefa
8 Replies

6. Shell Programming and Scripting

Using two shell variables in single AWK statement

meas is a shell variable, and this works perfectly fine for me: awk -v var=$meas -F, '$1==var' /abcd/efgh.txt > temp1.csv However, i want to introduce another shell variable, named, defnfile in the statement, in place of hardcoded path for efgh.txt like: awk -v var=$meas -F, '$1==var'... (3 Replies)
Discussion started by: indianjassi
3 Replies

7. Shell Programming and Scripting

I can't seem to pass variables properly into a nawk statement

Ok, So up front I'm going to say that I'm a very elementary scripter, and I tend to use tools I don't fully understand, but I shotgun at something until I can get it to work...that said, I can't for the life of me understand why I can't get this to go down the way I want it to. The goal: -to... (6 Replies)
Discussion started by: DeCoTwc
6 Replies

8. Shell Programming and Scripting

Variables within a sed statement

I am just wondering if it's possible to refer to variables within a sed statement as follows:- cat $file | sed -e 1's/$oldtext/$newtext/' > $file as when I run the script, the variables are not recognised and nothing happens..?? Thanks (5 Replies)
Discussion started by: sirtrancealot
5 Replies

9. Shell Programming and Scripting

echoing two variables in one statement

I have the following -------------------- foreach var (STO SNY WKF) set ta = 5 end --------- How can I echo both variables at the same time. Something to the effect of echo ${$var}ta But this doesn't work. Seems like it would. Thanks. (4 Replies)
Discussion started by: wxornot
4 Replies

10. UNIX for Dummies Questions & Answers

How can I put wildcards in an if statement that uses variables?

With the if statement: if How can I make it so it accepts a wildcard after the ${CURR_DAY_MONTH} variable? Putting a -f /webtrends/SUN/mrw2/access.${CURR_DAY_DAY}${CURR_DAY_MONTH}* won't work, right? I think I need some kind of special character so it knows the wildcard is... (3 Replies)
Discussion started by: LordJezo
3 Replies
Login or Register to Ask a Question