0

I have data in a comma separate file. This is the structure of the input file.

code, day_1, day_2, day_3

  • 1235, 0, 21, 6
  • 1236, 9, 3, 4
  • 1237, 1, 7, 3

I would like transform it, and display the column data as row entries

code, day, count

  • 1235, day_1, 0
  • 1235, day_2, 21
  • 1235, day_3, 6
  • 1236, day_1, 9
  • 1236, day_2, 3
  • 1236, day_3, 4
  • 1237, day_1, 1
  • 1237, day_2, 7
  • 1237, day_3, 3

Here's my code and output.

Code

#!/bin/bash
file='test_file.in'
IFS=$','
while read -r code day_1 day_2 day_3
do
        #echo "$code, $branch, $description"
        count=1
        while [ $count -le 3 ]
        do

                day_var=""'$day_'"${count}"
                #echo $day_var
                echo "$code, day_${count}, $day_var"
                count=$[$count+1]
        done
done < $file

Output:

120006, day_1, $day_1 120006, day_2, $day_2 120006, day_3, $day_3 120007, day_1, $day_1 120007, day_2, $day_2 120007, day_3, $day_3 140197, day_1, $day_1 140197, day_2, $day_2 140197, day_3, $day_3

Kindly suggest a bash script solution for me.

0

2 Answers 2

1

Here is the corrected version of your code:

#!/bin/bash

file='test_file.in'
while IFS=, read -r code day_1 day_2 day_3
do
        #echo "$code, $branch, $description"
        count=1
        while [ $count -le 3 ]
        do
                day_var=day_$count
                echo "$code, $day_var, ${!day_var}"
                count=$((count+1))
        done
done < "$file"

Compare it with yours.

But my approach would be:

#!/bin/bash

while IFS=, read -r code day1 day2 day3; do
    printf '%s, day_1,%s\n%s, day_2,%s\n%s, day_3,%s\n' \
        "$code" "$day1" "$code" "$day2" "$code" "$day3"
done < file
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you so much. Really appreciated.
0

A quick awk solution:

awk 'BEGIN{FS=OFS=","}{for(i=2;i<=NF;++i) print $1,"day_"(i-1),$i}' file

Why is your code not working:

There are many reasons why this is not the best code, but the main are in the following two lines:

day_var=""'$day_'"${count}"
echo "$code, day_${count}, $day_var"

You would like to use a variable name which you actually created from variables. This is called indirection and there is a nice BashFAQ/006 about it.

When we would fix your code, it would be something like this:

#!/usr/bin/evn bash
file='test_file.in'
while IFS=, read -r code day_1 day_2 day_3
do
        for (( count=1;count<=3; count++)); do
                day_var="day_${count}"
                echo "$code, day_${count}, ${!day_var}"
        done
done < $file

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.