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.

2
  • 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. Commented Aug 20, 2020 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. Commented Aug 20, 2020 at 14:45

1 Answer 1

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.

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

9 Comments

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 :(
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).
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.
Thanks for ur time tho. For sed solution i don't know how it works . Where do i pass in parameter ?
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.
|

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.