Variable interpolation in "print" of awk command


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Variable interpolation in "print" of awk command
# 1  
Old 02-22-2012
Variable interpolation in "print" of awk command

Hi,
I have a snippet like below.
Based on variable i, i wish to print 1,2,3,4,5th columns to Sample files.
For each loop, one column from contetn and results will be pused to sample files. But i have a problem here
Code:
i=1
while [ $i -le 5 ]; do
`awk -F"\t" '{print $($i)}' $content > Sample_${i}_original`;
`awk -F"\t" '{print $($i)}' $results > Sample_${i}_results`;
i=`expr $i+1|bc`;
done

Variable i is not being interpolated inside awk command
Code:
'{print $($i)}'

I tried many variations of it like
Code:
"{print $($i)}"
'{print $i}'

but nothing works.
How can i modify the code to make variable i is interpolated under print of awk command?
Appreciate your responses.

Last edited by Franklin52; 02-23-2012 at 04:17 AM.. Reason: Please use code tags for code and indent your code, thank you
# 2  
Old 02-22-2012
Code:
awk -F"\t" '{print \$$i}'

# 3  
Old 02-22-2012
Didn't worked.
Output from set -x is below. No Interpolation is happening.
I am using korn shell.
Code:
awk -F\t {print $$i}


Last edited by Franklin52; 02-23-2012 at 04:18 AM.. Reason: Please use code tags for code and data samples, thank you
# 4  
Old 02-22-2012
Right. I missed that you used single quotes. Try this:
Code:
awk -F"\t" "{print \$$i}"

# 5  
Old 02-22-2012
\$$i turning out to take Procees ID. You can see the set -x below
Code:
awk -F\t {print 14306i}

I tried with below variation and it is working now.
Code:
awk -F"\t" "{print $"$i"}"

Thanks for your inputs.

Last edited by Franklin52; 02-23-2012 at 04:18 AM.. Reason: Please use code tags for code and data samples, thank you
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Bash script - Print an ascii file using specific font "Latin Modern Mono 12" "regular" "9"

Hello. System : opensuse leap 42.3 I have a bash script that build a text file. I would like the last command doing : print_cmd -o page-left=43 -o page-right=22 -o page-top=28 -o page-bottom=43 -o font=LatinModernMono12:regular:9 some_file.txt where : print_cmd ::= some printing... (1 Reply)
Discussion started by: jcdole
1 Replies

2. Shell Programming and Scripting

Why awk print is strange when I set FS = " " instead of FS = "\t"?

Look at the following data file(cou.data) which has four fields separated by tab. Four fields are country name, land area, population, continent where it belongs. As for country name or continent name which has two words, two words are separated by space. (Data are not accurately... (1 Reply)
Discussion started by: chihuyu
1 Replies

3. Shell Programming and Scripting

awk variable into shell command "date -d": possible...?

Hello, there! I am trying to pass an awk variable into a shell command in order to collect the result into an awk variable; in Bash it does work, as in: v='2'; date -d "now + $v weeks" But in awk it does not, as in: v="2" "date -d 'now + v weeks'" | getline newdate close ("date -d 'now... (3 Replies)
Discussion started by: fbird3
3 Replies

4. Shell Programming and Scripting

awk "date" and "system" command

Hello experts! I need your help please I have a file.txt of which I want to extract 3rd and 4th columns with date with the form e.g.: 2016-11-25 03:14:50and pass them to "date" command, but also append the 9th column in a file as well. So I want to execute date -d '2016-11-25 03:14:50' ... (2 Replies)
Discussion started by: phaethon
2 Replies

5. UNIX for Dummies Questions & Answers

Using "mailx" command to read "to" and "cc" email addreses from input file

How to use "mailx" command to do e-mail reading the input file containing email address, where column 1 has name and column 2 containing “To” e-mail address and column 3 contains “cc” e-mail address to include with same email. Sample input file, email.txt Below is an sample code where... (2 Replies)
Discussion started by: asjaiswal
2 Replies

6. Shell Programming and Scripting

How can I pass variable to "awk" and use print ?

Problem comes with following command: $ a=2 $ awk 'NR==2 {print $2}' about.txt rights $ awk "NR==2 {print $2}" about.txt All rights reserved. Use is subject to license terms. ????why print all fields???? $ awk "NR==$a {print $2}" about.txt All rights reserved. Use is... (6 Replies)
Discussion started by: 915086731
6 Replies

7. Shell Programming and Scripting

awk command to replace ";" with "|" and ""|" at diferent places in line of file

Hi, I have line in input file as below: 3G_CENTRAL;INDONESIA_(M)_TELKOMSEL;SPECIAL_WORLD_GRP_7_FA_2_TELKOMSEL My expected output for line in the file must be : "1-Radon1-cMOC_deg"|"LDIndex"|"3G_CENTRAL|INDONESIA_(M)_TELKOMSEL"|LAST|"SPECIAL_WORLD_GRP_7_FA_2_TELKOMSEL" Can someone... (7 Replies)
Discussion started by: shis100
7 Replies

8. Shell Programming and Scripting

cat $como_file | awk /^~/'{print $1","$2","$3","$4}' | sed -e 's/~//g'

hi All, cat file_name | awk /^~/'{print $1","$2","$3","$4}' | sed -e 's/~//g' Can this be done by using sed or awk alone (4 Replies)
Discussion started by: harshakusam
4 Replies

9. Shell Programming and Scripting

ls -laR | grep "^-" | awk '{print $9}'| grep "$.txt"

Hi, I don't know hot to make this command work: ls -laR | grep "^-" | awk '{print $9}'| grep "$.txt" It should return the list of file .txt It's important to search .txt at the end of the line, becouse some file name have "txt" in their name but have other extensions (13 Replies)
Discussion started by: DNAx86
13 Replies

10. UNIX for Dummies Questions & Answers

Explain the line "mn_code=`env|grep "..mn"|awk -F"=" '{print $2}'`"

Hi Friends, Can any of you explain me about the below line of code? mn_code=`env|grep "..mn"|awk -F"=" '{print $2}'` Im not able to understand, what exactly it is doing :confused: Any help would be useful for me. Lokesha (4 Replies)
Discussion started by: Lokesha
4 Replies
Login or Register to Ask a Question