Stripping out more than a space from a line, but keep single space.


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Stripping out more than a space from a line, but keep single space.
# 1  
Old 03-05-2010
Stripping out more than a space from a line, but keep single space.

Hi all,

Is there a way to perform the above, I am trying to strip out more than one space from a line, but keep the single space. See below output example.

Code:
My   Name is test  test2 test3  test4 test5
My Name is test test2 test3 test4 test5

Please note that the lines would contain different text, so that pattern match cannot be done using name from the text.

Thanks

Last edited by pludi; 03-05-2010 at 06:42 AM..
# 2  
Old 03-05-2010
Try this,

Code:
sed  -r "s/[ ]+/ /g" file

# 3  
Old 03-05-2010
Code:
tr -s '  '.

Use the above command
This is used to squeeze the repeated space character into a single character.
So repeated space character will become as single space.
And it wont affect the single space.

Example:
Code:
echo "My   Name is test  test2 test3  test4 test5" | tr -s ' '

The output is
Code:
My Name is test test2 test3 test4 test5

# 4  
Old 03-05-2010
Try This

Code:
cat filename | tr -s ' '
or
tr -s ' ' < filename

# 5  
Old 03-05-2010
Another one:

Code:
awk '$1=$1' file

# 6  
Old 03-05-2010
vi
Code:
:%s/  */ /g

vim
Code:
:%s/\s+/ /g

sed
Code:
sed -e 's/  */ /g' file

Perl
Code:
perl -pe 's/(\s)+/$1/g' file

# 7  
Old 03-05-2010
Code:
awk '$1=$1'

Code:
sed 's/  */ /g'

Code:
sed 's/[[:space:]]\{2,\}/ /g'

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Getting email output in single line with out space in email

I have tried below email method and i am getting every thing in single line . i have put echo to provide space, but it is not helping my code ( echo "From: $FROM" echo "To: $MAILTO" echo "CC: $CC" echo "Subject: $SUBJECT" echo "MIME-Version: 1.0" echo 'Content-Type: multipart/mixed;... (6 Replies)
Discussion started by: mirwasim
6 Replies

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

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

4. Shell Programming and Scripting

Calculate total space, total used space and total free space in filesystem names matching keyword

Good afternoon! Im new at scripting and Im trying to write a script to calculate total space, total used space and total free space in filesystem names matching a keyword (in this one we will use keyword virginia). Please dont be mean or harsh, like I said Im new and trying my best. Scripting... (4 Replies)
Discussion started by: bigben1220
4 Replies

5. Shell Programming and Scripting

Break lines up into single lines after each space in every line

It sounds a bit confusing but what I have is a text file like the example below (without the Line1, Line2, Line3 etc. of course) and I want to move every group of characters into a new line after each space. Example of text file; line1 .digg-widget-theme2 ul { background: rgb(0, 0, 0) none... (7 Replies)
Discussion started by: lewk
7 Replies

6. UNIX for Dummies Questions & Answers

tab char appearing as single space?

I'm trying to run a script which will ssh to several other servers (All Solaris 10) and execute a sar -f command to get each server's CPU usage for a given hour. It kinda works OK but I just can't figure out how to separate the returned fields with a Tab character. I've done lots of searching... (2 Replies)
Discussion started by: jake657
2 Replies

7. Shell Programming and Scripting

replace space or spaces in a line of a file with a single :

I am searching while I await a response to this so if it has been asked already I apologize. I have a file with lines in it that look like: bob johnson email@email.org I need it to look like: bob:johnson:email@email.org I am trying to use sed like this: sed -e 's/ /:/g' file >... (5 Replies)
Discussion started by: NewSolarisAdmin
5 Replies

8. Shell Programming and Scripting

delete a single space in a line

hi i have a line like TL1330000000800 000DE9248737900 08000TS0231DE92 87379AMEX0000.T N 0080000000 00000. if there any single space between strings, i need to delete, if more than one keep the space as it is can some one help thanks Satya (1 Reply)
Discussion started by: Satyak
1 Replies

9. Shell Programming and Scripting

stripping white space...

Hi All; Having a problem with a file.. the file contains the following data... (a snapshot) 1331F9E9DB7C2BB80EAEDE3A8F043B94,AL7 1DZ,M,50 186FDF93E1303DBA217279EC3671EA91,NG5 1JU,M,24 3783FFAF602015056A8CD21104B1AAAF,CH42 4NQ,M,17 It has 3 columns sepreated by a , the second column... (7 Replies)
Discussion started by: Zak
7 Replies

10. Shell Programming and Scripting

replacing single space in argument

I want to write a script which will check the arguments and if there is a single space(if 2 more more space in a row , then do not touch), replace it with _ and then gather the argument so, program will be ran ./programname hi hello hi usa now hello hello so, inside of program,... (7 Replies)
Discussion started by: convenientstore
7 Replies
Login or Register to Ask a Question