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


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How can I pass variable to "awk" and use print ?
# 1  
Old 08-11-2011
Data How can I pass variable to "awk" and use print ?

Problem comes with following command:
Code:
[saturn@localhost Desktop]$ a=2
[saturn@localhost Desktop]$ awk  'NR==2 {print $2}' about.txt 
rights
[saturn@localhost Desktop]$ awk  "NR==2 {print $2}" about.txt 
All rights reserved. Use is subject to license terms.           ????why print all fields????
[saturn@localhost Desktop]$ awk  "NR==$a {print $2}" about.txt 
All rights reserved. Use is subject to license terms.           ????why print all fields????
[saturn@localhost Desktop]$ awk  'NR==$a {print $2}' about.txt 
                                                               !!!print nothing!!!

Why does it print all fields? How to resolve it.
Thanks!!
# 2  
Old 08-11-2011
Quote:
Code:
awk  "NR==2 {print $2}" about.txt

The awk script is specified between ", so the shell makes the substition for $2 before awk execution.
# 3  
Old 08-11-2011
Thanks!
However I want to use $a, not "2", I want to use variable set by others in the awk.
# 4  
Old 08-11-2011
Code:
awk -v a=$a 'NR==$a {print $2}' about.txt
awk 'NR==$a {print $2}' a=$a about.txt

# 5  
Old 08-11-2011
Thanks, your command seems get nothing:
Code:
[saturn@localhost Desktop]$ echo $a
2
[saturn@localhost Desktop]$ awk 'NR==$a {print $2}' a=$a about.txt
[saturn@localhost Desktop]$ awk -v a=$a 'NR==$a {print $2}' about.txt
[saturn@localhost Desktop]$

What's wrong?
# 6  
Old 08-11-2011
Oups !
Code:
awk -v a=$a 'NR==a {print $2}' about.txt
awk 'NR==a {print $2}' a=$a about.txt

Jean-Pierre.
# 7  
Old 08-11-2011
Thanks all !!
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

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 i=1 while ; do `awk -F"\t" '{print $($i)}' $content > Sample_${i}_original`;... (4 Replies)
Discussion started by: forums123456
4 Replies

4. Shell Programming and Scripting

how to use "cut" or "awk" or "sed" to remove a string

logs: "/home/abc/public_html/index.php" "/home/abc/public_html/index.php" "/home/xyz/public_html/index.php" "/home/xyz/public_html/index.php" "/home/xyz/public_html/index.php" how to use "cut" or "awk" or "sed" to get the following result: abc abc xyz xyz xyz (8 Replies)
Discussion started by: timmywong
8 Replies

5. 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

6. Solaris

How to check "faulty" or "stalled" print queues - SAP systems?

Hi all, First off, sorry for a long post but I think I have no other option if I need to explain properly what I need help for. I need some advise on how best to check for "faulty" or "stalled/jammed' print queues. At the moment, I have three (3) application servers which also acts as print... (0 Replies)
Discussion started by: newbie_01
0 Replies

7. 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

8. 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

9. 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

10. UNIX for Advanced & Expert Users

pf not working properly even with only "pass in all" and "pass out all" rules

i have two rules in my pf.conf file, "pass in all" and "pass out all" i was having issues with getting pf working to begin with, so i went with starting from nothing and working on up. i have an ultrasparc ultra1 200e, with an added 4-port fast ethernet sbus card, running "3.4 GENERIC#85... (4 Replies)
Discussion started by: xyyz
4 Replies
Login or Register to Ask a Question