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.
3 个解决方案
#1
4
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.
#2
3
Example with GNU sed:
GNU sed示例:
$sed -r 's/(\".*\")|\s*/\1/g' file Thisisanexampletextwithsomespaces. Thisshouldbe2ndline. Howeverthespacesbetween"quotes should not change". lastline.
#3
2
Can be done using perl:
可以使用perl完成:
perl -pe 's{^\s*\n$}{}; s/ +(?=(([^"]+"){2})*[^"]*$)//g' file
This will delete all the blank line or lines with just 0 or more spaces and trim spaces when not between double quotes.
这将删除所有空行或行仅包含0或更多空格,并在不在双引号之间时修剪空格。
Live Demo: http://ideone.com/xizPNI
#1
4
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.
#2
3
Example with GNU sed:
GNU sed示例:
$sed -r 's/(\".*\")|\s*/\1/g' file Thisisanexampletextwithsomespaces. Thisshouldbe2ndline. Howeverthespacesbetween"quotes should not change". lastline.
#3
2
Can be done using perl:
可以使用perl完成:
perl -pe 's{^\s*\n$}{}; s/ +(?=(([^"]+"){2})*[^"]*$)//g' file
This will delete all the blank line or lines with just 0 or more spaces and trim spaces when not between double quotes.
这将删除所有空行或行仅包含0或更多空格,并在不在双引号之间时修剪空格。