Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how delete space in blank only between " " with bash

Tags:

regex

sed

awk

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
like image 612
mohamed Avatar asked Aug 09 '21 15:08

mohamed


People also ask

How do I remove blank space in bash?

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."

How do I remove blank spaces in Linux?

s/[[:space:]]//g; – as before, the s command removes all whitespace from the text in the current pattern space.

How do I remove blank spaces in a text file?

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.

How to remove blank spaces from a string in shell script?

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.

How do I remove a space between 1 and 9 in Bash?

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.

How to delete a file with no white space between words?

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.

What does it mean to delete all blank spaces in Excel?

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.


Video Answer


5 Answers

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
like image 96
RavinderSingh13 Avatar answered Oct 22 '22 17:10

RavinderSingh13


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
like image 28
Wiktor Stribiżew Avatar answered Oct 22 '22 16:10

Wiktor Stribiżew


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
like image 25
Ed Morton Avatar answered Oct 22 '22 16:10

Ed Morton


You can use a small loop:

sed -r ':a; s/ (.*")/\1/;ta' file
like image 32
Walter A Avatar answered Oct 22 '22 15:10

Walter A


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.

like image 33
HatLess Avatar answered Oct 22 '22 15:10

HatLess