Skip to content
Advertisement

Swapping the first word with itself 3 times only if there are 4 words only using sed

Hi I’m trying to solve a problem only using sed commands and without using pipeline. But I am allowed to pass the result of a sed command to a file or te read from a file. EX:

JavaScript

Anyway lets say I had a file F1 and its contents was :

JavaScript

The output should be:

JavaScript

Explanation: the program must only print lines that have exactly 4 words and when it prints them it must print the first word of the line 3 times.

I’ve tried doing commands like this:

JavaScript

or

JavaScript

but I can’t figure out how i can calculate with sed that there are only 4 words in a line. any help will be appreciated

Advertisement

Answer

JavaScript

The above uses a sed that supports EREs with a -E option, e.g. GNU and OSX seds).

User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement