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

  • Replace `> tmp.txt` with `>> tmp.txt`. – Cyrus Aug 05 '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](https://unix.stackexchange.com/questions/169716/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 05 '20 at 21:49

2 Answers2

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
Kahn
  • 1,652
  • 2
  • 19
  • 36
  • thank you! it works fine! – Erasmo Vizzaccaro Aug 06 '20 at 08:28
  • Glad to hear it. Please accept the answer as a solution if this is working for you. – Kahn Aug 06 '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 07 '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 07 '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
Praveen Kumar BS
  • 5,139
  • 2
  • 9
  • 14