txt with this format
"service blabla 1" 5.456
"service blabla 2" 456
i need remove space in blank only between " "
"serviceblabla1" 5.456
"serviceblabla2" 456
i have this command
sed 's/ //g'
result:
"serviceblabla2"456
Use sed 's/^ *//g', to remove the leading white spaces. There is another way to remove whitespaces using `sed` command. The following commands removed the spaces from the variable, $Var by using `sed` command and [[:space:]]. $ echo "$Var are very popular now."
s/[[:space:]]//g; – as before, the s command removes all whitespace from the text in the current pattern space.
The easy way would be select everything (Ctrl+A), go to Edit>Blank Operation>Trim Trailing Space. This should remove all the spaces in between.
So glob chars are in play. You can also use echo to remove blank spaces, either at the beginning or at the end of the string, but also repeating spaces inside the string. This is not reliable : if myVar contains special characters like *, the shell will expand it with the files of the current directory.
With sed 's/ //N', where N is an integer between 1 and 9, you can pick which space to remove. This uses the $ {parameter/pattern/string} parameter expansion in bash to do the same thing as the sed command, but on the value in $var. The resulting string is, in this example, then stored back into $var.
Solution: 1) remove all the white space between the words making on long string of chars with no white space between then then delete that file. or 2) place quotes on both sides of the filename with spaces then delete that file.
To "delete all blank spaces" can mean one of different things: delete all occurrences of the space character, code 0x20. delete all horizontal space, including the horizontal tab character, "\t". delete all whitespace, including newline, "\n" and others.
In GNU awk
with shown samples, please try following awk
code.
awk -v RS='"[^"]*"' '{gsub(/[[:space:]]+/,"",RT);ORS=RT} 1' Input_file
Explanation: Setting RS as "[^"]*"
to get values from "
till next occurrence of "
. Then removing spaces in its value to match OP's requirement. Finally printing the line.
Above code will print data on terminal, in case you want to inplace save into Input_file, then try following command.
awk -v RS='"[^"]*"' '{gsub(/[[:space:]]+/,"",RT);ORS=RT} 1' Input_file > temp && mv temp Input_file
You can use perl
here:
perl -i -pe 's{"[^"]*"}{$&=~s|\s+||gr}ge' file
Note that -i
will replace the text inline.
The "[^"]*"
pattern will match any substrings in between the closest double quotation marks and $&=~s|\s+||gr
willl remove all chunks of one or more whitespace chars inside the matches only.
See an online demo:
#!/bin/bash
s='"service blabla 1" 5.456
"service blabla 2" 456'
perl -pe 's{"[^"]*"}{$&=~s|\s+||gr}ge' <<< "$s"
Output:
"serviceblabla1" 5.456
"serviceblabla2" 456
With GNU awk for the 3rd arg to match():
$ awk 'match($0,/("[^"]*")(.*)/,a){gsub(/ /,"",a[1]); print a[1] a[2]}' file
"serviceblabla1" 5.456
"serviceblabla2" 456
or FPAT:
$ awk -v FPAT='"[^"]*"| .*' '{gsub(/ /,"",$1); print $1 $2}' file
"serviceblabla1" 5.456
"serviceblabla2" 456
You can use a small loop:
sed -r ':a; s/ (.*")/\1/;ta' file
Your sed
command was close but you requested a global substitution which did not give your desired output.
Splitting the sed
command to address the individual spaces with the same command would have worked.
sed 's/ //2;s/ //' $file
The will remove the first and second occurence of space.
Output
"serviceblabla1" 5.456
"serviceblabla2" 456
This code will only work on the first two spaces.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With