Friday, November 27, 2009

Awk - replace blank spaces with single space


Input file 'file.txt' contains fields separated with uneven spaces (or tabs)

$ cat file.txt
6767 1212 9090 12
657676 1212 21212 21232
76767 12121 909090 121212
12 9090 1212 21


Required: Replace one or more space with single space or single tab or comma.

The solutions using awk:

$ awk -v OFS="," '$1=$1' file.txt
Output:
6767,1212,9090,12
657676,1212,21212,21232
76767,12121,909090,121212
12,9090,1212,21


$ awk -v OFS="\t" '$1=$1' file.txt
Output:
6767 1212 9090 12
657676 1212 21212 21232
76767 12121 909090 121212
12 9090 1212 21


$ awk -v OFS=" " '$1=$1' file.txt
Output:
6767 1212 9090 12
657676 1212 21212 21232
76767 12121 909090 121212
12 9090 1212 21


The solution using sed is here

9 comments:

Duncan said...

or just use:
tr -s " "
Handy!

Unknown said...

Rats, not quite what I was after. Any way to replace another character with a space or more spaces? Bash Unix seems to have a problem with spaces.

Unknown said...

@hugmyster, thanks for the question. Could you please help in understanding your requirement with an example, will be really helpful. Thanks.

Fitzcarraldo said...

Jadu, thanks for posting. Your command to replace spaces with a tab helped me a lot in extracting data from a .mht file with a space in some of the cells in a table to indicate an empty cell, and some cells with two strings separated by a space. I could not import the .mht file into Excel 2007 for work. Now the job is done. Thanks again!

Imtiaz Ahmed said...

This is not working on my system giving following error
awk -v OFS="*" '$1=$1' imtfile
awk: syntax error near line 1
awk: bailing out near line 1

AVI said...

Hi, I have a csv file which contains one liner output like this
cidcsspovm004 13 093105 22088
It shows all four values in a single row. I want it into tab-delimited file. Please help me on this.
Server Date Time lwps
cidcsspovm004 13 93105 22088


Thanks in Advance.

AVI said...

Hi, I have a output CSV file which contains one liner information I want it into tab delimited csv format.Can someone help me on this.
Output file data is like this.

cidcsspovm004 13 093105 22088

Unknown said...

@AVI,

Something like this ?

$ awk -v OFS="\t" '$1=$1' test.csv

Server Date Time lwps
cidcsspovm004 13 93105 22088

Unknown said...

"tr" is the tool for the job here.

To squeeze multiple spaces into a comma:

tr -s '[ ]' '[,]' < file.txt
perl -ple 'y/ /,/s' < file.txt
sed -e 's/[ ][ ]*/,/g' < file.txt

© Jadu Saikia www.UNIXCL.com