awk script to remove spaces - examples don't show up correctly


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk script to remove spaces - examples don't show up correctly
# 8  
Old 12-22-2008
Another issue is that I need to maintain the white spaces in the last several columns in or do account for place holders for no data.
# 9  
Old 12-22-2008
Question Does the data currently line up?

Your first post seems to show the data already lining up in columns.
Also, you might review the substr($0,10,2) awk command; which would take the input line and output 2 characters beginning at position #10.

Can you provide a sample of the input file (with code tags) if you current input file does not line up.
# 10  
Old 12-22-2008
a more clear explanation

I have a file of about 60,000 lines of output. Each line has multiple fields, some blank. I have an example of what it looks like the way it needs to look by manually lining things up in vi and awk. Here is what is needs to looks like
Code:
0 0000                              SEE TEXT                                               1.00000 EA         M  No
1 SEE-TEXT                          INSURE CORRECT PCBA AND FIRMWARE.                      1.00000 EA  U J M  M  No
0 0032RS01                          LABEL,DECAL,BOX,900-0032-00                            1.00000 EA         P  No
0 0910-BC                           SMK,0910,BULK,C/S,UL                     A             1.00000 EA         M  No
0 1475-2104                         BATTERY DOOR                             02  2715      1.00000 EA         M  No
0 21002381                          CO,900-0076,C/S,12PK,PEZ,UL              A             1.00000 EA         P  No
1 21007527                          LID,DET,C/S,BULK,PEZ,7 1/2x18 1/2x X     A             1.00000 EA  U J M  M  No
1 21007528                          TRAY,DET,C/S,BULK,PEZ,7 1/2x18 1/2x X    A             1.00000 EA  U J M  M  No
1 820-1625                          LABEL,INFO,BULK C/S,900-0076             A             1.00000 EA  U J M  M  No
2 810-2399                          LABEL,DIESTRIKE,6X4                      A             1.00000 EA  U J M  M  No
1 900-0076-003                      CO,900-0076,C/S,UL,SMT,EN                A            12.00000 EA  U J M  M  No
2 800-0056                          ASSY,CO,SUB,MOUNTING_KIT,(SCREW/ANCHOR)  C   1552      2.00000 EA  U P M  P  No
3 810-1292                          BAG,POLY,CLR,2 X 3                       A   2748      1.00000 EA  L I O  M  No
3 810-0009                          SCREW,PHILLIPS,#8x3/4",TYPE_A,PANHEAD    C    679      1.00000 EA  U P M  P  No
3 810-0010                          ANCHOR,PLASTIC,#8-1"_LG                  C    680      1.00000 EA  U P M  P  No
2 810-0629                          INK,COOL_GRA,TYPE_P-NT,GLOSSY(PMS_10)    0             0.00001 OZ  U P M  P  No
2 810-0689                          LABEL,DECAL,NO_BATTERY,ORANGE            C   2669      1.00000 EA  U P M  P  No
2 810-0939                          LABEL,ATWD,DLX,UL,4.1x.57,"DO NOT PAINT" O             1.00000 EA  U J M  M  No
2 810-1025                          LABEL,WARNING,WALL MOUNT,w\ADH,ENGL      F   5443      2.00000 EA  U J M  M  No
2 810-1174                          BAG,ACTIVATED CARBON,TYVEK,5 GRAM,1.5x3  C1  4436      1.00000 EA  U J M  M  No
2 810-1270                          LABEL,SECURITY LABEL,1.78"x.42",WHT      B   3886      1.00000 EA  U J M  M  No


Here is an example of the data without alteration:

