Sponsored Content
Top Forums Shell Programming and Scripting how to fix the column length in a file using Awk Prog Post 302354038 by meva on Thursday 17th of September 2009 12:28:45 AM
Old 09-17-2009
how to fix the column length in a file using Awk Prog

Hi

I use the following code to read the file and to fix the length of the column of the record in the file 'Sample.txt'

Code:
ls Samp* | awk '
{ a[NR]=$1 }
END{
FS="n"
for(i=1;i<=NR;i++)
{
while( getline < a[i] ) 
{
f1=$0;
print("Line::",f1); 
f2=substr(f1,1,10) 
print("Field1::",f2);
f3=substr(f1,11,57)
print("Field2::",f3);
f5=substr(f1,58,75)
print("Field4::",f5);
f6=substr(f1,86,96) 
print("Field5::",f6);
f7=substr(f1,97,104)
print("Field6::",f7);
}
}

Contents of Sample.txt

Code:
1234567891XYZABC                            13/08/09 00:00:00 67890   12345 67890       00

but only field1 is printed correctly as below

Code:
Line::1234567891XYZABC                            13/08/09 00:00:00 67890   12345 67890       00
Field1::1234567891
Field2::XYZABC                                     13/08/09 00:00:00
Field3::13/08/09 00:00:00          67890   12345 67890       00

Please input your thougts to get the fields correctly...

Thanks for your Inputs
meva

Last edited by Franklin52; 09-17-2009 at 03:25 AM.. Reason: Please use code tags!
 

10 More Discussions You Might Find Interesting

1. 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

2. 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

3. 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

4. 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

5. 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

6. 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

7. 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

8. 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

9. 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

10. 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
COLRM(1)						    BSD General Commands Manual 						  COLRM(1)

NAME
colrm -- remove columns from a file SYNOPSIS
colrm [start [stop]] DESCRIPTION
The colrm utility removes selected columns from the lines of a file. A column is defined as a single character in a line. Input is read from the standard input. Output is written to the standard output. If only the start column is specified, columns numbered less than the start column will be written. If both start and stop columns are spec- ified, columns numbered less than the start column or greater than the stop column will be written. Column numbering starts with one, not zero. Tab characters increment the column count to the next multiple of eight. Backspace characters decrement the column count by one. ENVIRONMENT
The LANG, LC_ALL and LC_CTYPE environment variables affect the execution of colrm as described in environ(7). EXIT STATUS
The colrm utility exits 0 on success, and >0 if an error occurs. SEE ALSO
awk(1), column(1), cut(1), paste(1) HISTORY
The colrm command appeared in 3.0BSD. BSD
August 4, 2004 BSD
All times are GMT -4. The time now is 12:25 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy