Using AWK to get a specific line using a variable


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Using AWK to get a specific line using a variable
# 1  
Old 10-03-2012
MySQL [solved] Using AWK to get a specific line using a variable

Hi

I have a text like this
example.input
Code:
1 red
2 blue
3 green

If I set this
Code:
c=2

Then try
Code:
awk 'NR==$c { print $2 }' example.input

I do get nothing

If I try
Code:
awk 'NR==2 { print $2 }' example.input

i get
Code:
blue

I know this can e down with head/tail/sed, but I like it to work in AWK

What is wrong?

Last edited by Jotne; 10-03-2012 at 04:13 AM..
# 2  
Old 10-03-2012
Code:
awk -v c=2 'NR==c { print $2 }' example.input

Shell and awk variables are different.
# 3  
Old 10-03-2012
Code:
c=2
awk -v VM="$c" 'NR == VM{print $2}' file

# 4  
Old 10-03-2012
found solution my self using google some more Smilie

Code:
awk 'NR=='$c' { print $2 }' example.input

adding singe quote

Thanks for your quick responce
# 5  
Old 10-03-2012
That solution is worse than the ones other people posted. There's two proper ways to get a variable into awk, and that's not either of them Smilie
# 6  
Old 10-03-2012
Code:
export c=2
awk 'NR == ENVIRON["c"] { print $2 }' example.input

This User Gave Thanks to rdrtx1 For This Post:
# 7  
Old 10-03-2012
Quote:
Originally Posted by Corona688
That solution is worse than the ones other people posted. There's two proper ways to get a variable into awk, and that's not either of them Smilie
Why is it worse?
Is it more slow?
May not work with all variable?

For me it works, and its minimum of programing
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to read a two files, line by line in UNIX script and how to assign shell variable to awk ..?

Input are file and file1 file contains store.bal product.bal category.bal admin.bal file1 contains flip.store.bal ::FFFF:BADC:CD28,::FFFF:558E:11C5,6,8,2,1,::FFFF:81C8:CA8B,::FFFF:BADC:CD28,1,0,0,0,::FFFF:81C8:11C5,2,1,0,0,::FFFF:81DC:3111,1,0,1,0 store.bal.... (2 Replies)
Discussion started by: veeruasu
2 Replies

2. Shell Programming and Scripting

Print specific line using a variable

Hi Everyone, Is there a way I can print specific lines using sed -n '3,3p' file.dat or awk 'FNR==3' file.dat when using variable? For example, I have this script (get_line.ksh) that accepts line parameter that a user wanted to print in the file.dat. file.dat one two three four ... (1 Reply)
Discussion started by: zzavilz
1 Replies

3. Shell Programming and Scripting

Replace specific field on specific line sed or awk

I'm trying to update a text file via sed/awk, after a lot of searching I still can't find a code snippet that I can get to work. Brief overview: I have user input a line to a variable, I then find a specific value in this line 10th field in this case. After asking for new input and doing some... (14 Replies)
Discussion started by: crownedzero
14 Replies

4. Shell Programming and Scripting

Using awk to read a specific line and a specific field on that line.

Say the input was as follows: Brat 20 x 1000 32rf Pour 15 p 1621 05pr Dart 10 z 1111 22xx My program prompts for an input, what I want is to use the input to locate a specific field. Like if I type in, "Pou" then it would return "Pour" and just "Pour" I currently have this line but it is... (6 Replies)
Discussion started by: Bungkai
6 Replies

5. Shell Programming and Scripting

Counting rows line by line from a specific column using Awk

Dear UNIX community, I would like to to count characters from a specific row and have them displayed line-by-line. I have a file called testAwk2.csv which contain the following data: rabbit penguin goat giraffe emu ostrich I would like to count in the middle row individually... (4 Replies)
Discussion started by: vnayak
4 Replies

6. Shell Programming and Scripting

awk removing specific line

Hi, I have file with lines key=val. For ex(conf.file):- c=3 ef=78 b=40 ca=40 I want to remove the line with c=3, when I execute the below command it's removing both lines i.e c=3 and ca=40. Could you please correct my command. /usr/xpg4/bin/awk -v key=c 'match($0,key)... (3 Replies)
Discussion started by: axes
3 Replies

7. UNIX for Dummies Questions & Answers

Selecting specific line using awk

Hi, I would like to get the specific line from the file taking specific coloumn as reference. Thanks and Regards (1 Reply)
Discussion started by: kkarthik_kaja
1 Replies

8. Shell Programming and Scripting

How to read the value from a specific line and column in to a csh variable

Hi All, Although its a basic question the last 2 hours of googling and trying didnt help me to achieve what i want. Maybe some one can help me I have a text file text1.txt: blablablabla A B C D E F and i would like to read to read what is on position E (line 3 column 2) in a... (2 Replies)
Discussion started by: Radamez
2 Replies

9. Shell Programming and Scripting

Printing a specific line using AWK

Hi, I have a script that fetches only specific information from fcinfo command. Below is a portion of the script. #!/usr/bin/ksh set -x HBA_COUNT=`sudo fcinfo hba-port | grep -i state | awk 'END{print NR}'` echo "$HBA_COUNT HBAs exist" echo '........' INDEX=1 while $INDEX -le... (2 Replies)
Discussion started by: jake_won
2 Replies

10. Shell Programming and Scripting

want to specific line which i will give through variable

Hi, I have a file on unix which contains -------------------------------------- 1 # Do not remove the following line, or various programs 2 # that require network functionality will fail. 3 127.0.0.1 romhelp3 localhost.localdomain localhost 4 ... (2 Replies)
Discussion started by: srikanthus2002
2 Replies
Login or Register to Ask a Question