Previous in Forum: Mobile OS Installtion   Next in Forum: Install Primavera3.1 in windows7
Close
Close
Close
5 comments
Rate Comments: Nested
Anonymous Poster

How to Remove Spaces in a File - Unix

08/06/2010 1:50 AM

Hi

I have a .txt file in that 3 fields are there. Now i want to remove remove extra spaces in the file in all 3 columns.

Please tell the unix command to remove spaces in my .txt file

Reply
Interested in this topic? By joining CR4 you can "subscribe" to
this discussion and receive notification when new comments are added.

Good Answers:

These comments received enough positive votes to make them "good answers".
3
Guru

Join Date: Feb 2009
Location: Houston, USA
Posts: 946
Good Answers: 244
#1

Re: How to Remove Spaces in a File - Unix

08/06/2010 5:30 PM

It is not clear what exactly you want to do. Here are some tips that can help:

To remove the repeated space:

> cat file | tr -s " "

To remove the leading and tailing spaces:

> cat file | sed "s/^ *//g" | sed "s/ *$//g"

To remove the blank line from a file:

> cat file | grep -v '^$'

You can use awk and regular expression to do this type of things.

- MS

__________________
"All my technical advices in this forum must be consulted with and approved by a local registered professional engineer before implementation" - Mohammed Samad (Linkedin Profile: http://www.linkedin.com/in/msamad)
Reply Good Answer (Score 3)
Anonymous Poster
#2
In reply to #1

Re: How to Remove Spaces in a File - Unix

08/09/2010 7:53 AM

hi

i have 3 field in the file

ex :name address city

but the values are like(' Guest ',' mumbai ',' mumbai ')

now i want to remove those spaces contains in each field

Reply
Guru

Join Date: Feb 2009
Location: Houston, USA
Posts: 946
Good Answers: 244
#3
In reply to #2

Re: How to Remove Spaces in a File - Unix

08/09/2010 7:16 PM

Try this:

cat file | sed "s/ *//g" > new_file

(there is a space before * and no space between //)

This will remove all spaces from the file.

- MS

__________________
"All my technical advices in this forum must be consulted with and approved by a local registered professional engineer before implementation" - Mohammed Samad (Linkedin Profile: http://www.linkedin.com/in/msamad)
Reply
Anonymous Poster
#4
In reply to #3

Re: How to Remove Spaces in a File - Unix

08/11/2010 1:10 AM

hi

its not working

Reply
Active Contributor

Join Date: Aug 2010
Posts: 13
Good Answers: 1
#5

Re: How to Remove Spaces in a File - Unix

09/05/2010 12:02 AM

There is not a single command that will do a complex search and replace. But you can look up how to grep, sed, and awk if must stick with raw unix. You can appropos them to find out how to use them.

Reply
Reply to Forum Thread 5 comments

Good Answers:

These comments received enough positive votes to make them "good answers".
Copy to Clipboard

Users who posted comments:

Anonymous Poster (2); jsolar (1); msamad (2)

Previous in Forum: Mobile OS Installtion   Next in Forum: Install Primavera3.1 in windows7
You might be interested in: Columns, GC Columns, HPLC Columns

Advertisement