1

I have written a shell script and want to print output of 5 defined variables in csv file, I am using a condition, If condition 1 success then output should print in first 5 columns, else output should in next 5 columns, like below:

if condition 1 success

$DAY,$ModName,$Version,$END_TIME,$START_TIME

(should print in column number 1..5 of csv)

if condition 2 success

$DAY,$ModName,$Version,$END_TIME,$START_TIME

(should print in column number 6..10 of csv)

But using my code output always appends to next row

Below is my code:

if [ "$Version" = linux ] 
then 
echo "$DAY","$ModName","$Version","$END_TIME","$START_TIME" | awk -F "\"*,\"*" '{print $a ","}' >> output.csv;
else 
echo "$DAY","$ModName","$Version","$END_TIME","$START_TIME" | awk -F "\"*,\"*" '{print $b}' >> output.csv; 
fi

I tried n number of things apart from this code, but not able to find the solution.

I would appreciate your help :)

7
  • 1
    For instance: You are using awk variables a and b, but you never assign a value to them. Commented Jul 9, 2018 at 15:00
  • @user1934428, I tried doing awk -F "\",\"" '{print $1, $2, $3, $4, $5} , It works for 1st 5 values which satisfies with 1st condition, but applying awk -F "\",\"" '{print $6, $7, $8, $9, $10} is not printing values in column 6...10 . Commented Jul 9, 2018 at 16:38
  • 1
    You have only 5 input fields, so $6 will always be empty. Commented Jul 10, 2018 at 4:58
  • @user1934428 , I know, this is where i am stuck with, do you have any suggestion for this, so that i can drive in right direction ? Commented Jul 11, 2018 at 17:15
  • 1
    He doesn't print in separate rows. Please update your posting by your complete program based on Walter's solution, so we can't investigate. Commented Jul 12, 2018 at 13:09

1 Answer 1

1

{print $6, $7, $8, $9, $10} refers to input fields, not output.
When you want to start with 5 empty fields just printf them (avoiding a \n)

if [ "${Version}" != "linux" ]; then 
   printf "%s" ",,,,,"
fi
echo "${DAY},${ModName},${Version},${END_TIME},${START_TIME}" 

(Next time please use lowercase variable names)

When a variable can have a ',', you might need to give values in double quotes.

Sign up to request clarification or add additional context in comments.

1 Comment

your answer didn't help me out.

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.