0

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

[?1h=
=> ["AD070517",
 "AD070518",
: ESCESCOOBB "AD070809",
 "NE0000013",
 "NE0000014",
: ESCESC[[66~~ "LG100085-097",
 "LG100085-098",
]

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.

0

3 Answers 3

2

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

or

grep -Po '[-A-Z0-9]+(?=")' data.txt > clean.txt
1
  • 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, 2019 at 13:42
1

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
6
  • 1
    That still doesn't remove the things before and after the match
    – Philippos
    May 16, 2019 at 13:30
  • @Philippos Ah, then you have to match it so you can throw it away. Edited my answer to show how. May 16, 2019 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, 2019 at 13:41
  • @GrayedFox OK. But you said you wanted to trim the quotes. May 16, 2019 at 13:43
  • Not quite trim - but extract (or match) only the characters between them ;)
    – GrayedFox
    May 16, 2019 at 13:46
0

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]+)"/'

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .