Following data represents 2 lines of text file:
1 2340: 2 1930: 1
1 9: 3 4501: 1 45: 1 5620: 2
I want to delete the space after ":". So the output of above text file should be
1 2340:2 1930:1
1 9:3 4501:1 45:1 5620:2
Use the following: If you want to replace ":" character followed by single/multiple spaces (" " or "\t" aka tab character(s)).
sed -i "s/:[ \t][ \t]*/:/g" filename.txt
or If you just want to substitute ":" with only one visible space into ":"
sed -i "s/:[ \t]/:/g" filename.txt
PS:
You can also use other characters for ex: ^ or # etc in place of / in sed command.
For ex: sed -i "s#koba#loki#g" filename.txt.
-i option (I used above) will update the file (i.e. in place update) instead of throwing the std output on screen. Don't use -i until you are sure, what you are updating. Taking a backup of the given file first, helps sometimes.
You can use:
EXPLANATION:
sed -e "COMMAND"
will execute the command over file.dat`.s
says sed to Substitute/: /:/
are the chain to substitute and the chain desired, separated by/
.g
tellssed
to be executed more than once time per line.As @Tom commented, the quotes are better single quotes, and
-e
is not necessary, then:EDIT 2 As @josifoski commented, the * after the space char, will allow more than one space, not just one
You can read some
sed
docs here:AWK GSUB