variable inside awk '{print $c}'


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting variable inside awk '{print $c}'
# 1  
Old 04-13-2007
variable inside awk '{print $c}'

i'm trying to do this (in bash darwin);

echo "give me some words: "
read a
c=2 # this is get by other ways
echo $a | awk '{print $c}' # i want to print the column given
# by de $c variable

if there is someone understand what i pretend and see what im doing wrong, please help me.thanks
# 2  
Old 04-13-2007
Code:
echo $a | awk -v c=$c '{print $c}'

or
Code:
echo $a | awk '{print $'$c'}'

# 3  
Old 04-13-2007
Code:
awk 'BEGIN { print "Give me some words (Ctrl+C to exit):" }
{ print $c; next }' c=2

# 4  
Old 04-13-2007
thank you man.it works perfectly.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

To print value for a $variable inside a $variable or file

Hi guys, I have a file "abc.dat" in below format: FILE_PATH||||$F_PATH TABLE_LIST||||a|b|c SYST_NM||||${SRC_SYST} Now I am trying to read the above file and want to print the value for above dollar variables F_PATH and SRC_SYST. The problem is it's reading the dollar variables as... (5 Replies)
Discussion started by: abcabc1103
5 Replies

2. Shell Programming and Scripting

Passing awk variable argument to a script which is being called inside awk

consider the script below sh /opt/hqe/hqapi1-client-5.0.0/bin/hqapi.sh alert list --host=localhost --port=7443 --user=hqadmin --password=hqadmin --secure=true >/tmp/alerts.xml awk -F'' '{for(i=1;i<=NF;i++){ if($i=="Alert id") { if(id!="") if(dt!=""){ cmd="sh someScript.sh... (2 Replies)
Discussion started by: vivek d r
2 Replies

3. Shell Programming and Scripting

HELP with AWK one-liner. Need to employ an If condition inside AWK to check for array variable ?

Hello experts, I'm stuck with this script for three days now. Here's what i need. I need to split a large delimited (,) file into 2 files based on the value present in the last field. Samp: Something.csv bca,adc,asdf,123,12C bca,adc,asdf,123,13C def,adc,asdf,123,12A I need this split... (6 Replies)
Discussion started by: shell_boy23
6 Replies

4. Shell Programming and Scripting

Using variable inside awk

Hi, Please help me how to use variables inside awk in code below: ll | awk -v "yr=`date '+%Y'`" -v "mnth=`date '+%m'`" -v Jan=1 -v Feb=2 -v Mar=3 -v Apr=4 -v May=5 -v Jun=6 -v Jul=7 -v Aug=8 ' !/^d/ { if(NR>1) {printf "%-29s\t\t%s\t%5s\t\t%s %s,", $9,$1,$5,$`$6`,$7} }' Thanks. (10 Replies)
Discussion started by: manubatham20
10 Replies

5. Shell Programming and Scripting

Sysdate inside awk print statement

Hi, I am using awk statement to extract data from a file and write a new file with certain columns rearranged and few hard coded values added to new file. Now i need to add a column with sysdate. can i do that inside the awk print statement? Now: nawk ' /^3/ BEGIN {FS=","}... (2 Replies)
Discussion started by: selvankj
2 Replies

6. Shell Programming and Scripting

Can't print multiple lines inside awk :(

Hi Friends, I have small issue with following code snippet. I am trying call one function inside awk in which the function inturn will echo few lines. However when i ran script its throwing an error saying "nawk: syntax error at source line 1". #!/bin/sh eval input=$@ while read... (3 Replies)
Discussion started by: Shahul
3 Replies

7. Shell Programming and Scripting

How to print array values whose name is inside a variable

I have a array as CArray=( a1 a2 ) and a1,a2,a3 are also array as: a1=(1 2 3) a2=(3 4 5) now I have this in my code: for i in `echo "${CArray}"` do echo ${$i} done It is giving error as :"bad substitution" It should give me value as 1 2 3 3 4 5 how can I get this...Can u please... (2 Replies)
Discussion started by: joshilalit2004
2 Replies

8. Shell Programming and Scripting

Using variable inside awk

I am trying to print the lines with pattern and my pattern is set to a variable express awk '/$express/{where=NR;print}' test2.log I am not getting any data even though i have the data with the pattern. Can seomeone correct me with the awk command above? (20 Replies)
Discussion started by: rdhanek
20 Replies

9. Shell Programming and Scripting

print argv inside awk

mode=$1 psg telnetd | awk current=`date +%M`'{ printf ("mode is %s",mode) printf ("mode is %s",ARGV) }' at command prompt when i run the script along with the argument i get only-- 'mode is ' argument is not printed.(If the argument is... (3 Replies)
Discussion started by: Anteus
3 Replies

10. Shell Programming and Scripting

getting variable inside awk

Hi All, I have awk script for replacing the nth ocurance of a string in an xml file... My code is like this FILETYPE=xml TAGNAME=type OCCURANCE=$1 TAGVALUE=valueur echo OCCURANCE:$OCCURANCE echo TAGNAME:$TAGNAME echo TAGVALUE:$TAGVALUE awk -v n=$OCCURANCE -v... (1 Reply)
Discussion started by: subin_bala
1 Replies
Login or Register to Ask a Question