DomainsFeatured DomainsFeatured - 2 months ago 8
Linux Question

If Line Matches Anything After String, Then Do Command

I'm fairly good with grep, cut, sed, etc. However, I'm having a tough time extracting this data from a file. It looks like this:

--timestamp-- data://example1// -----| This could be anything or nothing
--timestamp-- data://example2// -----| This could be something
--timestamp-- data://example3// -----|
--timestamp-- data://example4// -----| This could something else
--timestamp-- data://example5// -----| This could be another something
--timestamp-- data://example6// -----|
--timestamp-- data://example7// -----| This could something else


I would like to return all lines that have anything after the string
-----|
so the output would be:

example1
example2
example4
example5
example7


I know that I need to use an IF statement based on a
grep '-----|.*$'
match. It would be if there is any characters after the string, then output the line. Then, I could pipe to an awk command that would extract between the delimiters
//
before and after example. I'm just really stuck on how to structure the if statement.

Any help with this IF statement would be very appreciated.

Answer

You can use awk to do this using // as field separator and print the 2nd field:

awk -F'//' '/-{5}\|[ \t]*[^ \t]/{print $2}' file

example1
example2
example4
example5
example7

Regex: /-{5}\|[ \t]*[^ \t]/ searches for five dashed followed by a literal pipe followed by 0 or more whitespace and then a non-whitespace character.

You can also use POSIX class [[:blank:]] to match a space or tab:

awk -F'//' '/-{5}\|[[:blank:]]*[^[:blank:]]/{print $2}' file