Removing blank spaces from text files in UNIX


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Removing blank spaces from text files in UNIX
# 1  
Old 05-04-2010
Removing blank spaces from text files in UNIX

Hello,

I am an super newbie, so forgive my sheer ignorance. I have a series of text files formatted as follows (just showing the header and first few lines):

Code:
  mean_geo mean_raw        lat        lon
     0.000       0   -70.616   163.021
     0.000       0   -70.620   163.073
     0.000       0   -70.624   163.125
     0.000       0   -70.628   163.178
     0.000       0   -70.632   163.230
     0.000       0   -70.635   163.282

I want to convert these to csvs with 4 columns (mean_geo, mean_raw, lat, and lon), so I'd like to replace all blank spaces with commas. Note that the length of the blank spaces is variable.

Is it possible to do this in Unix?

Thanks.

Last edited by radoulov; 05-04-2010 at 03:38 AM.. Reason: Please use code tags!
# 2  
Old 05-04-2010
Use nawk on Solaris:


Code:
awk '$1=$1 _' OFS=, infile

# 3  
Old 05-04-2010
Beautiful. The only problem is that while Unix window shows all the blanks being changed to commas my original file still seems unchanged when opened with a text editor. Do I need to save the output to a new file?

Quote:
Originally Posted by radoulov
Use nawk on Solaris:


Code:
awk '$1=$1 _' OFS=, infile

# 4  
Old 05-04-2010
Yes:

Code:
awk > newfile '$1=$1 _' OFS=, infile

To modify the original file in-place (not recommended):

Code:
{ rm infile && awk '$1=$1 _' OFS=, > infile;} < infile

# 5  
Old 05-04-2010
What if I am doing a whole bunch of files? Can I use wildcards for batch processing?
# 6  
Old 05-04-2010
Yes:

Code:
perl -i.orig -ple'
  s/^\s*//; s/\s+/,/g
  ' *

You'll need to adjust the glob (*).
# 7  
Old 05-04-2010
Thank you

---------- Post updated at 02:06 AM ---------- Previous update was at 02:01 AM ----------

OK, I tried modifying in place but I get the following error:

{ rm test.txt && awk '$1=$1 _' OFS=, > test.txt;} < test.txt
{: Command not found.
}: Command not found.

I'm not sure I understand the batch commands - what is a glob? Can I use perl commands in a Unix shell? I am very, very new to all this.
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Add Blank Spaces in text, to perform beter alignment of the string

Hi Guru, I need some advice on how to add blank spaces to the code, rather than me just adding <space-bar spaces> which does not work. Current output of the code File System Backed Up - ALL_LOCAL_DRIVES Daily - Incremental Backup Schedule - 1 Month Retention • 7pm - PRD... (2 Replies)
Discussion started by: Junes
2 Replies

2. Shell Programming and Scripting

Removing blank/white spaces and special characters

Hello All , 1. I am trying to do a task where I need to remove Blank spaces from my file , I am usingawk '{$1=$1}{print}' file>file1Input :- ;05/12/1990 ;31/03/2014 ; Output:- ;05/12/1990 ;31/03/2014 ;This command is not removing all spaces from... (6 Replies)
Discussion started by: himanshu sood
6 Replies

3. Emergency UNIX and Linux Support

Removing ONLY UNIQUE blank spaces with sed?

Hi, I have data that looks similar to this: In which the sentences are written horizontally and the beginning of a sentence is indicated by a 1 in the first column and the number increments until the last item of the sentence. The end of the sentence and the beginning of the next is then indicate... (1 Reply)
Discussion started by: owwow14
1 Replies

4. Solaris

Removing blank spaces

Hi , I want to go out of vi editor temporarily and execute a command in command prompt and again going back to the editor . Is it possible . Any help on this is really helpful. 1. Need to open vi 2. Temporarily come out and execute a command and go back to vi editor (6 Replies)
Discussion started by: rogerben
6 Replies

5. Shell Programming and Scripting

removing blank spaces in a script

Hi I am writing a shell to run sqlplus scripts. i found a issue in my scripts that oracle will not spool a file if there is blank spaces in the word like /backup dir/scriptrelease/1_DDL or /backupdir/script release/2_DDL can we write some thing in the shell to make "/backup dir" to... (1 Reply)
Discussion started by: srichunduru
1 Replies

6. Shell Programming and Scripting

Replace blank spaces with semicolon - text file

Hi all, Been trying to find a solution to this, I'm sure its a sed 1 liner, but I don't know sed well enough to work it out... I have a text file in the following format: 431 666 1332 2665 0.24395 432 670 ... (3 Replies)
Discussion started by: mpcengineering
3 Replies

7. Shell Programming and Scripting

Removing blank spaces, tab spaces from file

Hello All, I am trying to remove all tabspaces and all blankspaces from my file using sed & awk, but not getting proper code. Please help me out. My file is like this (<b> means one blank space, <t> means one tab space)- $ cat file NARESH<b><b><b>KUMAR<t><t>PRADHAN... (3 Replies)
Discussion started by: NARESH1302
3 Replies

8. Shell Programming and Scripting

Remove blank spaces in a text file...

Hi, I have this problem that there are blank spaces in my text file... i want to remove them line 1 line 2 line 3 I want to remove the space between line 2 and line 3... I tried sed... it work but it prints the whole text file at the command prompt which i dont want.... sde i tried was... (4 Replies)
Discussion started by: bhagya2340
4 Replies

9. Shell Programming and Scripting

Removing blank spaces from a file?

Guys, I need some help... how can I remove the blank spaces between the lines below? (between the date and the hour fields) 21/05/07 00:05:00 99 21/05/07 00:10:01 99 21/05/07 00:15:00 99 21/05/07 00:20:00 99 21/05/07 00:25:00 99 I want to make the file... (4 Replies)
Discussion started by: dfs
4 Replies

10. UNIX for Dummies Questions & Answers

To remove Continous blank spaces from a file in UNIX

All... I want to remove blank spaces in file . I just leraned that we can use " cat <Input filename> | tr -s ‘ ‘ > <Target file name> " i also know with SED we can replace a blank space by other character by sed s/ /*/g filename. Please let me know how can i do that by... (1 Reply)
Discussion started by: arunkumar_mca
1 Replies
Login or Register to Ask a Question