0
0
0_O2017-05-30 18:56:57
linux
0_O, 2017-05-30 18:56:57

How to make awk or sed not split substrings with quoted spaces?

There is a file where strings can contain substrings delimited by double quotes.
These substrings can be either with or without spaces.
awk does not accept quotes and separates such substrings by spaces.
Example:
cat test.txt abc

"line1" where flick is "line 2" lmnop
bav "line 3" where flick is "line 4" lmnop
cat test.txt | awk '{print $1":"$3":"$4":"$6}'
abc:where:jzik:2" ab
:3":where:"string
to get:
abc:where:jzik:lmnop baw
:where: zhzik: lmnop
Please help.

Answer the question

In order to leave comments, you need to log in

3 answer(s)
D
Dmitry, 2017-05-31
@Tabletko

Alternatively, use sed to remove quoted substrings

M
MustDie95, 2017-05-31
@MustDie95

Why not first remove the extra substrings and then substitute the required separator instead of spaces?
cat test.txt | sed -E 's/(["]+[a-zA-Za-zA-Z0-9 ]+["]+)//g; s/+/:/g'

S
Saboteur, 2017-05-31
@saboteur_kiev

Possible without sed and without awk

cat test.txt | while read; do eval 'for word in '$REPLY'; do echo -n "$word"|tr " " "_"; echo -n " "; done' | cut -d " " --output-delimiter=":" -f 1,3,4,6;done

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question