(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!
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
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?
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 ==
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!