Select a specific line after matching a pattern
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.