file column length using awk


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting file column length using awk
# 1  
Old 08-17-2012
file column length using awk

Code:
1|BANG|KINR|3456
2|BANG2222|KINR|347
3|BANG|KINR|347
4|BANG|KINR|347
5|BANG|KINR|347
6|BANG|KINR|347

Code:
 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 character.
OUTPUT:
Code:
 1|BANG|KINR|3456
2|BANG2222|KINR|347

Please help me. it's not working above awk command.

Thanks
# 2  
Old 08-17-2012
Code:
awk -F'|' '{if(length($4) > 3 || length($2) > 4) print $0}' infile

# 3  
Old 08-17-2012
Thanks lot it's working fine... one more how to add the filename as parameter...

Code:
awk -F'|' '{if(length($4) > 3 || length($2) > 4 ) print $0  >"filename.txt";next}' test1.txt

filename.txt should be parameter
# 4  
Old 08-17-2012
Code:
awk -F'|' '(length($4)>3) || (length($2)>4) {print $0  > output}' output="outputfile.txt" test1.txt

# 5  
Old 08-18-2012
bmk,
You may have already noticed this, but to satisfy your requirement, "or 4th column more than 4 character",
the "3" in the below line:

Code:
if(length($4) > 3

should be replaced with "4" in which case line 1 of your input file will not be outputed:

Code:
if(length($4) > 4

# 6  
Old 08-18-2012
@neutronscott....Thanks lot it's working what i excepted...
and also thanks @mif
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Awk: Matching Pattern From other file with length

Hi, I have input file whose first column needs(match.txt) to be matched with the first column of the input file with min & max length as defined in match.txt. But conditions are not matching. Please help on the changes in the code below as for multiple enteries in match.txt complete match.txt will... (3 Replies)
Discussion started by: siramitsharma
3 Replies

2. Shell Programming and Scripting

Splitting fixed length file using awk

Hi, I need to split a fixed length file of 160 characters based on value of a column. Example: ABC 456780001 DGDG SDFSF BCD 444440002 SSSS TTTTT ABC 777750003 HHHH UUUUU THH 888880001 FFFF LLLLLL HHH 999990002 GGGG OOOOO I need to split this file on basis of column from... (7 Replies)
Discussion started by: Neelkanth
7 Replies

3. Shell Programming and Scripting

Awk: Need help replacing a specific column in a file by part of a column in another file

Hi, I have two input files as File1 : ABC:client1:project1 XYZ:client2-aa:project2 DEF:client4:proj File2 : client1:W-170:xx client2-aa:WT-04:yy client4:L-005A:zz Also, array of valid values can be hardcoded like Output : ABC:W:project1 XYZ:WT:project2 (1 Reply)
Discussion started by: aa2601
1 Replies

4. Shell Programming and Scripting

awk get length of a binary file.

Hi.. Just got a problem with awk and cannot figure out whats the issue. I am not really expert in awk so looking for help/opinion from experts Command is $ cat abinaryfile | sed -e "s/@@//g;s/@$//" | awk ' begin {} { printf(length($0)); }' Well its a binary file and I am... (3 Replies)
Discussion started by: iffarrukh
3 Replies

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

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 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' ls Samp* | awk ' { a=$1 } END{ FS="n" for(i=1;i<=NR;i++) { while( getline < a ) { f1=$0; print("Line::",f1); f2=substr(f1,1,10) print("Field1::",f2);... (10 Replies)
Discussion started by: meva
10 Replies

8. UNIX for Dummies Questions & Answers

Convert a tab delimited/variable length file to fixed length file

Hi, all. I need to convert a file tab delimited/variable length file in AIX to a fixed lenght file delimited by spaces. This is the input file: 10200002<tab>US$ COM<tab>16/12/2008<tab>2,3775<tab>2,3783 19300978<tab>EURO<tab>16/12/2008<tab>3,28523<tab>3,28657 And this is the expected... (2 Replies)
Discussion started by: Everton_Silveir
2 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