Sed command to replace the second number in every line with the string system

Consider the following file called system.txt:

system.txt
1  2  3  4
5  6  7  8
9  He 11 Hg
Ts H  15 Be
17 Mc Li 20

Write a one-line sed command that replaces the second number in every line with the string "system", without the quotes. For example, after running the appropriate sed command, the following would be output to the terminal:

updated system.txt
1  system  3  4
5  system  7  8
9  He system Hg
Ts H  15 Be
17 Mc Li system

We will use s command to replace the number with system but we have to replace the second occurrence of the number so we will use 2 as a flag to s command. A character class([]) will be used to select the numbers. sed command to achieve this is:

$sed 's/[0-9][0-9]*/system/2' system.txt

Recommended Posts