Issue
I have a file and I only want to find lines that have "here". In each of these lines there are multiple string and integer values (see example below). I only want the first integer of each line that matches the pattern.
I have created a solution that uses a bash script, but is there a simpler method I am missing. I was hoping something like grep -w here -Eo [0-9] file
would work. However when I try that it expects anything that comes after "here" to be the file.
STEP 1 STAGE 1 here other info
foo
bar
STEP 2 STAGE 1 here other info
more
foo
bar
STEP 3 STAGE 1 here other info
For this file the desired output would be
1
2
3
Solution
This simpler awk
should work for you:
awk '/ here / {sub(/^[^0-9]+/, ""); print $1+0}' file
1
2
3
Answered By - anubhava Answer Checked By - Terry (WPSolving Volunteer)