Select a specific line after matching a pattern I just recently started using AWK and I'm still learning about it. I have solved the problem I'm about to show but I feel it's not the best solution and I'm trying to find a solution to fit within an AWK command rather than keep piping.

I have a bunch of .txt files ending for which I read the header (1st line only).

head -1 *.txt 


Outputs:

==&gt; anglia.txt &lt;== 
String - Anglian            

==&gt; carr.txt &lt;==   
String - Carr               
etc..


From here I have a case switch where a user inputs 1-9. It's sorted so first line is always Anglian and second is always Carr.. etc
So if a user inputs 1 I know they want to select anglian. But for further process my code I need to extract the String "Anglian".

head -1 *.txt | awk '/[a-z]/&amp;&amp;!/.txt/' 


Outputs:

String - Anglian
String - Carr


Here I got rid of the first line that had the filename.

head -1 *.txt | awk '/[a-z]/&amp;&amp;!/.txt/' | awk '{print $3}'


Outputs:

Anglian
Carr


Here I selected column 3 which contains the String that I need!

head -1 *.txt | awk '/[a-z]/&amp;&amp;!/.txt/' | awk 'NR==1{print $3}'


Outputs:

Anglian


Here I selected the first row which is exactly the output that I want! However I had to use an extra pipe. All I want is a awk command that does all of this in a single command somethings around the lines of:

head -1 *.txt | awk '/[a-z]/&amp;&amp;/.txt/{if(NR==1)print $3}'


Outputs:

EMPTY LINE


This is the problem I'm having. Since I'm having the pattern and NR in 1 command it's selecting the 1st line but the first line is a line that is being hidden by my pattern and it's the ==&gt; anglian.txt &lt;== hence

head -1 *.txt | awk '/[a-z]/&amp;&amp;/.txt/{if(NR==2)print $3}'


Outputs:

Anglian


However this is of no use because only matches Anglian where it is NR==2 and due to my case switch that I have I want it to be NR==1 otherwise the code does not work.

Is this possible? I hope I made myself clear here :)
You can replace this pipeline

head -1 *.txt | awk '/[a-z]/&amp;&amp;!/.txt/' | awk '{print $3}'


with this one awk command

awk 'FNR == 1 {print $3}' *.txt


FNR is the file record (i.e. line) number. NR is the cumulative record number of all records seen from all files.



Now, you can select the user's numeric choice with

awk 'FNR == 1 {print $3}' *.txt | awk -v n="$user_selection" 'NR == n'


or, with a single awk:

awk -v n="$user_selection" 'FNR == 1 &amp;&amp; ++filenum == n {print $3; exit}' *.txt




If you're looking for a way to get your users to select a name from one of the files, perhaps some more advanced bash:

# read the 3rd word from the 1st line of all txt files into an array
readarray -t names &lt; &lt;(awk 'FNR == 1 {print $3}' *.txt)

# get the user to select one of them
PS3="Choose a name: "
select name in "${names[@]}"; do
    [ "$name" ] &amp;&amp; break
done

echo "$name"




This really isn't a code review. Since you're asking "how can I do this", you should have asked on Stack Overflow.