I have a file (data.txt) which contains strings like this:

=> ["AD070517",
: ESCESCOOBB "AD070809",
: ESCESC[[66~~ "LG100085-097",

I am attempting to extract only the entries from the array (minus the double quotes) using the following command:

sed -r 's/([-A-Z0-9]+)"/\1/g' data.txt > clean.txt

According to this regex snippet, that control group and match works as expected, and from what I understand of this post I should be able to output only the matches from sed and direct the output stream to a new file, but the resulting clean.txt file is an exact copy of data.txt with nothing filtered out.


You're sed command is not supposed to output the matches only.

s/pattern/replacement/g just replaces the pattern with the replacement, everything else is untouched.

Actually, the output should not be an exact copy, but the " after the pattern are getting removed.

You may want to use grep

grep -Eo '[-A-Z0-9]+"' data.txt | sed 's/"$//' > clean.txt


grep -Po '[-A-Z0-9]+(?=")' data.txt > clean.txt
| improve this answer | |
  • Excellent. Using grep and lookahead is an elegant and very clean solution, that fixed it! And well done noticing the missing double quotes too (I missed that). Cheers. For those wanting the whole thing (including the output file) I used grep -Po '[-A-Z0-9]+(?=")' data.txt > clean.txt – GrayedFox May 16 '19 at 13:42

Use -n to only show matching lines.

sed -n -r 's/([-A-Z0-9]+)"/\1/g' data.txt > clean.txt

and for the second question, replace the things you don't want with nothing:

sed -n -r 's/^.*"([-A-Z0-9]+)".*/\1/g' data.txt > clean.txt
| improve this answer | |
  • 1
    That still doesn't remove the things before and after the match – Philippos May 16 '19 at 13:30
  • @Philippos Ah, then you have to match it so you can throw it away. Edited my answer to show how. – L. Scott Johnson May 16 '19 at 13:32
  • Thanks for your speedy answer, gave it to the second one due to figuring out how to do it with a single command and for noticing it was trimming the double quote – GrayedFox May 16 '19 at 13:41
  • @GrayedFox OK. But you said you wanted to trim the quotes. – L. Scott Johnson May 16 '19 at 13:43
  • Not quite trim - but extract (or match) only the characters between them ;) – GrayedFox May 16 '19 at 13:46

You can tell sed to only print if the substitution was successful. Use the t command for that: it goes to the specified label if it was. b skips the rest of the code, p prints the replaced line.

sed -n -r 's/.*"([-A-Z0-9]+)".*/\1/g;t a;b;:a p' 

I had to include two missing double quotes on the last two lines.

Probably more readable using Perl:

perl -lne 'print $1 if /([-A-Z0-9]+)"/'
| improve this answer | |

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.