Home > database >  Update a column matching criteria in TAB seperated line using bash
Update a column matching criteria in TAB seperated line using bash

Time:10-28

I have a TAB separated txt file looking like this;

Serving Sector  Target Sector   HO Attempts HO Successful Attempts
1002080 1002081 8   8
1002080 1002084 0   0
1002080 1002974 2   2
1002080 2104-2975   5   5
1002080 1002976 2   2
1002080 1012237 10  10
1002080 1012281 0   0

In some situations the Target Sector(column 2) might be on this format 2104-2975( ABCD-YYYY). In those cases I wish to update this string of column 2 to the correct format (BC0YYYY = 1002975)

This is what I have written so far;

while read -r line;
do
        if echo $line | grep -E '([0-9])-([0-9])' # If line matches criteria
        then
                string=`echo "$line" | awk -F '\t' '{{print $2}}'`   #fetch column 2
                LAC=${string%-*}   #LAC= ABCD
                CI=${string##*-}   #CI = YYYY
                if [ ${#CI} -lt 5 ]; then CI="0"$CI;  #IF stringlength of CI is less than 5, add 0 
                fi
                LAC2=`echo $LAC | cut -c2-3` #LAC2 = BC
                GERANCELL=$LAC2$CI

        fi
done < input.txt

Anyone know how to update the 2nd column of the line with the new value $GERANCELL?

CodePudding user response:

# perl -lane'BEGIN{$"="\t"}if($F[1]=~/-/){($a,$b)=(split/-/,$F[1]);$F[1]=sprintf "dd",substr($a,1,2),$b;print "@F"}else{print "@F"}' file 
erving  Sector  Target  Sector  HO  Attempts    HO  Successful  Attempts
1002080 1002081 8   8
1002080 1002084 0   0
1002080 1002974 2   2
1002080 1002975 5   5
1002080 1002976 2   2
1002080 1012237 10  10
1002080 1012281 0   0

CodePudding user response:

A straightforward sed solution could be:

sed 's/[0-9]\([0-9][0-9]\)[0-9]-\([0-9]\{4\}\)/\10\2/' input.txt
  • Related