Code:
0 21006658                                 ASY,SMK,10-YR,UL,CS,4 PK                 A             1.00000 EA         M  No
1 900-0136                                ASY,SMK,10-YR,UL                         C   4730      4.00000 EA  U J M  M  No
2 800-0448                               ASY,SUB,PCBA,SMK,DC,10-YR                A   4998      1.00000 EA  U J M  M  No
3 810-1734                              BATTERY,CR2/3,8L-FIST,35                 B   3754      3.00000 EA  U J M  M  No
3 6001-1009                             CAP.,0.001uf                                           3.00000 EA  U J M  M  No
3 6701-1007                             CAP,MC,0.1UF,50V,10%,2.5MM               A             1.00000 EA  U J M  M  No
3 6103-1006                             CAP,MC,1.0UF,50V,20%,DXL:4X7             A             1.00000 EA  U J M  M  No
3 5925-2001                             DIODE,1N4001                                           3.00000 EA  U J M  M  No
3 5910-1001                             DIODE 1N4148 AXIAL                       A             1.00000 EA  U J M  M  No
3 5980-0011                             LED,RED,5MM L-15MM & 10MM                A             1.00000 EA  U J M  M  No
3 5051-5100                             RES,5.1_OHM                                            3.00000 EA  L I O  M  No
3 5052-1801                             RES.,18_OHM                                            2.00000 EA  U J M  M  No
3 5111-0018                             RES.,100K-22M,1/4W                                     2.00000 EA  U J M  M  No
3 5051-2205                             RES.,220K,CF,1/4W,5%,AXIAL               A             1.00000 EA  U J M  M  No
3 5051-1506                             RES.,1.5M                                              2.00000 EA  U J M  M  No
3 5111-1475                             RES,147K,1%                                            1.00000 EA  L I O  M  No
3 5051-1306                             RES.,1.3M                                              1.00000 EA  U J M  M  No
3 5051-0006                             RES.,1.5-10M,1/4W                                      1.00000 EA  U J M  M  No
3 5051-4702                             RES.,470_ohm                                           1.00000 EA  U J M  M  No
3 5051-2207                             RES.,22M,1/4W                                          1.00000 EA  U J M  M  No


The first column is the "level" indicator. 0 = 7 space indentation 1 = 6 space indentation.......

My goal is to align the columns so that I can then import into a spreadsheet and define the columns based on the set widths. If one cell is empty, it needs to have a white space in the column to ensure the remaining cells fill correctly.

What I have been trying to do is to set a variable based on the first column in an awk script and then try and delete the white spaces between the 2nd and 3rd column. Let me know if this is clear. I am working on the sub() and now the substr() functions.

Thanks for your help,

chris
# 11  
Old 12-22-2008
Your are right, it can be so simple...

Code:
sed '/^3/s/^\(.\{15\}\)\s/\1/;/^2/s/^\(.\{15\}\)\s\s/\1/' file

For me this works perfectly.
# 12  
Old 12-22-2008
And with a little sed file now covering the new special cases of
your latest example:

command:
Code:
sed -f sedfile file

sedfile:
Code:
/^3/s/^\(.\{15\}\)\s/\1/
/^2/s/^\(.\{15\}\)\s\s/\1/ 
/^1/s/^\(.\{15\}\)\s\s\s/\1/ 
/^0/s/^\(.\{15\}\)\s\s\s\s/\1/

This should be easily adaptable to your needs.

HTH Chris
# 13  
Old 12-22-2008
Hammer & Screwdriver Here is a solution ising awk

