Charlie Bondurant Charlie Bondurant - 22 days ago 5
Bash Question

Extracting values from a CSV-like file consisting of key=value pairs using SED

Input file:

Test1=169930;Testing2=240;Tester3=403;Tests4=RCFL2PKN;


Need to get output like:

169930;240;403;RCFL2PKN;


Tried to do it like this:

sed 's/(([A-Z][a-z]+)[=](^[0-9]+)[^;])//g'


didn't return any error, but nothing happened with output file.

Please help.

Answer
sed 's/[^=]\+=\([^;]\+;\?\)/\1/g' file

In the basic regular expression syntax you should write \+, \(, \), and \? instead of +, (, ), and ? correspondingly. If you find this syntax inconvenient, you may use the extended syntax:

sed -r 's/[^=]+=([^;]+;?)/\1/g' file

The only difference between basic and extended regular expressions is in the behavior of a few characters: ?, +, parentheses, and braces ({})

If you want to modify the file in-place, use -i option. If you want to output the result into another file, use I/O redirection:

sed -r 's/[^=]+=([^;]+;?)/\1/g' file > outfile