need help to remove spaces from first column


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting need help to remove spaces from first column
# 1  
Old 01-26-2006
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  
Old 01-26-2006
Awk:
Code:
{ col1 = substr($0,1,27)
  gsub(  / /, "", col1 )
  print col1,
        substr("                           ", 1, 25-length(col1)),
        substr($0,28)
}

# 3  
Old 01-26-2006
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  
Old 01-26-2006
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  
Old 01-28-2006
Code worked pefectly for me.
@futurelet Thanks for the code & its explanation.

Regards,
Tayyab
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to awk or grep the last column in file when date on column contains spaces?

Hi have a large spreadsheet which has 4 columns APM00111803814 server_2 96085 Corp IT Desktop and Apps APM00111803814 server_2 96085 Corp IT Desktop and Apps APM00111803814 server_2 96034 Storage Mgmt Team APM00111803814 server_2 96152 GWP... (6 Replies)
Discussion started by: kieranfoley
6 Replies

2. Shell Programming and Scripting

Remove the values from a certain column without deleting the Column name in a .CSV file

(14 Replies)
Discussion started by: dhruuv369
14 Replies

3. Shell Programming and Scripting

Remove the first character from the fourth column only if the column has four characters

I have a file as follows ATOM 5181 N AMET K 406 12.440 6.552 25.691 0.50 7.37 N ATOM 5182 CA AMET K 406 13.685 5.798 25.578 0.50 5.87 C ATOM 5183 C AMET K 406 14.045 5.179 26.909 0.50 5.07 C ATOM 5184 O MET K... (14 Replies)
Discussion started by: hasanabdulla
14 Replies

4. Shell Programming and Scripting

remove brackets and put it in a column and remove repeated entry

Hi all, I want to remove the remove bracket sign ( ) and put in the separate column I also want to remove the repeated entry like in first row in below input (PA156) is repeated ESR1 (PA156) leflunomide (PA450192) (PA156) leflunomide (PA450192) CHST3 (PA26503) docetaxel... (2 Replies)
Discussion started by: manigrover
2 Replies

5. Shell Programming and Scripting

remove spaces

Hi folks, I need to remove spaces at the end of each line in a *.txt file. it looks like this word 1 word 2 . . . word n i found some sed commands but any of them didnt work so far thank you for your posts (6 Replies)
Discussion started by: Jimmy7
6 Replies

6. Shell Programming and Scripting

Remove spaces in filenames

Hi, I have files like below, In files coming as spaces. Before transfering those files into ftp server. I want to remove the spaces and then can transfer the files into unix server. e.g: filenames are 1) SHmail _profile001_20120908.txt 2) SHmail_profile001 _20120908.txt 3) sh... (3 Replies)
Discussion started by: kirankumar
3 Replies

7. Shell Programming and Scripting

need to remove duplicates based on key in first column and pattern in last column

Given a file such as this I need to remove the duplicates. 00060011 PAUL BOWSTEIN ad_waq3_921_20100826_010517.txt 00060011 PAUL BOWSTEIN ad_waq3_921_20100827_010528.txt 0624-01 RUT CORPORATION ad_sade3_10_20100827_010528.txt 0624-01 RUT CORPORATION ... (13 Replies)
Discussion started by: script_op2a
13 Replies

8. Shell Programming and Scripting

Remove Spaces

Hi All, I have a comma seperated file. I wanna remove the spaces from column 2. I mean i don't wanna remove the spaces those are presnt in column 1. ex: emp name, emp no, salary abc k, abc 11, 1000 00 bhk s, bhk 22, 2000 00 the output should be: emp name, emp no, salary abc k, abc11,... (4 Replies)
Discussion started by: javeed7
4 Replies

9. UNIX for Dummies Questions & Answers

How to remove trailing spaces

Hi, I have a file like this (ADD_MONTHS((Substr(Trim(BOTH FROM Translate(Maximum(closeDa ------------------------------------------------------------ 2007-06-30 00:00:00 I have a requirement where i need just the date. When i do: tail -1... (2 Replies)
Discussion started by: mahek_bedi
2 Replies
Login or Register to Ask a Question