Code:
> cat file125
0 21006658                                 ASY,SMK,10-YR,UL,CS,4 PK                 A             1.00000 EA         M  No
1 900-0136                                ASY,SMK,10-YR,UL                         C   4730      4.00000 EA  U J M  M  No
2 800-0448                               ASY,SUB,PCBA,SMK,DC,10-YR                A   4998      1.00000 EA  U J M  M  No
3 810-1734                              BATTERY,CR2/3,8L-FIST,35                 B   3754      3.00000 EA  U J M  M  No
3 6001-1009                             CAP.,0.001uf                                           3.00000 EA  U J M  M  No
3 6701-1007                             CAP,MC,0.1UF,50V,10%,2.5MM               A             1.00000 EA  U J M  M  No
3 6103-1006                             CAP,MC,1.0UF,50V,20%,DXL:4X7             A             1.00000 EA  U J M  M  No
3 5925-2001                             DIODE,1N4001                                           3.00000 EA  U J M  M  No
3 5910-1001                             DIODE 1N4148 AXIAL                       A             1.00000 EA  U J M  M  No
3 5980-0011                             LED,RED,5MM L-15MM & 10MM                A             1.00000 EA  U J M  M  No
3 5051-5100                             RES,5.1_OHM                                            3.00000 EA  L I O  M  No
3 5052-1801                             RES.,18_OHM                                            2.00000 EA  U J M  M  No
3 5111-0018                             RES.,100K-22M,1/4W                                     2.00000 EA  U J M  M  No
3 5051-2205                             RES.,220K,CF,1/4W,5%,AXIAL               A             1.00000 EA  U J M  M  No
3 5051-1506                             RES.,1.5M                                              2.00000 EA  U J M  M  No
3 5111-1475                             RES,147K,1%                                            1.00000 EA  L I O  M  No
3 5051-1306                             RES.,1.3M                                              1.00000 EA  U J M  M  No
3 5051-0006                             RES.,1.5-10M,1/4W                                      1.00000 EA  U J M  M  No
3 5051-4702                             RES.,470_ohm                                           1.00000 EA  U J M  M  No
3 5051-2207                             RES.,22M,1/4W                                          1.00000 EA  U J M  M  No
> space="~~~~~~~"
> awk -v space=$space '{print substr($0,1,37)substr(space,1,substr($0,1,1))substr($0,41)}' file125 | tr "~" " "
0 21006658                              ASY,SMK,10-YR,UL,CS,4 PK                 A             1.00000 EA         M  No
1 900-0136                              ASY,SMK,10-YR,UL                         C   4730      4.00000 EA  U J M  M  No
2 800-0448                              ASY,SUB,PCBA,SMK,DC,10-YR                A   4998      1.00000 EA  U J M  M  No
3 810-1734                              BATTERY,CR2/3,8L-FIST,35                 B   3754      3.00000 EA  U J M  M  No
3 6001-1009                             CAP.,0.001uf                                           3.00000 EA  U J M  M  No
3 6701-1007                             CAP,MC,0.1UF,50V,10%,2.5MM               A             1.00000 EA  U J M  M  No
3 6103-1006                             CAP,MC,1.0UF,50V,20%,DXL:4X7             A             1.00000 EA  U J M  M  No
3 5925-2001                             DIODE,1N4001                                           3.00000 EA  U J M  M  No
3 5910-1001                             DIODE 1N4148 AXIAL                       A             1.00000 EA  U J M  M  No
3 5980-0011                             LED,RED,5MM L-15MM & 10MM                A             1.00000 EA  U J M  M  No
3 5051-5100                             RES,5.1_OHM                                            3.00000 EA  L I O  M  No
3 5052-1801                             RES.,18_OHM                                            2.00000 EA  U J M  M  No
3 5111-0018                             RES.,100K-22M,1/4W                                     2.00000 EA  U J M  M  No
3 5051-2205                             RES.,220K,CF,1/4W,5%,AXIAL               A             1.00000 EA  U J M  M  No
3 5051-1506                             RES.,1.5M                                              2.00000 EA  U J M  M  No
3 5111-1475                             RES,147K,1%                                            1.00000 EA  L I O  M  No
3 5051-1306                             RES.,1.3M                                              1.00000 EA  U J M  M  No
3 5051-0006                             RES.,1.5-10M,1/4W                                      1.00000 EA  U J M  M  No
3 5051-4702                             RES.,470_ohm                                           1.00000 EA  U J M  M  No
3 5051-2207                             RES.,22M,1/4W >


Last edited by joeyg; 12-22-2008 at 05:33 PM.. Reason: changed one of the substr values
# 14  
Old 12-22-2008
Or a solution using perl:

