简体   繁体   English

如何使用linux命令(如awk,sed)删除文本块内双引号之间的所有空格

[英]How to remove all spaces between double quotes within a block of text using linux command such as awk, sed

As shown by sample line of text below, there is only one pair of quotes within this line and I want to remove all spaces inside the quotes, # of spaces are unknown and is finite. 如下面的文本示例行所示,此行中只有一对引号,并且我想删除引号内的所有空格,#个空格是未知的并且是有限的。 I tried to remove spaces but only end up with removing the first space or consecutive number of spaces, need help on how to remove all spaces in between words within quotes. 我试图删除空格,但最终只删除了第一个空格或连续的空格,需要有关如何删除引号内单词之间所有空格的帮助。

Example string: 示例字符串:

000000 100614 0000000... "All spaces to be removed" A path/segment1/segment2

Output: 输出:

000000 100614 0000000... "Allspacestoberemoved" A path/segment1/segment2
$ awk 'BEGIN{FS=OFS="\""} {gsub(/[[:space:]]/,"",$2)} 1' file
000000 100614 0000000... "Allspacestoberemoved" A path/segment1/segment2
$ cat file 
000000 100614 0000000... "All spaces to be removed" A path/segment1/seg

$ awk '!(NR%2){gsub(FS,"")}1' RS=\" ORS=\" file
000000 100614 0000000... "Allspacestoberemoved" A path/segment1/seg

$ awk  'BEGIN{FS=OFS="\""}{for(i=2;i<NF;i+=2)gsub(/ /,"",$i)}1' file
000000 100614 0000000... "Allspacestoberemoved" A path/segment1/seg

$ sed -e :a -e 's/^\(\([^"]*"[^"]*"[^"]*\)*[^"]*"[^"]*\) /\1/;ta' file
000000 100614 0000000... "Allspacestoberemoved" A path/segment1/seg

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM