Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In-place replacement

Tags:

sed

awk

I have a CSV. I want to edit the 35th field of the CSV and write the change back to the 35th field. This is what I am doing on bash:

awk -F "," '{print $35}' test.csv  | sed -i 's/^0/+91/g'

so, I am pulling the 35th entry using awk and then replacing the "0" in the starting position in the string with "+91". This one works perfet and I get desired output on the console.

Now I want this new entry to get written in the file. I am thinking of sed's "in -place" replacement feature but this fetuare needs and input file. In above command, I cannot provide input file because my primary command is awk and sed is taking the input from awk.

Thanks.

like image 679
slayedbylucifer Avatar asked Sep 10 '25 17:09

slayedbylucifer


2 Answers

You should choose one of the two tools. As for sed, it can be done as follows:

sed -ri 's/^(([^,]*,){34})0([^,]*)/\1+91\3/' test.csv 

Not sure about awk, but @shellter's comment might help with that.

like image 165
Lev Levitsky Avatar answered Sep 13 '25 16:09

Lev Levitsky


The in-place feature of sed is misnamed, as it does not edit the file in place. Instead, it creates a new file with the same name. eg:

$ echo foo > foo
$ ln -f foo bar
$ ls -i foo bar  # These are the same file
797325 bar  797325 foo
$ echo new-text > foo  # Changes bar
$ cat bar
new-text
$ printf '/new/s//newer\nw\nq\n' | ed foo  # Edit foo "in-place"; changes bar
9
newer-text
11
$ cat bar
newer-text
$ ls -i foo bar  # Still the same file
797325 bar  797325 foo
$ sed -i s/new/newer/ foo   # Does not edit in-place; creates a new file
$ ls -i foo bar
797325 bar  792722 foo    

Since sed is not actually editing the file in place, but writing a new file and then renaming it to the old file, you might as well do the same.

awk ... test.csv | sed ... > test.csv.1 && mv test.csv.1 test.csv

There is the misperception that using sed -i somehow avoids the creation of the temporary file. It does not. It just hides the fact from you. Sometimes abstraction is a good thing, but other times it is unnecessary obfuscation. In the case of sed -i, it is the latter. The shell is really good at file manipulation. Use it as intended. If you do need to edit a file in place, don't use the streaming version of ed; just use ed

like image 20
William Pursell Avatar answered Sep 13 '25 18:09

William Pursell



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!