Split (various sized) lines into two columns keeping the first word in a column

Viewed 72

I would like to reformat a text file that contains multiple words by line: each word should be in a separate line, that starts with the first word of the original line.

The original text file looks like this:

line1 word1 word2 word3
line2 word4
line3 word5 word6

and the expected result should be

line1 line1
line1 word1
line1 word2
line1 word3
line2 line2
line2 word4
line3 line3
line3 word5
line3 word6

In fact, all the words are the same: there is no difference between the first word of a line and the others, this solution does not work.

As a beginner in awk, I tried this

awk 'BEGIN {FS=" "; OFS="\n"} { x=$1; $1=$1; printf "%s", x "\t" $0 "\n"}'

but it results in this

line1   line1
word1
word2
word3
line2   line2
word4
line3   line3
word5
word6

Thanks for your help!

1 Answers

Could you please try following:

awk '{for(i=1;i<=NF;i++){print $1,$i}}'  Input_file
Related