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



如下文本的示例行所示,此行中只有一对引号,我想删除引号中的所有空格,空间#是未知的,是有限的。我试图卸下空间,但最终仅删除了第一个空间或连续数量的空间,需要在引号中的单词之间删除所有空格。

中的所有空间。

示例字符串:

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

输出:

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

最新更新