I'm writing my own password manager, which I want to be strictly POSIX compliant. To simplify: I have this file, file.csv
:
mastodon;password2
bla;test
test;test
posteo;tewtasdwqrr
Basically, I want to have a function which takes two arguments: it should select the first argument from the first column and from that line, it should replace the second column entry with the second argument.
For instance: f "bla" "etewtw"
would change the password of the last entry to etewtw
.
I tried to use awk, which also somewhat works:
awk -F ";" -v acc="bla" -v newpw="etewtw" \
'$1 ~ acc { $2=newpw; print $1";"$2 } END {print;} ' file.csv
Basically, I tried to set the second column to the newpw
argument if the first column matches on the acc
argument. After changing the stream, I want to print the complete stream, which doesn't work. The above is obviously not the correct solution, but I don't know how to fix that.
The output is:
bla;etewtw
posteo;tewtasdwqrr
So it is kinda successful, the entry was changed (at least in the stream, but actually changing the file is not difficult).
However, two problems arise:
Entries are missing in the output. Namely
mastodon;password2
andtest;test
. I expect those to a) stay at the same line and b) be unchanged.If I want to change the last line, it is always wrong. For instance, if I use
awk -F ";" -v acc="posteo" -v newpw="test" '$1 ~ acc { $2=newpw; print $1";"$2 } END {print;} '
instead, the result is:
posteo;test
posteo test
which is not what I want. I want the last line to be no different to any other line.