![]() |
|
|
google unix.com
|
|||||||
| Forums | Register | Forum Rules | Links | Albums | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
| 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 |
![]() |
|
|
LinkBack | Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
||||
|
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 |
|
||||
|
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 |
![]() |
| Bookmarks |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|