0

I have the following script:

file="home/report.csv"

while IFS= read -r line
do
sed 's/\,/;/' > tmp.txt
done <"$file"

file2="home/tmp.txt"

while IFS= read -r line
do
awk -F. '{print $1";service" > "report_v2.csv"}' OFS=;
done <"$file2"

After the first "While", the file "tmp.txt" does not have the first line of "report.csv". Then, after the second "While", the file report_v2.csv does not have the first line of tmp.txt.

Hence, the last file has two lines less than the original one.

This is an example of my files:

report.csv

1,foo
2,pippo
3,pluto
4,davis

tmp.txt

2;pippo
3;pluto
4;davis

report_v2.csv

3;pluto;service
4;davis;service

I need to keep the first two lines of the original file also in the last file. How can I do?

Thanks

2
  • Replace > tmp.txt with >> tmp.txt. – Cyrus Aug 5 '20 at 17:09
  • You never need sed when you're using awk and you shouldn't use shell loops just to manipulate text, see why-is-using-a-shell-loop-to-process-text-considered-bad-practice. The script you posted wouldn't do anything but hang waiting for input since you aren't giving the sed or awk commands a file to operate on. Please edit your question to clarify what it is you're trying to do and then we can help you. – Ed Morton Aug 5 '20 at 21:49
0

Welcome to the site, Erasmo. You could simplify a good deal with:

#!/bin/bash

file="report.csv"
sed 's/\,/;/g' "$file" > tmp.txt
file2="tmp.txt"
awk '{print $1";service"}' "$file2" > report_v2.csv

Which should have tmp.txt yield:

1;foo
2;pippo
3;pluto
4;davis

And should have report_v2.csv yield:

1;foo;service
2;pippo;service
3;pluto;service
4;davis;service
4
  • thank you! it works fine! – Erasmo Vizzaccaro Aug 6 '20 at 8:28
  • Glad to hear it. Please accept the answer as a solution if this is working for you. – Kahn Aug 6 '20 at 12:41
  • I've already tried to accept your answer but I get the following pop-up message: "Votes cast by those with less than 15 reputation are recorded, but do not change the publicly displayed post score" – Erasmo Vizzaccaro Aug 7 '20 at 14:36
  • Oh. That sounds like you're trying to upvote the solution rather than click the greyed out checkmark. Either way, glad the solution is working for you. – Kahn Aug 7 '20 at 14:49
0

You can just use below command to get the required result

awk '{gsub(",",";",$0);print $0";service"}' report.csv >> report_v2.csv

output

1;foo;service
2;pippo;service
3;pluto;service
4;davis;service

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.