-2

I have a csv file and I want to replace a line (remove it and add new information in its place) but I can't use sed. I don't know, my bash terminal doesn't support it. Let's say the csv file is like this:

a,b,c
d,e,f

I want to remove the line d,e,f and add g,h,i, for example.

randomguy
  • 1
  • 3
  • 1
    I wan't to solve X in a language which is not A isn't a good question to ask. That's because it is impossible to say what _can_ be used – hek2mgl Dec 11 '18 at 09:38
  • Assuming `bash` is available, try: `while read -r line; do echo "${line/d,e,f/g,h,i}"; done < input.csv > replaced.csv` – tshiono Dec 11 '18 at 09:51
  • 1
    How are basic tools not supported? Are you in some sort of restricted shell? Can you use `awk`? `perl`? `ls -l /tmp`? Could it just be that you need to fix your `$PATH`? – Paul Hodges Dec 11 '18 at 14:53
  • Can you use `tr`? – fd0 Dec 11 '18 at 19:30
  • My bash shell when I use `sed -i` pops illegal option. I never restricted you to anything but because I searched around and I only found solutions with `sed -i`, I was wondering if it could be done differently. @PaulHodges Yes those commands, I can used them fine. @fd0 , I haven't used `tr` but maybe it runs. And a little update, I used grep -v and it works (maybe I thought there was some problem with some argument). Anyway thanks for your replies so far. – randomguy Dec 12 '18 at 06:28
  • The duplicate has *dominantly* `sed` solutions, but there are a few good answers with alternatives. – tripleee Dec 12 '18 at 06:53
  • Does your `sed` work without `-i`? What OS and `sed` versions are you using? Either way, if `sed` works, you could `sed 's/a/b' < x > y && mv y x`. – Paul Hodges Dec 12 '18 at 14:28

1 Answers1

0

With Awk:

awk '/^d,e,f$/ { $0 = "g,h,i" }1' file.csv

GNU Awk has an inplace option, or just write to a temporary file and then mv it on top of the original.

If you have sed but it doesn't support the -i option, the same approach will work there.

perl -pi 's/^d,e,f$/g,h,i/' file.csv

will replace in-place just like sed -i.

tr is not suitable for this task. A Bash while read -r loop will work, but is clumsy and very inefficient.

tripleee
  • 175,061
  • 34
  • 275
  • 318