0

Using shell script,I want to count the csv file records (minus the header) and append the count to the same file in a new row.I also want to rename the file once the count

I have an input file mtest.csv like this

 IP Header,Process Header,Device Header,ID Header
 TestIP1,TestProcess2,TestDevice1,TestID1
 TestIP2,TestProcess2,TestDevice2
 TestIP3,TestProcess3,TestDevice3,TestID3

Sample output should look like this and the file should be renamed to mtest1.csv :

 IP Header,Process Header,Device Header,ID Header
 TestIP1,TestProcess2,TestDevice1,TestID1
 TestIP2,TestProcess2,TestDevice2
 TestIP3,TestProcess3,TestDevice3,TestID3
 TOTAL_RECORDS,3

So I am trying the count the number of values of rows minus the header and append it in the last row with comma delimtted value like (TOTAL_RECORDS,3)

I am trying like this

#!/bin/bash
count = `cat mtest.csv  | wc -l`
final_records = count -1
echo "TOTAL_RECORDS",$(final_records)  $mtest.csv > mtest1.csv

But this isn't working for me and overriding the whole file and not appending.Can someone please help.

pythonNinja
  • 453
  • 5
  • 13
  • 2
    Dupe of [how to append output to the end of a text file](https://stackoverflow.com/questions/6207573/how-to-append-output-to-the-end-of-a-text-file) – hnefatl Sep 23 '20 at 11:05
  • 1
    Btw, `final_records = count -1` isn't valid bash (too many spaces) and won't do what you expect, see e.g. https://www.gnu.org/software/bash/manual/html_node/Arithmetic-Expansion.html. There are other big problems with this, e.g. `$mtest.csv` is meaningless. You might want to read more about bash syntax. – hnefatl Sep 23 '20 at 11:06
  • No thats because i m trying to rename the file as well..I will try once >> and check – pythonNinja Sep 23 '20 at 11:28
  • Yes but you're saying use the undefined variable `$mtest.csv` as opposed to the file mtest.csv – bob dylan Sep 23 '20 at 12:12
  • The question that is supposed to be a duplicate answers only a part of the problem. It does not show how to fix the calculation of the number of records and the other errors in the script. – Bodo Sep 23 '20 at 12:27
  • 1
    `#!/bin/bash` `count=$(cat mtest.csv | wc -l)` `final_records=$((count -1))` `cp mtest.csv mtest1.csv` `echo "TOTAL_RECORDS,$final_records" >> mtest1.csv` (This should be an answer, but question is currently closed.) – Bodo Sep 23 '20 at 12:33
  • Thanks @Bodo.Let me try this – pythonNinja Sep 23 '20 at 12:38
  • @Bodo final_records=$((count -1)) gives " syntax error: invalid arithmetic operator (error token is " ")". .I also tried final_records=$((--count)) but same.Let me check more on this – pythonNinja Sep 23 '20 at 12:57
  • @Bodo Yeah tried like this final_records=$((count - 1)) .Still same error – pythonNinja Sep 23 '20 at 13:04
  • For me it works with and without space. You can try an alternative: `let final_records=count-1`. Read about arithmetic calculations in `bash`. – Bodo Sep 23 '20 at 13:05

0 Answers0