Skip to content
Advertisement

Replace every second instance of word in each line of the file

I’m trying to replace every second instance word of each line with sed.

For example: If I had a line as below

JavaScript

should be replaced as below.

JavaScript

Only second instance of word is replaced with kiran/.

My attempt :

JavaScript

Output:

JavaScript

My Question:

Please help me with the proper sed or any Linux command which does the above job.

Advertisement

Answer

Could you please try following, if you are ok with awk.

JavaScript

Or adding a non-one liner form of above solution:

JavaScript

Output for shown sample will be as follows.

JavaScript
Advertisement