One way: awk :
awk '!array[$2]++ && $2 ~ /^1259$|^3009$|^1589$/' file.txt
Results:
ADWN 1259 11:00 B23 ADWN 3009 12:00 B19 ADWN 1589 14:20 B12
change
I should really get used to reading the whole question first. I see that you are thinking of creating a file with the values ββthat you would like to find in the first case. Place them in a file called values.txt with one value per line. For instance; here is the contents of values.txt :
1259 3009 1589
Then run this:
awk 'FNR==NR { array[$0]++; next } $2 in array { print; delete array[$2] }' values.txt file.txt
Results:
ADWN 1259 11:00 B23 ADWN 3009 12:00 B19 ADWN 1589 14:20 B12
Explanation of the first command:
If the second column ( $2 ) is equal to one of these three values, add it to the array if it does not already exist. awk prints the entire line by default.
Description of the second command:
FNR - the number of records relative to the current input file.
NR - total number of records.
The FNR==NR { ... } construct is valid only for the first input file. Therefore, for each of the lines in values.txt we add the entire line ( $0 ) to the array (I called it an array, but you could give it a different name). next causes awk to read the next line in values.txt (and skip processing the rest of the command). When FNR==NR ceases to be true, the second file in the argument list is read. Then we check the second column ( $2 ) in the array, if it is there, print it and remove it from the array. Using delete , we essentially set the maximum number of units.
source share