regexperlsedawkpattern-matching

Remove all spaces in lines but not between double quotes


Example:

input =

This is an example text with    some      spaces. 
This should be 2nd line.
However the spaces between "quotes    should not    change".
last line.

output =

Thisisanexampletextwithsomespaces. 
Thisshouldbe2ndline.
Howeverthespacesbetween"quotes    should not    change".
lastline.

Solution

  • awk '
        BEGIN {FS = OFS = "\""}
        /^[[:blank:]]*$/ {next}
        {for (i=1; i<=NF; i+=2) gsub(/[[:space:]]/,"",$i)} 
        1
    ' 
    
    Thisisanexampletextwithsomespaces.
    Thisshouldbe2ndline.
    Howeverthespacesbetween"quotes    should not    change".
    lastline.