1

I have records in list.txt (INPUT) as

List of animals SET 1=Dog 2=Crow 3=Snake Pet,bird,reptile List of Countries SET 1=France 2=Singapore 3=Columbia Europe,Asia,SouthAmerica List of Parts SET 1=KeyBoard 2=Speaker 3=LEDpanel Computer,AudioPlayer,Television List of Cities SET 1=Amsterdam 2=KualLumpur 3=Paris Netherlands,Malaysia,France 

Want the last column of each line to be used as array for replacing the numbers 1,2,3 For eg. Use Pet,bird,reptile of first line to make Pet=Dogbird=Crow and reptile=Snake.

So, the OUTPUT file would be

List of animals SET Pet=Dog bird=Crow reptile=Snake List of Countries SET Europe=France Asia=Singapore SouthAmerica=Columbia List of Parts SET Computer=KeyBoard AudioPlayer=Speaker Television=LEDpanel List of Cities SET Netherlands=Amsterdam Malaysia=KualLumpur France=Paris 

Using awk, I could split the last column as string of array. But unable to use the same to replace the numbers 1,2,3.

    4 Answers 4

    1

    You can loop over items in an awk array a using the syntax for (i in a). So for example you could do something like

    awk '{split($NF,a,","); $NF=""; for (i in a) sub(i"=",a[i]"=",$0); print}' list.txt 
    0
      0

      If there are always 3 items (x=y) and no space in the lines, this awk statement should work:

      awk -F',| |=' '{printf "%s %s %s %s %s=%s %s=%s %s=%s\n", \ $1, $2, $3, $4, $11, $6, $12, $8, $13, $10}' list.txt 

      Explanation:

      • -F',| |=': set the field separator to ,, space and =
      • '{printf ... print the values in the desired format
      • list.txt the input file
        0

        This script should work for any number of items in a row.

        while read line do valType=($(echo $line | awk '{ print $NF }' | tr ',' ' ')) vals=($(echo $line | awk '{ $NF="";$1="";$2="";$3="";$4=""; print $0 }' | tr [1234567890] ' ' | tr '=' ' ' )) echo $line | awk '{print $1" "$2" "$3" "$4 }' | tr -d '\n' numVals=${#vals[@]} for i in $(seq 0 $((numVals-1))) do echo -n " "${valType[$i]}"="${vals[$i]} done echo " " done < list.txt

        Explanation:

        valType contains an array of the comma seperated elements.

        vals contains the associated values between the 4th and last positions in the row, with numbers and equals signs removed.

        Looping through the number of values in the arrays, the matching values in both arrays can be referenced.

          0
          set '.\(=.[^ ]*\) ' '\([^,]*\),' sed "s/\$/,/;s/$1$1$1$2$2$2/\4\1 \5\2 \6\3/ " <<\DATA List of animals SET 1=Dog 2=Crow 3=Snake Pet,bird,reptile List of Countries SET 1=France 2=Singapore 3=Columbia Europe,Asia,SouthAmerica List of Parts SET 1=KeyBoard 2=Speaker 3=LEDpanel Computer,AudioPlayer,Television List of Cities SET 1=Amsterdam 2=KualLumpur 3=Paris Netherlands,Malaysia,France DATA 

          That uses the shell array to set your array.

          OUTPUT

          List of animals SET Pet=Dog bird=Crow reptile=Snake List of Countries SET Europe=France Asia=Singapore SouthAmerica=Columbia List of Parts SET Computer=KeyBoard AudioPlayer=Speaker Television=LEDpanel List of Cities SET Netherlands=Amsterdam Malaysia=KualLumpur France=Paris 

            You must log in to answer this question.

            Start asking to get answers

            Find the answer to your question by asking.

            Ask question

            Explore related questions

            See similar questions with these tags.