Grab from the file in one command

Dear All,

I have a file in which there are 54 fields, i want to grab the all the lines and send to a new file where filed18 has lenght greater than 14. How can i do it without if condition and faster way:

currently i am reading file line by line and comparing the length

read fileLine || break
        oldDate=`echo $fileLine | cut -f18 -d "|" `
        if [ "${#oldDate}" -gt 14 ]
        then
                `echo $fileLine >> $newFileName`
        fi

can anyone suggest better and improved way. Thanks

awk 'length($18) > 14' input_file > new_file

If you read the code which bilalghazi provided, there is | to be used in cut.

So the code should be fixed by:

awk -F "|" 'length($18) > 14' input_file > new_file

Good spot... thanks :slight_smile:

Thanks a lot, it resolved my problem. :slight_smile:

---------- Post updated at 05:36 PM ---------- Previous update was at 05:35 PM ----------

is it possible that i replace the value of field18 with a variable value and then save the whole line in the new file, only if the length of field18 is greater than 14.

awk -F "|" 'length($F) > L' F=18 L=14 input_file > new_file

i think i was not clear enough to explain my problem, the problem is that i have one variable which has some value say value1 now i want to check each line of file, if the field18 is greater than 14 than replace whatever in the field18 with value1. I hope i am clear this time. Thanks

Hi.

awk -F "|" 'length($18) > 14 && $18=F' F="$MY_VARIABLE" input_file > new_file

well thanks, but this command eliminates "|" from the new_file and put a space :(.

---------- Post updated at 04:51 PM ---------- Previous update was at 04:49 PM ----------

i use the following which resolved the problem:

awk -F "|" 'length($18) > 14 && $18=F' F="TESTING_BILAL" SMSC2010012812420800009799  | sed -e "s/ /|/g" > new_file2

Thanks :slight_smile:

use OFS in awk to format your output and you can skip the sed command