Tips to use “sed” command

After we used the sed command in our pipeline, we had some surprises and took a little bit little bit of occasions to grasp our problem.

So right here is a few suggestions that I discovered from these searches.




Change each prevalence in a file

As stated within the title, the next command will change each occcurrence of “AAAA” by “BBBB” within the “file.json” file.

sed -i 's/AAAA/BBBB/g' file.json
Enter fullscreen mode

Exit fullscreen mode




Change first prevalence of every line

Right here is the most important problem that we had. With out the “g” on the finish of the exchange choice, the next command will solely exchange the primary prevalence of “AAAA” in every line.

sed -i 's/AAAA/BBBB/' file.json
Enter fullscreen mode

Exit fullscreen mode




Change first prevalence

The next command is to switch the primary prevalence of “Apple” by “Banana”. However this substitute will solely happen between the index 0 and the primary prevalence of Apple.

sed '0,/Apple/{s/Apple/Banana/}' input_filename
Enter fullscreen mode

Exit fullscreen mode

So you possibly can simply customise it to switch the primary prevalence of Apple after a specific index or particular phrases.


I hope it can show you how to! 🍺

Add a Comment

Your email address will not be published. Required fields are marked *