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
remove spaces btw tjmannonline UNIX for Dummies Questions & Answers 7 05-26-2008 02:35 AM
Remove spaces from columns jacks Shell Programming and Scripting 2 01-18-2008 10:08 AM
How to remove trailing spaces mahek_bedi UNIX for Dummies Questions & Answers 2 08-10-2007 07:21 AM
how to remove spaces in a string using sed. radhika Shell Programming and Scripting 4 06-02-2005 03:00 PM
removing trailing spaces of a particular column in a file rooh UNIX for Dummies Questions & Answers 2 01-12-2002 11:34 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 01-26-2006
tayyabq8's Avatar
tayyabq8 tayyabq8 is offline Forum Advisor  
Moderator
  
 

Join Date: Nov 2004
Location: Bahrain
Posts: 578
need help to remove spaces from first column

Hi,

Here is sample data which I have:

Code:
column#1                   column#2          column#3

001A 50005                 ROCKER ADJ        00010000100018UTIRR                  
001A 50020                 CRANKSHAFT        0003445ES             
001A 52201                 SPARKPLUG         0000010000100016UTIRR
001ALD626000  GAR          PINTURA BI        21000000271100011
I want to remove all white spaces from first column, but thing which is confusing me that how we'll define the width of first column, since number and location of spaces are arbitrary in first column. after editing my output should look like this:

Code:
column#1                   column#2          column#3

001A50005                 ROCKER ADJ        00010000100018UTIRR 
001A50020                 CRANKSHAFT        0003445ES
001A52201                 SPARKPLUG         0000010000100016UTIRR
001ALD626000GAR           PINTURA BI        21000000271100011
any idea about how we can acheive this?

Regards,
Tayyab
  #2 (permalink)  
Old 01-26-2006
futurelet futurelet is offline
Registered User
  
 

Join Date: Jul 2005
Posts: 137
Awk:
Code:
{ col1 = substr($0,1,27)
  gsub(  / /, "", col1 )
  print col1,
        substr("                           ", 1, 25-length(col1)),
        substr($0,28)
}
  #3 (permalink)  
Old 01-26-2006
tayyabq8's Avatar
tayyabq8 tayyabq8 is offline Forum Advisor  
Moderator
  
 

Join Date: Nov 2004
Location: Bahrain
Posts: 578
Quote:
Originally Posted by futurelet
Awk:
Code:
{ col1 = substr($0,1,27)
  gsub(  / /, "", col1 )
  print col1,
        substr("                           ", 1, 25-length(col1)),
        substr($0,28)
}
Thanks for your reply.
I'll check it when i'll be in office. Meanwhile would you pls. explain the code to me?

Regards,
Tayyab
  #4 (permalink)  
Old 01-26-2006
futurelet futurelet is offline
Registered User
  
 

Join Date: Jul 2005
Posts: 137
The block within { ... } is executed for every line of the file.
substr($0,1,27) extracts a substring of $0, the line just read, starting at character number 1 and continuing for 27 characters.
gsub( / /, "", col1 ) globally substitutes for each space in col1 the empty string; i.e., it removes all spaces.
When the resulting line is printed, we have to make sure that the data we changed is still 27 columns wide. Since each comma in the print statement provides 1 space, we subtract the size of col1 from 25 instead of from 27.
  #5 (permalink)  
Old 01-28-2006
tayyabq8's Avatar
tayyabq8 tayyabq8 is offline Forum Advisor  
Moderator
  
 

Join Date: Nov 2004
Location: Bahrain
Posts: 578
Code worked pefectly for me.
@futurelet Thanks for the code & its explanation.

Regards,
Tayyab
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 01:31 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