-1

I want to parse the file name and assign a variable (code) based on it. I think I can use cut to get each values. But can we do it in one command?

sample file name: 012345678_012345678-abcdefghijklmnop_YYYYMMDD_TTTT

from the 2nd qualifier 012345678 i need to assign a value to the variable (code).

if col-2 & col-3 is yy and col-4 & col-5 is 00 or 01 or 02 and col 678 is 'abc' assign code = usa

if col-2 & col-3 is yy and col-4 & col-5 is 00 or 01 or 02 and col 678 is not equal to 'abc' assign code = uk

if col-2 & col-3 is not equal to yy and col-4 & col-5 is not equal to 00 or 01 or 02 and col 678 is not equal to 'abc' assign code = Aus

Note: the 2nd qualifer is always after the first underscore and before the first dash. The yy is the value in the 2nd qualifier in position col -2 and col-3

Example file name:012345678_01yy00abc-xxxxxxxxxxxxxxxxxxxxx_20180408_1300 assign code = usa

Example file name: 012345678_01yy00xyz-xxxxxxxxxxxxxxxxxxxxx_20180408_1300 assign code = uk

Example file name: 012345678_01aa98def-xxxxxxxxxxxxxxxxxxxxx_20180408_1300 assign code = Aus (so if it not usa or uk, it is Aus)

  • Is that second qualifier always after the first underscore and before the first dash? Also, there are no yy in your example, correct? – Jeff Schaller Apr 8 '18 at 15:59
  • 1
    Perhaps an example that matches one of your criteria would help. – Jeff Schaller Apr 8 '18 at 15:59
  • yes...the 2nd qualifer is always after the first underscore and before the first dash. The yy is the value in the 2nd qualifier in position col -2 and col-3. – Krrp78 Apr 8 '18 at 16:59
  • Example file name:012345678_01yy00abc-xxxxxxxxxxxxxxxxxxxxx_20180408_1300 assign code = usa – Krrp78 Apr 8 '18 at 17:01
  • 1
    If you're happy with one or several of the answers, upvote them. If one is solving your issue, accepting it would be the best way of saying "Thank You!" Accepting an answer also indicates to future readers that the answer actually solved the problem. – Kusalananda Apr 9 '18 at 6:22
0

The following bash script loops through all the files in the current directory that match the pattern (something) underscore (something) dash (something) underscore (something) underscore (something) and then picks apart the relevant fields for comparison. The ${field:4:2} syntax means to take the value of $field starting at position 4 and extract two characters.

The various pieces are then compared according to the logic you listed, and the code variable is assigned.

#!/bin/bash

for f in *_*-*_*_*
do
  field=${f#*_}
  field=${field%%-*}

  col23=${field:2:2}
  col45=${field:4:2}
  col678=${field:6:3}

  if [ "$col23" = "yy" ]
  then
    case $col45 in
      (00|01|02)
        if [ "$col678" = "abc" ]
        then
                code=usa
        else
                code=uk
        fi
        ;;
      (*)
        code=Aus
        ;;
    esac
  else
    code=Aus
  fi
  echo "For file $f, code is: $code"
done
  • Happy to help, @Krrp78. Don’t forget to click the check mark if this solves your problem – Jeff Schaller Apr 9 '18 at 0:54
0

Just one regex could gice you all fields.
Just one case statement could test all conditions:

#!/bin/bash

a='[^_]+_'    b='[^-]{2}'    c='[^-]{3}'
regex="^$a($b($b)($b)($c))-$a$a.*$"

for f in *_*-*_*_*; do
    if [[ $f =~ $regex ]]; then

        arr=("${BASH_REMATCH[@]:1}")
        # echo "field=${arr[0]} col23=${arr[1]} col45=${arr[2]} col678=${arr[3]}"

        a=0; b=0; c=0
        [[ ${arr[1]} == "yy"        ]] && a=1
        [[ ${arr[2]} == @(00|01|02) ]] && b=1
        [[ ${arr[3]} == "abc"       ]] && c=1
        case $a$b$c in
            (111) code=usa ;;
            (110) code=uk  ;;
            (*)   code=Aus ;;
        esac
        echo "For file $f, code is: $code"
    fi
done

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.