LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 01-06-2014, 05:34 PM   #1
ceantuco
Member
 
Registered: Mar 2008
Location: New Jersey
Distribution: CentOS, Debian, Kali
Posts: 579

Rep: Reputation: 37
Question Remove spaces from a text file


Hi Guys,

I have a file that contains data which some of the data has extra spaces. How can I remove those extra spaces? Please see example below:

First line is the correct format.
Second line is bad.

Code:
 425 4690 417901205WC/WHT         2 100026511781002    49.97

 425 4690 417901205WC/WHT         2 100026511781002        49.97
can you help? I need to remove the extra spaces from between the UPC and the price.

Thank you
 
Old 01-06-2014, 05:47 PM   #2
smallpond
Senior Member
 
Registered: Feb 2011
Location: Massachusetts, USA
Distribution: CentOS 6 (pre-systemd)
Posts: 2,609

Rep: Reputation: 702Reputation: 702Reputation: 702Reputation: 702Reputation: 702Reputation: 702Reputation: 702
Do all rows of the file contain all of the fields? No "missing" data?

If so, simplest is use awk. It will take care of reading each line and splitting it into fields. Then you just need one instruction to print the field formatted the way you want. Something like this:

Code:
awk '{printf "%4d %4d %15s %8d %15s %8.2f\n",$1,$2,$3,$4,$5,$6}' oldfile >newfile
 
2 members found this post helpful.
Old 01-06-2014, 05:47 PM   #3
smallpond
Senior Member
 
Registered: Feb 2011
Location: Massachusetts, USA
Distribution: CentOS 6 (pre-systemd)
Posts: 2,609

Rep: Reputation: 702Reputation: 702Reputation: 702Reputation: 702Reputation: 702Reputation: 702Reputation: 702
Do all rows of the file contain all of the fields? No "missing" data?

If so, simplest is use awk. It will take care of reading each line and splitting it into fields. Then you just need one instruction to print the field formatted the way you want. Something like this:

Code:
awk '{printf "%4d %4d %15s %8d %15s %8.2f\n",$1,$2,$3,$4,$5,$6}' oldfile >newfile
 
2 members found this post helpful.
Old 01-06-2014, 06:37 PM   #4
ceantuco
Member
 
Registered: Mar 2008
Location: New Jersey
Distribution: CentOS, Debian, Kali
Posts: 579

Original Poster
Rep: Reputation: 37
There is no data missing. All rows contain data. I will give it a try.

Thanks
 
Old 01-06-2014, 06:49 PM   #5
ceantuco
Member
 
Registered: Mar 2008
Location: New Jersey
Distribution: CentOS, Debian, Kali
Posts: 579

Original Poster
Rep: Reputation: 37
Cool

Ok, so it works; however it brought the price column an extra space to the left. (This is probably because I did not specify that the original file has a space before the data line)

Line 1: what the file should look like
Line 2: the new file created.

Code:
 425 4690 417901205WC/WHT         2 100026511781002    49.97
 425 4690 417901205WC/WHT        2 100026511781002    49.97
I need to add a space before '2'.

Please help.

Thank you!
 
Old 01-06-2014, 09:17 PM   #6
jms89
Member
 
Registered: Jan 2013
Posts: 41

Rep: Reputation: Disabled
Read about about the syntax of printf. Do you understand what the statement

Code:
{printf "%4d %4d %15s %8d %15s %8.2f\n",$1,$2,$3,$4,$5,$6}
means? If you want to add an extra space, then you should change %8d to %9d.
 
Old 01-07-2014, 12:59 AM   #7
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,252

Rep: Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684
If it is simple alignment and the field data stays consistent you could simply do:
Code:
awk '$1=$1' file
 
Old 01-07-2014, 08:53 AM   #8
ceantuco
Member
 
Registered: Mar 2008
Location: New Jersey
Distribution: CentOS, Debian, Kali
Posts: 579

Original Poster
Rep: Reputation: 37
The field data varies. for example, item description might be 10 characters or more.
 
Old 01-07-2014, 09:57 AM   #9
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,252

Rep: Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684
Then you could try:
Code:
column -t file
 
1 members found this post helpful.
Old 01-07-2014, 03:22 PM   #10
ceantuco
Member
 
Registered: Mar 2008
Location: New Jersey
Distribution: CentOS, Debian, Kali
Posts: 579

Original Poster
Rep: Reputation: 37
Thank you guys. I tried editing the file but some data got shifted to the left. Anyhow, I had to contact a developer to modify the program that creates the file so it would create it with the right margins.

Thank you!
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Delete spaces from text file ceantuco Linux - Newbie 6 12-13-2013 03:53 PM
How to remove spaces from many file names under Cygwin gwc01 Linux - Newbie 4 11-22-2010 07:01 AM
Remove lines in a text file based on another text file asiandude Programming 10 01-29-2009 11:59 AM
remove \r and \n from a text file powah Programming 9 10-02-2006 07:02 AM
Need to remove spaces from all file / dir names ?? Solution here! jsjohnst Linux - General 2 07-20-2004 10:39 PM


All times are GMT -5. The time now is 01:05 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration