简体   繁体   中英

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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