perl or awk, field length check


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting perl or awk, field length check
# 1  
Old 08-27-2010
perl or awk, field length check

Hi Everyone,

1.txt
Code:
a;1234;134;1111111
b;123;123;1111111
c;123;1334;1111111
d;1234;1234;1111111

output
Code:
a;1234;134;1111111
c;123;1334;1111111
d;1234;1234;1111111

if field2 legth>3 or field3 length >3, then output. Please advice.

Thanks
# 2  
Old 08-27-2010
Code:
awk -F";" 'length($2)>3 || length($3)>3' infile

This User Gave Thanks to zaxxon For This Post:
# 3  
Old 08-27-2010
Thanks
# 4  
Old 08-29-2010
Code:
while(<DATA>){
  print if /^[^;]*;(?=([^;]{4}|[^;]{1,3};[^;]{4}))/;
}
__DATA__
a;1234;134;1111111
b;123;123;1111111
c;123;1334;1111111
d;1234;1234;1111111

# 5  
Old 08-30-2010
Code:
$
$ cat f6
a;1234;134;1111111
b;123;123;1111111
c;123;1334;1111111
d;1234;1234;1111111
$
$
$ perl -F';' -lane 'print if length($F[1])>3 || length($F[2])>3' f6
a;1234;134;1111111
c;123;1334;1111111
d;1234;1234;1111111
$
$

tyler_durden
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Using awk to add length of matching characters between field in file

The awk below produces the current output, which will add +1 to $3. However, I am trying to add the length of the matching characters between $5 and $6 to $3. I have tried using sub as a variable to store the length but am not able to do so correctly. I added comments to each line and the... (4 Replies)
Discussion started by: cmccabe
4 Replies

2. Shell Programming and Scripting

awk to output the percentage of a field compared to length

The awk below using the sample input would output the following: Basically, it averages the text in $5 that matches if $7 < 30 . awk '{if(len==0){last=$5;total=$7;len=1;getline}if($5!=last){printf("%s\t%f\n", last,... (6 Replies)
Discussion started by: cmccabe
6 Replies

3. UNIX for Dummies Questions & Answers

Help with awk, where line length and field position are variable

I have several questions about using awk. I'm hoping someone could lend me a hand. (I'm also hoping that my questions make sense.) I have a file that contains pipe separated data. Each line has similar data but the number of fields and the field position on each line is variable. ... (3 Replies)
Discussion started by: Cheese64
3 Replies

4. Shell Programming and Scripting

Replace a field with a character as per the field length

Hi all, I have a requirement to replace a field with a character as per the length of the field. Suppose i have a file where second field is of 20 character length. I want to replace second field with 20 stars (*). like ******************** As the field is not a fixed one, i want to do the... (2 Replies)
Discussion started by: gani_85
2 Replies

5. Shell Programming and Scripting

Check for length which exceeds specified length in a line

Hi, I have a issue, I need to loop through a comma delimited file and check for the length which exceeds specified length , if Yes truncate the string. But my problem is , I do not have to check for all the fields and the field lenght is not same for all the fields. For ex: Say my line... (9 Replies)
Discussion started by: rashmisb
9 Replies

6. Shell Programming and Scripting

Flat file-make field length equal to header length

Hello Everyone, I am stuck with one issue while working on abstract flat file which i have to use as input and load data to table. Input Data- ------ ------------------------ ---- ----------------- WFI001 Xxxxxx Control Work Item A Number of Records ------ ------------------------... (5 Replies)
Discussion started by: sonali.s.more
5 Replies

7. Shell Programming and Scripting

Insert missing field using perl,sed,awk

sample file (comma as field separators) MessageFlow,1,BusIntBatchMgr,a OOBEvent,1,BusIntBatchMgr,a TaskEvents,1,,a MTTrace,1,,a MTWarning,,1,a MessageFlow,1,Batch,a OOBEvent,1,Batch,a TaskEvents,1,,a EAISAPIdocWizard,1,BusIntMgr,a EAISAPBAPIWizard,1,BusIntMgr,a... (3 Replies)
Discussion started by: vrclm
3 Replies

8. Shell Programming and Scripting

Using Awk script to check length of a character

Hi All , I am trying to build a script using awk that checks columns of the înput file and displays message if the column length exceeds 35 char. i have tried the below code but it does not work properly (2 Replies)
Discussion started by: amit1_x
2 Replies

9. Shell Programming and Scripting

Help with finding length of a field

I have a pipe delimited file. I need to check that the first and second fields are 5 characters long and if not i need to append 0 in front of them to make them 5 characters long. can some body let mwe know how i can find the length of the two fields and then make them 5 characters long if they... (6 Replies)
Discussion started by: dsravan
6 Replies

10. Shell Programming and Scripting

awk: How to check if field is blank?

In awk, I'd like to check if a field is blank. And by blank I mean, the field could be "" or " " In other words, the field could either be empty, or be filled with spaces. Would the regex look like this? $5 ~ // { Action }? What other ways are there? Hmm.. in any case I think... (7 Replies)
Discussion started by: yongho
7 Replies
Login or Register to Ask a Question