Code:
perl -wlape '$c=4-$F[0];s/^(.{13})\s{$c}/\1/' file

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Script to remove spaces and mv a file

I've tried various solutions to move a file name with spaces and nothing seems to work. I need to take a date as input, prepend it to a filename with spaces then remove the spaces and mv the file to the new name. #!/bin/ksh # if (( $# != 1 )) then echo "Usage: `basename $0` <DATE> " ... (5 Replies)
Discussion started by: w_s_s
5 Replies

2. Shell Programming and Scripting

Why does this awk script not work correctly?

I have a large database with English on the left hand side and Indic words on the left hand. It so happens that since the Indic words have been entered by hand, there are duplicates in the entries. The structure is as under: English headword=Indic gloss,Indic gloss A small sample will... (6 Replies)
Discussion started by: gimley
6 Replies

3. AIX

Showmount don't show NFS4

Configure nfs4 on aix ok i did all chnfsdom #return correct domain chnfs -r /export chnfs -p /export configured and shared pseudo root #etc/exports /export -nfsroot /var/pubblica -exname=/export/pubblica,vers=4,sec=sys:krb5p:krb5i:krb5:dh,rw exportfs -a mount correct mount... (1 Reply)
Discussion started by: Linusolaradm1
1 Replies

4. Shell Programming and Scripting

Remove spaces / tabs from variable in script

I want to remove extra spaces from variable in aix script. We retrieve the data from oracle database and then print the values. We have a value on 90th position. When we execute the query on sqlplus it shows the length of 90th position as 3, but when we use the same query in aix script it shows... (5 Replies)
Discussion started by: lodhi1978
5 Replies

5. Shell Programming and Scripting

AWK: Remove spaces before processing each line?

Hi, all I have a file containing the following data: name: PRODUCT_1 date: 2010-01-07 really_long_name: PRODUCT_ABCDEFG I want to get the date (it is "2010-01-07" here), I could use the following code to do that: awk... (6 Replies)
Discussion started by: kevintse
6 Replies

6. Shell Programming and Scripting

How to remove spaces using awk,sed,perl?

Input: 3456 565 656 878 235 8 4 8787 3 7 35 878 Expected output: 3456 565 656 878 235 8 4 8787 3 7 35 878 How can i do this with awk,sed and perl? (10 Replies)
Discussion started by: cola
10 Replies

7. Shell Programming and Scripting

how to remove blank spaces of a file with awk??

hello how to remove blank spaces of a file with awk?? i´m trying awk '{gsub(" ","",$0); print $0;}' filename.txt but it answers syntax error near line first of all i did this for download from netbackup database jobs privilege bpdbjobs -report -M sv88 -gdm -header |cut -c-1024... (4 Replies)
Discussion started by: pabloli150
4 Replies

8. Shell Programming and Scripting

Don't show keyboard input on terminal

I am developing a script that will run with '/bin/ksh' shell. The script is intended to receive a password by keyboard input, but for security reasons I would like to hide what the user is typing. The keyboard input is being caught by 'read' command. exmaple : echo "Please type your new... (1 Reply)
Discussion started by: marianor31
1 Replies

9. Shell Programming and Scripting

Variable with $ do not show correctly

Hey guys i need help i have a script to push a password to a remote server the only problem is the $ENCRYPT variable has $'s in it (from the encrypted password in the shadow file) and they drop out when apending to the shadow file via the usermod command so $1$Q/6a08n$EoAcBuR/YnoCQC shows up as... (3 Replies)
Discussion started by: insania
3 Replies

10. Linux

Why don't my clock show am or pm?

Is it not possible to get the "Digital" clock in KDE 3.3 to show am or pm? Well I just noticed the plain clock setting is the only one that shows it. (2 Replies)
Discussion started by: CTroxtell21
2 Replies
Login or Register to Ask a Question