0

I am trying to read a CSV which has data like:

Name           Time
John          
Ken    
Paul         

I want to read column one if it matches then change time. For example, if $1 = John then change time of the John to $2.

Here is what I have so far:

while IFS=, read -r col1 col2
do
    echo "$col1"
    if[$col1 eq $1] then
        echo "$2:$col2"
done < test.csv >> newupdate.csv

To run ./test.sh John 30.

I am trying to keep the csv updated so making a new file I thought would be okay. so I can read updated file again for next run and update again.

John Kugelman
  • 349,597
  • 67
  • 533
  • 578
X00145570
  • 57
  • 7
  • 1
    What is your question? Please [edit] your question to include your code, sample input and output, and any error messages. Tell us what you expect to happen as well as what's actually happening. This will help us answer your question better. – John Kugelman Aug 20 '20 at 14:38
  • 1
    Your example doesn't look like CSV. If you really have commas between the fields, please [edit] to show a genuine example. If you have another separator (tabs maybre?) your code is wrong, and you need to update the question to document your actual requirements. – tripleee Aug 20 '20 at 14:45

1 Answers1

1

Your shell script has a number of syntax errors. You need spaces inside [...] and you should generally quote your variables. You can usefully try http://shellcheck.net/ before asking for human assistance.

while IFS=, read -r col1 col2
do
    if [ "$col1" = "$1" ]; then
       col2=$2
    fi
    echo "$col1,$col2"  # comma or colon separated?
done < test.csv >newupdate.csv

Notice how we always print the entire current line, with or without modifications depending on the first field. Notice also the semicolon (or equivalently newline) before then, and use of = as the equality comparison operator for strings. (The numeric comparison operator is -eq with a dash, not eq.)

However, it's probably both simpler and faster to use Awk instead. The shell isn't very good (or very quick) at looping over lines in the first place.

awk -F , -v who="$1" -v what="$2" 'BEGIN { OFS=FS }
   $1 == who { $2 = what } 1' test.csv >newupdate.csv

Doing this in sed will be even more succinct; but the error symptoms if your variables contain characters which have a special meaning to sed will be bewildering. So don't really do this.

sed "s/^$1,.*/$1,$2/" test.csv >newupdate.csv

There are ways to make this less brittle, but then not using sed for any non-trivial scripts is probably the most straightforward solution.

None of these scripts use any Bash-specific syntax, so you could run them under any POSIX-compatible shell.

tripleee
  • 175,061
  • 34
  • 275
  • 318
  • Thanks for your help , and detailed description . When i ran the first code i get output like Row1= Jhon , Row2= : Row3 = Ken Row4= : . i tried running second code and it has same issue :( – X00145570 Aug 20 '20 at 15:19
  • Your attempt looks like you wanted a colon-separated output file. Switch to a comma between the fields if that's what you want. But that should only affect the first script where I copy/pasted parts of your original attempt. I updated the scripts to always print commas. If you don't want to have a second field on the lines which currently don't have two fields, that's not really valid CSV (but certainly fixable as well; and the Awk script won't modify those lines). – tripleee Aug 20 '20 at 15:23
  • I just want to read a file and add time to the names. So if i Jhon wants a update then i can just run bash file ./bash.sh Jhon 30. It should output like Column 1 = Jhon Column 2 = 30 on same row. If u know what i mean . But when i ran the code it kept adding " : " to the next row and 30 wasn't added at all. – X00145570 Aug 20 '20 at 15:31
  • Thanks for ur time tho. For sed solution i don't know how it works . Where do i pass in parameter ? – X00145570 Aug 20 '20 at 15:33
  • 1
    As your original code, this assumes you call it with `John` as `$1` and the new value as `$2`. I can't see how it's not doing what you ask. – tripleee Aug 20 '20 at 15:36
  • Bash demo (adds comma to every line): https://ideone.com/4yaDeJ; Awk demo (only adds comma to John): https://ideone.com/L9L0JZ; `sed` demo (doesn't change anything because the input is not valid CSV): https://ideone.com/4UQ24K; modified `sed` demo https://ideone.com/wTXxQJ – tripleee Aug 20 '20 at 15:44
  • Again, I asked you to clarify whether you really have CSV input. If you don't then you need to explain in more detail what your requirements are. – tripleee Aug 20 '20 at 15:44
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/220155/discussion-between-x00145570-and-tripleee). – X00145570 Aug 20 '20 at 15:54
  • I added few bits in dm . when u get chance please have a look – X00145570 Aug 20 '20 at 16:29