how to fix the column length in a file using Awk Prog


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting how to fix the column length in a file using Awk Prog
# 8  
Old 09-17-2009
Code:
awk '{
        l=$0
        a=int($1)
        sub(/[0-9]+/,"",$1)
            print "Line::"   l
            print "Field1::" a
            print "Field2::" $1
            print "Field3::" $2 FS $3
            print "Field4::" $4
            print "Field5::" $5
            print "Field6::" $6
            print "Field7::" $7
    }
    ' file
Line::1234567891XYZABC 13/08/09 00:00:00 67890 12345 67890 00
Field1::1234567891
Field2::XYZABC
Field3::13/08/09 00:00:00
Field4::67890
Field5::12345
Field6::67890
Field7::00


Post 1111 Smilie
# 9  
Old 09-17-2009
Hi danmero

Thanks a lot for your response and worked for us files with a delimiter as space. But in another case we have to use the same logic to read a fixed length file wherein in the above case

e.g.
Quote:
1234567891XYZABC 13/08/09 00:00:00 67890 12345 67890 00
the field length is fixed such as
Field 1 is 5 as length
Field 2 is 5 as length
Field 3 is 10 as length and so on

Quote:
so the expected output is
Field 1 : 12345
Field 2 : 67891
Field 3 : XYZABC
i.e. the file should not be read with a delimiter and instead should read as per the length we specify as mentioned.

Thanks
Meva
# 10  
Old 09-17-2009
Quote:
Originally Posted by meva
Thanks a lot for your response and worked for us files with a delimiter as space.
Be welcome Smilie

Quote:
Originally Posted by meva
But in another case we have to use the same logic to read a fixed length file wherein in the above case
That's another case Smilie where you should use substr() function, something like.
Code:
# awk '{
            print "Line::"   $0
            print "Field1::" substr($0,1,5)
            print "Field2::" substr($0,6,5)
            print "Field3::" substr($0,11,8)
            print "Field4::" "etc...... "
    }
    ' file

Good Luck! Smilie
# 11  
Old 09-19-2009
Hi Danmero,

Thanks!!! It worked...
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

awk file subtracted by a fix value - conditioned

Hi all... i have been trying to make this work but I have been failing for 6 hours .. I know it should be something simple that I am missing to it would be great if you can help me ... I want to subtract a fixed value (lets set 1) from any value >=1 from the whole file my file looks like ... (4 Replies)
Discussion started by: A-V
4 Replies

2. Shell Programming and Scripting

Convert comma separated file to fix length

Hi, I am converting a comma separated file to fixed field lenght and I am using that: COLUMNS="25 24 67 26 39 63 20 34 35 14 397" ( cat $indir/input_file.dat | \ $AWK -v columns="$COLUMNS" ' BEGIN { FS=","; OFS=""; split(columns, arr, " "); } { for(i=1; i<=NF;... (5 Replies)
Discussion started by: apenkov
5 Replies

3. Shell Programming and Scripting

file column length using awk

1|BANG|KINR|3456 2|BANG2222|KINR|347 3|BANG|KINR|347 4|BANG|KINR|347 5|BANG|KINR|347 6|BANG|KINR|347 awk -F"|" ' length($2)>=4||length($4)>=4 {print $0 >"above.txt";next}' test1.txt i want required output,because if the 2nd column more than 4 character or 4th column more than 4... (5 Replies)
Discussion started by: bmk
5 Replies

4. Shell Programming and Scripting

Need to extract data from Column having variable length column

Hi , I need to extract data from below mentioned data, having no delimiter and havin no fixed column length. For example: Member nbr Ref no date 10000 1000 10202012 200000 2000 11202012 Output: to update DB with memeber nbr on basis of ref no. ... (6 Replies)
Discussion started by: ns64110
6 Replies

5. Shell Programming and Scripting

Fix CSV file with column missing quotes

I have a CSV file that is missing quotes around a column that contains text with commas. Example: Column1, Column2, Column3, Column4, Column5, Column6 Data1, Data2, Data3, Data, 4, Data5, Data6 Data1, Data3, Data3, Data, text, 4, Data5, Data6 I think the easiest way for me to fix this is to... (2 Replies)
Discussion started by: EmptyH
2 Replies

6. Shell Programming and Scripting

Finding multiple column values and match in a fixed length file

Hi, I have a fixed length file where I need to verify the values of 3 different fields, where each field will have a different value. How can I do that in a single step. (6 Replies)
Discussion started by: naveen_sangam
6 Replies

7. Shell Programming and Scripting

how to validate a field when it is blank using awk prog

Hi, I tried the below piece of code for my script to check whether it has a blank space for a particular field. if(f10==/]/){ print "Field 10 is Correct";} else{ print "Field 10 is Wrong"; } Please help me to know whether the "if" condition applied here is correct or do i... (14 Replies)
Discussion started by: meva
14 Replies

8. Shell Programming and Scripting

AWK record length fix

Hi Friends, Need some help in AWK. Working on AIX 5 Have been trying the following functionality to make the record length fixed: if( length(record) < 300 ) { printf("%-300s\n", record); } In my opinion it will apply some fillers in the end. Its is not making any... (4 Replies)
Discussion started by: kanu_pathak
4 Replies

9. Shell Programming and Scripting

print a file with one column having fixed character length

Hi guys, I have tried to find a solution for this problem but couln't. If anyone of you have an Idea do help me. INPUT_FILE with three columns shown to be separated by - sign A5BNK723NVI - 1 - 294 A7QZM0VIT - 251 - 537 A7NU3411V - 245 - 527 I want an output file in which First column... (2 Replies)
Discussion started by: smriti_shridhar
2 Replies

10. Shell Programming and Scripting

Comparing column of variable length anf fixed width file

Hi, I have two input files. File1: ID Name Place 1-234~name1~Newyork 1-34~name2~Boston 1-2345~name3~Hungary File1 is a variable length file where each column is seperated by delimitter "~". File2: ID Country 1-34<<11 SPACES>>USA<<7 spaces>> 1-234<<10 SPACES>>UK<<8... (5 Replies)
Discussion started by: manneni prakash
5 Replies
Login or Register to Ask a Question