Reputation: 15
I am exploring sed function
what I have here is a simple text file shown below
Id,fruit name and quantity. now I want to add the amount of the fruit behind the quantity of each fruit respectively
fruit.txt
1,apple
2,banana
how to make it become like this.
expected output
first input of sed command
my textfile(fruit.txt) will become like this
1,apple,50
2,banana,40
Upvotes: 1
Views: 375
Reputation: 195289
use awk:
awk -F, -v OFS="," '/apple/{$3="whatever"}7' file
but if you don't have gnu awk 4.1, you have to do this to write back to the file:
awk -F, -v OFS="," '/apple/{$3="whatever"}7' file >~/foo.tmp && mv ~/foo.tmp file
Upvotes: 1
Reputation: 786359
Use sed like this:
sed -i -r 's/(apple)(,[0-9]*)?$/\1,10/'
sed -i -r 's/(apple)(,[0-9]*)?$/\1,20/'
sed -i -r 's/(apple)(,[0-9]*)?$/\1,30/'
Upvotes: 1