The UNIX and Linux Forums  


Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Adding specific text and spaces to each line in a text file hertingm Shell Programming and Scripting 4 08-25-2008 03:34 PM
Query:just need to remove the contents of the file without deleting that namishtiwari Shell Programming and Scripting 3 02-04-2008 05:41 AM
How to read and write directory or file contents in c++ ? namrata5 High Level Programming 3 09-28-2007 04:58 PM
How can i remove spaces in between the fields in a file dsravan Shell Programming and Scripting 3 09-26-2006 05:11 PM
remove the contents of a file surjyap SUN Solaris 2 03-02-2006 04:02 PM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 11-10-2008
carriehoff carriehoff is offline
Registered User
  
 

Join Date: Jul 2006
Posts: 5
Remove spaces from first field, and write entire contents into other text file

Hi all,

I have searched and found various threads about removing spaces from a field within a text file. Unfortunately, I have not found exactly what I'm looking for, nor am I adept enough to modify what I've found into what I need.
I use the following command to remove the first line from a text file (the column headers): sed 1d file > file2
I then want to strip the leading spaces from the first field only, but so far, all I can do is the following: cat file2 | awk '{print $1}' > file3
That successfully strips the spaces, but only gives me that one field. Since the number of fields could vary from day to day (an updated field is received every night, and additional fields are often added), how can I modify that script to ensure all fields are written to the new file with the spaces stripped only from the first field?

Thank you,
Carrie
  #2 (permalink)  
Old 11-10-2008
cfajohnson's Avatar
cfajohnson cfajohnson is offline Forum Advisor  
Shell programmer, author
  
 

Join Date: Mar 2007
Location: Toronto, Canada
Posts: 2,361


Code:
awk 'NR > 1 { gsub( /^ */, "" ); print }' file > file2

  #3 (permalink)  
Old 11-10-2008
Ikon's Avatar
Ikon Ikon is offline Forum Advisor  
Registered User
  
 

Join Date: Jul 2008
Location: Phoenix, Arizona
Posts: 669
This will strip all spaces from front of file and output to newfile:

Code:
sed 's/^ *//' /path/to/file > /path/to/newfile

  #4 (permalink)  
Old 11-11-2008
carriehoff carriehoff is offline
Registered User
  
 

Join Date: Jul 2006
Posts: 5
Excellent. Can I ask for one step further? Can you correct my syntax for a one-liner that will remove the leading spaces from every field? I have some db fields that specify for char of 9, but then they have only 6 or 7 characters within the field, so it fills in the extra spots with spaces. There are a few fields that I've had to script around to deal with this.

For the sed 's/^ *//' /path/to/file > /path/to/newfile, It looks as though this: /^ */ specifies the beginning of the line, and the 's means space, so I understand that it is stripping the spaces from the beginning of the line. The next problem I found is that a field further down in the list of fields may also need the leading spaces stripped. Problem is, the field number may change as more fields are added/removed, so I don't know what the field number will be on any given day - better to just strip 'em all.

Is there a way using the sed command to strip all leading spaces from all fields?

Thanks,
Carrie
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 11:42 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0