-1

I have a log file, which outputs information this way:

2016-01-01: foo bar fnord
2016-01-01: this is static 'this is of interest' some blob bar
2016-01-01: this is static 'this is of interest' some hurz poit
2016-01-01: foo bar fnord
2016-01-01: this is static 'this is of interest as well' some blob bar

I want to only print the string inside the single quotes, and duplicates entries should be removed, as in:

this is of interest
this is of interest as well

I tried working with regexes that look for the content in between quotes, yet I didn't manage to get them working, e.g.:

grep -io "static.*" |  sed -e '\w+'|'\w+(\s\w+)*'
2

Here's a "sed-only" solution:

sed -n 's/^.*'\''\([^'\'']*\)'\''.*$/\1/p' file

This breaks down to

  • sed -n in combination with the poption at the end of the pattern: print only matching lines
  • '\''is shell notation to specify a single quote inside of a single-quoted string (the 's/…/…/p' argument)
  • therefore the pattern ^.*'\''\([^'\'']*\)'\''.*$ matches lines starting with any character sequence (^.*), followed by a single quote ', a sequence of characters which are not single quotes ([^'\'']*), followed by a single quote ', and finally any remaining characters up to the end of the line (.*$).
  • ([^'\'']*\)is enclosed in parantheses so sedwill store this part of the match into variable \1
  • Finally, s/pattern_explained_above/\1/preplaces the entire matching line with the contents of variable \1 (namely, the string part inside single quotes) and prints it (p option). All other lines which do not match the pattern are suppressed because of the -n option
  • That looks ... intense. Could you please describe the regex? What is each part doing? – k0pernikus Apr 5 '16 at 15:28
  • 1
    @k0pernikus I updated the answer with a description. I hope it makes sense 😉 – Guido Apr 5 '16 at 15:50
1

try

awk -F\' '/static/ { if (!seen[$2]++) print $2 ;}' 

with

  • static being static string
  • !seen[$2]++ will be true first time, then false
  • -F\' use ' as separator
1

cut is simpler to use than writing a regex:

grep -io "static.*" logfile.txt | cut  -d "'" -f2 | sort -u

manages to do the trick. It prints:

this is of interest
this is of interest as well
  • 1
    uniq will only remove adjacent duplicates; you would want to sort its input first (or just use 'sort -u') -- further assuming you don't care about the original order. – Jeff Schaller Apr 5 '16 at 15:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.