I have line from log file:
field 1234 "text in quotes" 1234 "other text in quotes"
I would like to replace spaces in between quotes, so I could than extract columns using space as delimiter. So the result could be something like
field 1234 "text@in@quotes" 1234 "other@text@in@quotes"
I was not able to find out working regex for sed myself. Thanks a lot for help. Martin
Pipe your log file through this awk command:
awk -F\" '{OFS="\"";for(i=2;i<NF;i+=2)gsub(/ /,"@",$i);print}'
Thanks for all answers.
This is perl one-liner I finally use:
perl -pe 's{("[^\"]+")}{($x=$1)=~s/ /@/g;$x}ge'
it results in required
field 1234 "text@in@quotes" 1234 "other@text@in@quotes"
.
Ruby(1.9+)
$ cat file
field 1234 "text in quotes" 1234 "other text in quotes"
$ ruby -ne 'print $_.gsub(/(\".*?\")/){|x| x.gsub!(/\s+/,"@") }' file
field 1234 "text@in@quotes" 1234 "other@text@in@quotes"
by using double quote as RS all even records are the ones inside double quotes. replace space in those even records. Since output records separator is newline by default ,change it to double quote.
awk 'BEGIN {RS="\"";ORS="\"" }{if (NR%2==0){gsub(/ /,"@",$0);print $0}else {p
rint $0}}' InputText.txt
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With