Help to write a script for the below requirement

Hi,

I am not so familiar with shell Script but I have a task in hand. So, here it goes:

There is a file details.txt in the server where the below details are stored:

Name Country D  M    Acc.No.
sameer India  30 july  sscc-errttt-q
random US     20 july    pecc-ttt4-s
Deb      India  31 july  ssee-tttttt-s
Amit     US     20 july    recc-ttt7-s
Sharad  India 31 july   ssee-tttttt-s

Now, the 5th column,i.e. Acc.No. ideally should have a length of 13 bytes where as in 2 cases (I am only showing 2, but there are many in reality) it is of 11 bytes. So I want to write a script to take out those lines (the complete line) which are having length as 11 bytes in the Acc.No. column and direct them to a file. I am in a fix as I dont know how to achieve it. Kindly provide me the solution. So, the o/p file shuld have entry like:

Name Country D   M    Acc.No.
random US     20 july    pecc-ttt4-s
Amit     US     20 july    recc-ttt7-s

try:

awk 'NR==1 || length($5)!=13' details.txt > new_file.txt
1 Like

Another approach using bash:

#!/bin/bash

while read nm cn dt mm acc
do
        [ "$nm" = "Name" ] && printf "%s\t%s\t%s\t%s\t%s\n" "${nm}" "${cn}" "${dt}" "${mm}" "${acc}"
        [ "${#acc}" -eq 11 ] && printf "%s\t%s\t%s\t%s\t%s\n" "${nm}" "${cn}" "${dt}" "${mm}" "${acc}"

done < file

Output:

Name    Country D       M       Acc.No.
random  US      20      july    pecc-ttt4-s
Amit    US      20      july    recc-ttt7-s
1 Like

Thank u so much for your help.. the second one worked smoothly. for teh 1st solution, it goes into an infinite loop saying commannot found (the awk command is providing the result though when used explicitly). I am using while loop. Can you help?
~Neel