Help required on AWK command

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

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

Hi,

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

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

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?

I also tried with a slight modification of your command

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

but this didn't work either

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.

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

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.

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!

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

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!

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

abc |123 |234
bcd |345 |456
cde |567 |678
#!/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"

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