The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

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
Reducing Risk Versus Eliminating Risk iBot IT Security RSS 0 05-29-2008 02:50 PM
UrgentPlease: compare 1 value with file values eliminating special characters kittusri9 Shell Programming and Scripting 10 04-28-2008 04:51 AM
To split a string to obtain the words delimited by whitespaces Sudhakar333 Shell Programming and Scripting 4 08-06-2007 02:26 PM
Moving Part of a field to another field using AWK rjsha1 Shell Programming and Scripting 5 08-04-2006 05:39 AM
help me to remove the whitespaces between the lines ...urgent kumar1 Shell Programming and Scripting 5 10-10-2005 08:51 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 12-13-2004
videsh77 videsh77 is offline
Registered User
  
 

Join Date: Aug 2004
Posts: 144
Question Eliminating whitespaces during reading of a field.

I am reading a field from a file. This field have trailing whitespaces.
Does anyone knows, how to get rid of these whitespaces, at the time of reading?

I know sed command as follows which removes whitespaces on a row -
sed 's/[ \t]*$//' rows
  #2 (permalink)  
Old 12-13-2004
zazzybob's Avatar
zazzybob zazzybob is offline Forum Advisor  
Registered Geek
  
 

Join Date: Dec 2003
Location: Melbourne, Australia
Posts: 2,100
I normally use awk.

For example, say we had a tab delimtied file where fields sometimes have trailing spaces (i.e. between the data and the tab/newline), such as
Code:
field1          field2          field3
field1-a        field2-a        field3-a
Looking at this with od, we can see the spaces
Code:
$ od -ta datafile
0000000   f   i   e   l   d   1  sp  sp  ht   f   i   e   l   d   2  sp
0000020  sp  sp  ht   f   i   e   l   d   3  sp  sp  sp  nl   f   i   e
0000040   l   d   1   -   a  sp  sp  sp  ht   f   i   e   l   d   2   -
0000060   a  sp  sp  sp  sp  ht   f   i   e   l   d   3   -   a  sp  sp
0000100  sp  sp  sp  sp  sp  nl  nl
0000107
Awk will grab the tab-delimited fields and discard the spaces, i.e. to print just the first field...
Code:
awk '{print $1}' datafile
Piping the output through od, we can see that the trailing spaces are gone
Code:
$ awk '{print $1}' datafile | od -ta
0000000   f   i   e   l   d   1  nl   f   i   e   l   d   1   -   a  nl
0000020  nl
0000021
If you've got trailing tabs, then awk should take care of these too (depending on the format of the file).

If this isn't what you want - post a sample of the input file.

Cheers
ZB
  #3 (permalink)  
Old 12-13-2004
videsh77 videsh77 is offline
Registered User
  
 

Join Date: Aug 2004
Posts: 144
Unhappy Problem illustrated

Zazzybob,

The solution you have suggested removes spaces after reading the field from the file.
I want solution something like, at the time of reading this field, there
shouldnt be any trailing spaces.

Let me make my problem statement clear by an illustration-

Suppose I have a following file "tmpFile" :

'1111,aaa '
'2222,bbbb '

excluding single quotes, those I have shown in order to signify the presence of
trailing spaces at the end of the record.

Now if I run following command,
awk -F"," '{printf("%s:%s\n", $1,$2) }' tmpFile

$2 field is read as-
'aaa '
'bbbb '

Actually at the time of reading, I would like to read as -
'aaa'
'bbbb'

I dont want to execute new command sed to strip off
the trailing whitespaces, which I achieve with the following command -
awk -F"," '{printf("%s:%s\n", $1,$2) }' tmpFile | sed 's/[ \t]*$//'

Can we strip-off whitespaces at the time of reading?
  #4 (permalink)  
Old 12-13-2004
zazzybob's Avatar
zazzybob zazzybob is offline Forum Advisor  
Registered Geek
  
 

Join Date: Dec 2003
Location: Melbourne, Australia
Posts: 2,100
You could do something like this (modify it to suit your needs)
Code:
awk -F',' '{gsub(/[\t ]*$/,"",$2);print $2}' datafile
Cheers
ZB
  #5 (permalink)  
Old 12-13-2004
videsh77 videsh77 is offline
Registered User
  
 

Join Date: Aug 2004
Posts: 144
Thanx a ton zazzybob,
It helps!
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 04:18 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