# Help required on AWK command

**URL:** <https://community.unix.com/t/help-required-on-awk-command/182478>\
**Category:** UNIX for Dummies Questions & Answers\
**Created:** [January 21, 2008, 3:10am UTC](https://community.unix.com/t/help-required-on-awk-command/182478 "2008-01-21T03:10:04Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![udiptya](https://community.unix.com/letter_avatar/udiptya/32/6_449c533f586939b6e5f246632a46421f.png) [@udiptya](https://community.unix.com/u/udiptya)\
**Post date:** [January 21, 2008, 3:10am UTC](https://community.unix.com/t/help-required-on-awk-command/182478/1 "2008-01-21T03:10:04Z")

</div>

I have two questions:

(Q1) I am using a loop and want to print a particular field of a table using the AWK command by accessing one row at a time by validating NR with the loop counter. I am using a command like this but it isn't working!

count=1  
NAWK -F"|" -v var=$count 'NR == var { printf $5 }' file1

where, count is my loop counter variable;  
file1 is the table to search.

I will appreciate if someone gives me a solution

(Q2) I am taking a name as input and storing it in a variable empname  
Now, the situation is same as in question 1 and I have used a command:  
NAWK -F"|" -v var=$count 'NR = var { printf $5 }' file1  
in this case although I am using the variable count=1 for printing field 5 of the first row the answer is given according to the variable empname as if I have used a command something like  
NAWK -F"|" -v var="${empname}" '$1 ~ var { printf $5 }' file1

I will appreciate if someone gives me an explanation of this

---

<div class="post-metadata">

**Author:** ![ThobiasVakayil](https://community.unix.com/letter_avatar/thobiasvakayil/32/6_449c533f586939b6e5f246632a46421f.png) [@ThobiasVakayil](https://community.unix.com/u/ThobiasVakayil)\
**Post date:** [January 21, 2008, 4:00am UTC](https://community.unix.com/t/help-required-on-awk-command/182478/2 "2008-01-21T04:00:18Z")

</div>

count=1  
awk -F"|" '$1 ~/'$count'/ {print $5}' file1

---

<div class="post-metadata">

**Author:** ![udiptya](https://community.unix.com/letter_avatar/udiptya/32/6_449c533f586939b6e5f246632a46421f.png) [@udiptya](https://community.unix.com/u/udiptya)\
**Post date:** [January 21, 2008, 11:57pm UTC](https://community.unix.com/t/help-required-on-awk-command/182478/3 "2008-01-21T23:57:55Z")

</div>

Hi,

Thank you for your initiative but the suggestion isn't working. Can you suggest something else.

---

<div class="post-metadata">

**Author:** ![ThobiasVakayil](https://community.unix.com/letter_avatar/thobiasvakayil/32/6_449c533f586939b6e5f246632a46421f.png) [@ThobiasVakayil](https://community.unix.com/u/ThobiasVakayil)\
**Post date:** [January 22, 2008, 12:04am UTC](https://community.unix.com/t/help-required-on-awk-command/182478/4 "2008-01-22T00:04:53Z")

</div>

please check the following :  
cat file1  
1|aaa|bbb|ccc  
2|xxx|yyy|zzz  
3|mmm|nnn|ooo

$ count=2  
$ awk -F"|" '$1 ~ /'$count'/ {print $2}' file1

output :  
xxx

---

<div class="post-metadata">

**Author:** ![udiptya](https://community.unix.com/letter_avatar/udiptya/32/6_449c533f586939b6e5f246632a46421f.png) [@udiptya](https://community.unix.com/u/udiptya)\
**Post date:** [January 22, 2008, 2:33am UTC](https://community.unix.com/t/help-required-on-awk-command/182478/5 "2008-01-22T02:33:20Z")

</div>

Sorry, I should have given a bit of detail. My file is like this  
CAT file1  
abc |123 |abc123  
bcd |234 |bcd234  
cde |345 |cde345

In such a situation the command will not work. I want to extract one field from this file by scrolling through its individual lines which I want to do within a loop.

I also would like to ask something on your feedback too:  
Now considering I upgrade my file as you suggested like  
CAT file1  
1 |abc |123 |abc123  
2 |bcd |234 |bcd234  
3 |cde |345 |cde345  
.  
.  
100 |uvw |999 |uvw999  
In this situation also your command will give erronous results. What should I do in such a situation?

---

<div class="post-metadata">

**Author:** ![udiptya](https://community.unix.com/letter_avatar/udiptya/32/6_449c533f586939b6e5f246632a46421f.png) [@udiptya](https://community.unix.com/u/udiptya)\
**Post date:** [January 22, 2008, 2:42am UTC](https://community.unix.com/t/help-required-on-awk-command/182478/6 "2008-01-22T02:42:43Z")

</div>

I also tried with a slight modification of your command

AWK -F"|" '$1 == /'$count'/ {prinf $4}' file1

but this didn't work either

---

<div class="post-metadata">

**Author:** ![ThobiasVakayil](https://community.unix.com/letter_avatar/thobiasvakayil/32/6_449c533f586939b6e5f246632a46421f.png) [@ThobiasVakayil](https://community.unix.com/u/ThobiasVakayil)\
**Post date:** [January 22, 2008, 3:25am UTC](https://community.unix.com/t/help-required-on-awk-command/182478/7 "2008-01-22T03:25:37Z")

</div>

In my case the following command is working fine :  
$ count=2  
$ awk -F"|" '$1 ~ /'$count'/ {prinf $4}' file1

The below mentioned command won't work :  
awk -F"|" '$1 == /'$count'/ {prinf $4}' file1  
== won't work.

Please try the first command and send the error details.

---

<div class="post-metadata">

**Author:** ![udiptya](https://community.unix.com/letter_avatar/udiptya/32/6_449c533f586939b6e5f246632a46421f.png) [@udiptya](https://community.unix.com/u/udiptya)\
**Post date:** [January 22, 2008, 3:44am UTC](https://community.unix.com/t/help-required-on-awk-command/182478/8 "2008-01-22T03:44:28Z")

</div>

Your command is working fine for the type of file you gave as an example. But consider the file I have given as an example in such a case the command

count=1  
AWK -F"|" '$1 ~ /'$count'/ {printf $4}' file1

will give the output for all the numbers starting with 1 (eg 1, 100, 101..etc)in the first column of the table! Since ~ gives an approimate matching this won't help. I need an exact watch which is possible with ==

---

<div class="post-metadata">

**Author:** ![ThobiasVakayil](https://community.unix.com/letter_avatar/thobiasvakayil/32/6_449c533f586939b6e5f246632a46421f.png) [@ThobiasVakayil](https://community.unix.com/u/ThobiasVakayil)\
**Post date:** [January 22, 2008, 3:55am UTC](https://community.unix.com/t/help-required-on-awk-command/182478/9 "2008-01-22T03:55:11Z")

</div>

Hello,

If you want to get the line starts with 100 means, you have to give like this :  
count=100  
awk -F"|" '$1 ~ /'$count'/ {print $3} file1

you can also give the search string like this :  
$ search\_string="aaa"  
$ awk -F"|" '$2 ~ /'$search\_string'/ {print $3} file1

count is not the line number. It's a search string only.  
Hope you got better idea.

---

<div class="post-metadata">

**Author:** ![udiptya](https://community.unix.com/letter_avatar/udiptya/32/6_449c533f586939b6e5f246632a46421f.png) [@udiptya](https://community.unix.com/u/udiptya)\
**Post date:** [January 22, 2008, 4:59am UTC](https://community.unix.com/t/help-required-on-awk-command/182478/10 "2008-01-22T04:59:07Z")

</div>

I have understood your point of view btu I guess I couldn't properly express you with my problem. So, I am writing a part of my program underneath

my file is something like this with tabs and whitespaces inbetween and delimited by "|'

CAT file1  
abc |123 |234  
bcd |345 |456  
cde |567 |678

Now my program is something like this:

start\_cnt=1  
end\_cnt=200  
while [start\_cnt -le end\_cnt]  
do  
var1=`AWK -F"|" '...........{ printf $2 }' file1`  
echo "$var1" \>\> file2  
start\_cnt=`expr $start_cnt + 1`  
done  
echo "Finished file appending"

Now I don't know what to write in the space provided in the AWK command such that it will use the start\_cnt as the incrementing variable starting with line 1 as start\_cnt=1 and line 200 as start\_cnt=200 and on each step will append the value at position $2 to file2

I hope now I could make you understand my problem!

---

<div class="post-metadata">

**Author:** ![ThobiasVakayil](https://community.unix.com/letter_avatar/thobiasvakayil/32/6_449c533f586939b6e5f246632a46421f.png) [@ThobiasVakayil](https://community.unix.com/u/ThobiasVakayil)\
**Post date:** [January 22, 2008, 5:19am UTC](https://community.unix.com/t/help-required-on-awk-command/182478/11 "2008-01-22T05:19:12Z")

</div>

I think no need to use the loop in here.  
You can just use the awk command and write the output to a file like this :  
search\_string="xxx"  
awk -F"|" '$1 ~ '/$search\_string/' { printf $2 }' file1 \> file2

---

<div class="post-metadata">

**Author:** ![udiptya](https://community.unix.com/letter_avatar/udiptya/32/6_449c533f586939b6e5f246632a46421f.png) [@udiptya](https://community.unix.com/u/udiptya)\
**Post date:** [January 22, 2008, 6:14am UTC](https://community.unix.com/t/help-required-on-awk-command/182478/12 "2008-01-22T06:14:40Z")

</div>

Yes, that can be done but for a long table if you have to search each element of the field, you will require a lot of code.  
Whereas I am looking for a very genaralised pattern!

---

<div class="post-metadata">

**Author:** ![vgersh99](https://community.unix.com/user_avatar/community.unix.com/vgersh99/32/14851_2.png) [@vgersh99](https://community.unix.com/u/vgersh99)\
**Post date:** [January 22, 2008, 6:41am UTC](https://community.unix.com/t/help-required-on-awk-command/182478/13 "2008-01-22T06:41:03Z")

</div>

It's a bit expensive to scan the ENTIRE input file on EVERY iteration of the loop, but...  
Given udi.txt:

```nohighlight
abc |123 |234
bcd |345 |456
cde |567 |678

```

```nohighlight
#!/bin/sh

start_cnt=1
end_cnt=200
while ["${start_cnt}" -le "${end_cnt}"]
do
   var1=`awk -F"|" -v fnr="${start_cnt}" 'FNR == fnr { printf $2 }' udi.txt`
   echo "$var1"
   start_cnt=`expr $start_cnt + 1`
done
echo "Finished file appending"

```

---

<div class="post-metadata">

**Author:** ![udiptya](https://community.unix.com/letter_avatar/udiptya/32/6_449c533f586939b6e5f246632a46421f.png) [@udiptya](https://community.unix.com/u/udiptya)\
**Post date:** [January 22, 2008, 7:43am UTC](https://community.unix.com/t/help-required-on-awk-command/182478/14 "2008-01-22T07:43:03Z")

</div>

Thanks a lot. It worked! I tried that way but I was using NR in place FNR.  
Thanks again!
