-3

I wrote a todo list script with "add","clear" and "find" argument. I need to pass a "done" argument and script find related row and replace its first column with another value. How can I do that?

Result of script shows in the follow :

0,H,First Task
0,L,Second Task
0,M,Third Task

and now i want to execute something like ./todo done 2, and obtain the following result:

0,H,First Task
1,L,Second Task
0,M,Third Task
3
  • 1
    Welcome here! That is your task, but what is your problem? If you could share your approach and explain what fails about it, we can help you. You probably want to write a shell script with something like case "$1" in done) sed "$2s/^0,/1,/";; or what? How do we know? Commented May 30, 2023 at 6:50
  • 1
    You say you want to extend the functionality of an existing piece of software you wrote, but you do not show the source code of that software. This would likely mean that solutions would be mostly unusable as they would not be easy to retro-fit into the existing code. It's also not clear from the question what is the issue; the script with its handling of arguments, or the manipulation of the CSV file. Commented May 30, 2023 at 6:58
  • Please clarify your specific problem or provide additional details to highlight exactly what you need. As it's currently written, it's hard to tell exactly what you're asking. Commented May 31, 2023 at 12:50

2 Answers 2

0
file=todo.list
line=2
perl -F, -pi -lapse '
  $F[0] = 1 if $. == $line;
  $_ = join ",", @F' -- -line="$line" -- "$file"

Or with gawk:

file=todo.list
line=2
gawk -i /usr/share/awk/inplace.awk -v line="$line" -F, -v OFS=, -e '
  NR == line {$1 = 1}
  {print}' -E /dev/null "$file"

With GNU sed, you could do:

sed -i -e "${line}s/^[^,]*/1/" -- "$file"

But you'd have to sanitise $line (make sure it contains a sequence of [0123456789] characters) beforehand or otherwise, that would introduce an arbitrary command execution vulnerability.

With gawk, do not use -i inplace as gawk tries to load the inplace extension (as inplace or inplace.awk) from the current working directory first, where someone could have planted malware. The path of the inplace extension supplied with gawk may vary with the system, see the output of gawk 'BEGIN{print ENVIRON["AWKPATH"]}'

-1
#!/bin/bash
fis=$1
col1=$2
if [[ $fis == "done" ]]
then
awk -v col1="$col1" -F "," 'BEGIN{OFS=","}NR==col1{gsub(".*","PRAVEEN",$1)}1' ht.txt
fi

output

0,H,First Task
PRAVEEN,L,Second Task
0,M,Third Task
1
  • 1
    welcome back! gsub(".*","something",$1) == sub(".*","something",$1) == $1="something". Commented Jun 14, 2023 at 14:07

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.