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
ahjdh,dfhkajhdf,adhklf,eiour

say I need to check length only for 2nd and 4th and max length for 2nd is 6 and max length for 4th field is 4.

Can somebody please help me.
I was trying with AWK , but couldn't move forward with it , please let me know if my logic is correct.

awk 'BEGIN{ FS = OFS = ","
l="13,2000,160,200,100,50,50,30,30,30,170"
split(l,len,",")
row=$1
split(f,field,",")
f="1,3,11"
split(c,col,",")
}
{
 for (i=1;i<=len,i++) {
  for (j in len) {
  if 
  if (length($(field)) = len[j] )

Is this what you are trying to do?

awk -F, ' { if(length($2)>6) $2=substr($2,1,6); if(length($4)>4) $4=substr($4,1,4); print; } ' OFS=, filename
awk -F, 'BEGIN {len="5,6,7,8";split(len,arr,",");}{for(i=1;i<=NF;i++){if(length($i)>arr)next}}1' input.csv
 
$ cat input.csv
this123,is,test,message
this,is,test,message
this,is,test,message
this,is,test,message123

 
$awk -F, 'BEGIN {len="5,6,7,8";split(len,arr,",");}{for(i=1;i<=NF;i++){if(length($i)>arr)next}}1' input.csv
this,is,test,message
this,is,test,message

5,6,7,8 is the length to check the 1,2,3,4 fileds

if you want to check only for some particular fields, then

 
$awk -F, 'BEGIN {len="5,0,0,8";split(len,arr,",");}{for(i=1;i<=NF;i++){if( arr!=0 && length($i)>arr)next}}1'  input.csv
this,is,test,message
this,is,test,message

If you are checking only for few specific columns then use.

$ cat file
this123,is,test,mess
this,issdfd,test,mess
this,isdfsdsd,test,mes
this,isdsdddsd,test,message123

$ awk -F, 'length($2) <= 6 && length($4) <= 4' file
this123,is,test,mess
this,issdfd,test,mess

Thank you guys , checking the length and print works , but I also need to print the field name along with it . But somehow I am not able to print the field.
Can you please tell if we can print the array value.

For exp in the above example

This is what I tried

  nawk -F, 'BEGIN {len="5,6,7,8";split(len,arr,",");col="col1,col2,col3,col4";split(col,arr1,",");}{for(i=1;i<=NF;i++){if(length($i)>arr) $i=substr($i,1,arr); print${arr1} next}}1' input.txt

input.txt
------------
 checking,the, lengthof, string

output.txt
----------
check,the, lengtho, string,Field col1,col3 exceeds the length

Try this; may need some polishing, esp. reg. the lenghts to be supplied in a variable:

$ cat file
checking,the, lengthof, string
$ awk -F, 'BEGIN {split("5,6,7,8", len, ",");  OFS=","}
           {for (i=1; i<=NF; i++) if(length($i) > len) {$i = substr($i, 1, len); col++; OA=1}}
           {printf "%s", $0;
            if (OA) {printf ", Fields ";
                     for (i=1; i<=NF; i++) {if (col) printf "col%d ", i; col=0;}
                     printf "exceed the length."
                    }
            printf "\n"; OA=0
           }
          ' file
 check,the, length, string, Fields col1 col3 exceed the length.

Thank you , this is how I want the result to be , but my column names are not exactly col1, col2 etc . Actually they are different.

Like

name, city, zipcode, state, country,eff_date, start_date, end_date.

I need to display the actual field name for ex: city, zipcode, start_date is null.

I am not able to read this in an array. can you please help me .

Try sth like this.. adapting Rudic's sollution..

$ cat file
name, city, zipcode, state
checking,the, lengthof, string

$ awk -F, 'BEGIN {split("5,6,7,8",len,",");  OFS=","}
NR==1{split($0,P,",")}
NR>1{for (i=1; i<=NF; i++) if(length($i) > len) {$i = substr($i,1,len); arr=arr?arr" "P:P}
if (arr) {$(NF+1)=" Fields "arr" exceed the length."; arr=""}
}1' file

name, city, zipcode, state
check,the, length, string, Fields name  zipcode exceed the length.
1 Like

Perfect ..thank you very much.This is exactly what I need. :b: Can you please explain me this part of the code.

"arr=arr?arr" "P[i]:P[i]} "

arr=arr?arr" "P:P # Here we assign values to arr. First here it checks arr presents or not

arr? --> Is arr present? If present then do the true part else do the false part. see below.

arr?True:False

Assume at start arr="" so arr? is false because arr is not present. so it assing P to arr.
Next time arr? is true so it appends " "P to the arr.

And P represents field name for that particular value

Hope this helps you:)

pamu

1 Like