Issue
I have a file called test
that have:
1 2 3
2 3
4 5 6 7
8 9 10
11 12 13 14 15 16 17
18 19 20
I want to get the lines that have 3 words in them, and then print them, while the first word is duplicated.
I cant use pipeline, and I can use >| to put it in a tmp file and read from it.
so the output in this case is:
1 1 2 3
8 8 9 10
18 18 19 20
I understand more or less what regular expr I need, but the rest im struggling, could someone please help :?
this is what I did:
sed 's/'^[^ ]*[ ]+[^ ]+[ ]+[^ ]+[ ]*$'/&&/1/ test
I know this is not the solution but please help me understand :/
Solution
You could try this,
$ sed -nr 's/^([^ ]+) +[^ ]+ +[^ ]+$/\1 &/p' file
1 1 2 3
8 8 9 10
18 18 19 20
From man sed
-n, --quiet, --silent
suppress automatic printing of pattern space
p Print the current pattern space.
^
Asserts that we are at the start. (..)
called capturing group, which is used to capture characters. Later you could refer those captured characters by back-referencing it's index number. ([^ ]+)
Captures any character but not of a space one or more times. +
repeats the previous token one or more times. $
asserts that we are at the line end.
OR
$ sed -n 's/^\([^[:blank:]]\+\)\([[:blank:]]\+\)[^[:blank:]]\+[[:blank:]]\+[^[:blank:]]\+$/\1\2&/p' file
1 1 2 3
8 8 9 10
18 18 19 20
[^[:blank:]]\+
Matches one or more non-space characters. [[:blank:]]\+
matches one or more space characters. &
in the replacement part will print all the matched characters.
Answered By - Avinash Raj Answer Checked By - Senaida (WPSolving Volunteer)