[SOLVED] [regex] Right syntax to parse semicolon-separated data?
by littlebigman from LinuxQuestions.org on (#5NKNR)
Hello,
I can't figure out what regex to use with sed to parse a CSV file that uses the semicolon as separator: In both cases, the output contains all the columns instead of just 1, 3, and 5:
Code:input.txt:
FIELD1;FIELD2;FIELD3;FIELD4;FIELD5
123;Blah;456;Blah;Some text
sed -r 's@^(\d+);.+?;(\d+);.+?;(.+)$@\1\t\2\t\3@' < input.csv > output.csv
sed -r 's@^(\d+);[^;]+;(\d+);[^;]+;(.+)$@\1\t\2\t\3@' < input.csv > output.csvAny idea?
FWIW, I'm using sed (GNU sed) 4.4.
Thank you.
I can't figure out what regex to use with sed to parse a CSV file that uses the semicolon as separator: In both cases, the output contains all the columns instead of just 1, 3, and 5:
Code:input.txt:
FIELD1;FIELD2;FIELD3;FIELD4;FIELD5
123;Blah;456;Blah;Some text
sed -r 's@^(\d+);.+?;(\d+);.+?;(.+)$@\1\t\2\t\3@' < input.csv > output.csv
sed -r 's@^(\d+);[^;]+;(\d+);[^;]+;(.+)$@\1\t\2\t\3@' < input.csv > output.csvAny idea?
FWIW, I'm using sed (GNU sed) 4.4.
Thank you.