I need to extract a word from a line in a shell script, I've seen similar examples though none of which worked.
Take the example sentence
word1 word2 word3/pattern/word4.word5 word6
Given pattern
it should return word3/pattern/word4.word5
without the whitespace on either side.
In addition it should constrain that the line can't start with certain characters, #
.
I managed to find a grep command to isolate the line
grep "^[^#].*pattern" $FILE
will return all the lines which don't start with #
and contain pattern
. How can I then extract the word from this line?
edit: using ubuntu 20.04
Also regarding the sentence, the example is more or less exactly how it should look. The pattern will appear at most once per line, and will be separated from other words by whitespace. Given a line I want to return the word containing the pattern, taking everything left and right until you hit a whitespace, not including the whitespace.
grep
for example? Is this Linux? And how exactly do you define "word"? Do you just want the longest string that containspattern
and has no whitespace characters?