Cannot get to convert multiple spaces to one space


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Cannot get to convert multiple spaces to one space
# 1  
Old 03-25-2010
Cannot get to convert multiple spaces to one space

Hi Guys,

I am using a Redhat Linux Centos machine and trying to convert multiple spaces in a file to one space. I am using:

Code:
sed '/./,/^$/!d' input_file > output_file

I also tried

Code:
cat -s

Both gave me no change in the output file.
I tried this on cygwin and it worked just fine. I am using the bash shell.

Is there any other "universal" way to do this?

Thanks.
# 2  
Old 03-25-2010
Hi, I hope this works
Code:
awk '{gsub(/[ ]+/," ")}1' FILE

# 3  
Old 03-25-2010
Code:
awk '{$1=$1}1' FILE
tr -s ' '
sed 's/\( \+\)/ /g'

# 4  
Old 03-26-2010
Quote:
Originally Posted by rdcwayx
Code:
awk '{$1=$1}1' FILE
tr -s ' '
sed 's/\( \+\)/ /g'

The AWK proposal should definitely not be used. The goal is to squeeze multiple spaces into one, but that AWK oneliner will delete all leading and trailing whitespace (even if it's just a single space). Worse, it will delete any tabs in the data.

A nitpick regarding the sed, \+ I believe is a gnu basic regular expression extension. In standardized basic regular expressions, a plus sign is an ordinary character and a backslash will not make it into a multiplier. Instead of x\+, it would be more portable to use either x\{1,\} or xx*

I vote for the tr solution Smilie

Cheers,
Alister

Last edited by alister; 03-26-2010 at 12:43 PM..
# 5  
Old 03-26-2010
can somebody explain the working of the awk option
Code:
awk '{$1=$1}1' FILE

what is {$1=$1}1 in the above option doing . Thanks in advance
# 6  
Old 03-26-2010
Quote:
Originally Posted by alister
The AWK proposal should definitely not be used. The goal is to squeeze multiple spaces into one, but that AWK oneliner will delete all leading and trailing whitespace (even if it's just a single space). Worse, it will delete any tabs in the data.

A nitpick regarding the sed, \+ I believe is a gnu basic regular expression extension. In standardized basic regular expressions, a plus sign is an ordinary character and a backslash will not make it into a multiplier. Instead of x\+, it would be more portable to use either x\{1,\} or xx*

I vote for the tr solution Smilie

Cheers,
Alister
Hi Alister,

After seing your comment i have tested my code below with leading and trailing spaces and/or with tabs again, fortunately it worked well in all of this cases Smilie
But good to remember that we can use "tr" too.

Code:
awk '{gsub(/[ ]+/," ")}1' FILE

# 7  
Old 03-26-2010
Quote:
Originally Posted by asalman.qazi
can somebody explain the working of the awk option
Code:
awk '{$1=$1}1' FILE

what is {$1=$1}1 in the above option doing . Thanks in advance
AWK programs are pairs of patterns and actions. Either the pattern or the action may be absent, but not both. A missing pattern evaluates to true for all lines. A missing action (the code withing curly braces) defaults to "{print $0}", which prints the line.

That code represents two pattern-action pairs.

The first pair is "{$1=$1}". This pair is missing the pattern, so it will match on every line read. The action is the code between the curly braces. For each line read, it assigns the value of $1 to $1. This does not change the field's value, but it does cause $0 to be recomputed, with each field separated by OFS (output field separator, whose default value is a space). This leads to the loss of leading and trailing whitespace, and replacement of whatever strings were originally field separators (one space, multiple spaces, tabs, combinations thereof) with one OFS.

The second pair is simply the traling one, "1", nothing more. The pattern is the "1", which is a boolean true and will cause its corresponding action to execute for every line read by AWK. The action however is missing, so it defaults to printing the line (as stated above). You see this idiom used often by AWKers trying to save a few characters; it is shorthand for printing the current line ($0).

Cheers,
Alister

Last edited by alister; 03-26-2010 at 03:24 PM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Convert mutiple spaces file to single tab

I have the following file I wanted to convert mutiple spaces to tab: I tried cat filename | tr ' ' '\t' or sed 's/ */ /' FILE but it looses the format 5557263102 5557263102 5552074858 5726310211 5557263102 5557263102 5557263103 5557263103 2142406768 ... (2 Replies)
Discussion started by: amir07
2 Replies

2. Shell Programming and Scripting

Replacing Multiple spaces with a single space but excluding few regular expressions

Hi All. Attached are two files. I ran a query and have the output as in the file with name "FILEWITHFOURRECORDS.txt " I didn't want all the spaces between the columns so I squeezed the spaces with the "tr" command and also added a carriage return at the end of every line. But in two... (3 Replies)
Discussion started by: sparks
3 Replies

3. Shell Programming and Scripting

sed replace one space and leave other spaces untouched

Hi Friends, I looked up online, but couldn't figure out a proper solution. I have an input file where the columns are separated by multiple spaces and the column content is separated by single space. For example, Chr1 hello world unix is fun In the above example, chr1 is first... (3 Replies)
Discussion started by: jacobs.smith
3 Replies

4. UNIX for Dummies Questions & Answers

Replacing double spaces with single space

I am looking for a regular expression that uses sed to replace multiple spaces with single spaces on every line where it is not at the start of the line and not immediately before double slashes ('//') or between quotes ("). In its simplest form, it would look like this: sed -e 's# # #g'... (4 Replies)
Discussion started by: figaro
4 Replies

5. Shell Programming and Scripting

Shell command to convert low values to spaces

I neead a script which converts low values to the spaces, When I used sed -e 's/\x00/\x20/g' inputfile command it is removing the low values but not replacing it with spaces. Please help me. Its Uregent. Thanks Sam (12 Replies)
Discussion started by: bsreee35
12 Replies

6. UNIX for Dummies Questions & Answers

How to translate multiple spaces into a single space using tr command?

I am trying to read a txt file and trying to translate multiples spaces into single spaces so the file is more organized, but whenever I try the command: tr ' ' ' ' w.txt The output is: tr: extra operand `w.txt' Try `tr --help' for more information. Can someone please help? :wall: ... (2 Replies)
Discussion started by: Nonito84
2 Replies

7. Shell Programming and Scripting

How to avoid the truncating of multiple spaces into a single space while reading a line from a file?

consider the small piece of code while read line do echo $line done < example content of example file sadasdasdasdsa erwerewrwr ergdgdfgf rgerg erwererwr the output is like sadasdasdasdsa erwerewrwr ergdgdfgf rgerg erwererwr the... (4 Replies)
Discussion started by: Kesavan
4 Replies

8. Shell Programming and Scripting

convert rows (with spaces) to columns

Hey all, I have a list in the format ; variable length with spaces more variable information some more variable information and I would like to transform that 'column' into rows ; variable length with spaces more variable information some more variable information Any... (8 Replies)
Discussion started by: TAPE
8 Replies

9. Shell Programming and Scripting

Scripting to convert underscores to spaces

Greetings, I have a bunch of music files that I want to strip the underscores out, and leave only spaces. All that I've found on the web is how to add underscores to files that have spaces, and reversing the "tr" command does not make a difference. Here is how to convert spaces to... (6 Replies)
Discussion started by: brakeb
6 Replies

10. Shell Programming and Scripting

Consecutive spaces within input being converted to single space

I'm reading from a file that is semi-colon delimited. One of the fields contains 2 spaces separating the first and last name (4th field in - "JOHN<space><space> DOE"): e.g. TORONTO;ONTARIO;1 YONGE STREET;JOHN DOE;CANADA When I read this record and either echo/print to screen or write to... (4 Replies)
Discussion started by: NinersFan
4 Replies
Login or Register to Ask a Question