Split line into key-value pairs based on first string

I want to know if there is an easy way to split a line in a file into multiple lines, such as this:

I have

A B C
1 2 3 4

And I want to get something like this, based on the first string of the line:

A B
A C
1 2
1 3
1 4

Basically based on the first string in the line create multiple lines with the second and third and fourth string and so on.

Here is Solutions:

We have many solutions to this problem, But we recommend you to use the first solution because it is tested & true solution that will 100% work for you.

Solution 1

Here goes

awk '{for (i=2; i<=NF; ++i) print $1, $i}' file
A B
A C
1 2
1 3
1 4

Solution 2

printf %s\\n 'A B C' '1 2 3 4'|
sed -e's/\([^ ]*\)  *[^ ]*/&\n\1/;//P;D'

A B
A C
1 2
1 3
1 4

That works. It selects the first two sequences of zero or more not-space characters which are separated by one or more spaces. The first such sequence is referenced in \1 and the whole selection in &. The selection is replaced with itself followed by a \newline then \1. Pattern space is then printed up to the first occurring newline, and then the same portion is Deleted before the pattern space is recycled to the top of the script with what remains.

You can see what it does with the look command. Replace the P w/ l and put another l before the s///ubstitution…


A B C$
A B\nA C$
A C$
A C\nA$
A$
1 2 3 4$
1 2\n1 3 4$
1 3 4$
1 3\n1 4$
1 4$
1 4\n1$
1$

printf %s\\n 'A B C' '1 2 3 4'|
sed -ne:t -e'/  *[^ ]*/{s//\n&/2;P;s///;} -ett

A B
A C
1 2
1 3
1 4

It matches a pattern space with at least one sequence of space characters and any trailing not-spaces. The first substitution inserts a newline before the second occurrence of such a sequence, then Prints up to the newline, and the second substitution removes the first occurrence of that pattern – which will also now include the newline the first one appended to the tail of that sequence when operating on the second. The test branches back to the :t label each time a substitution occurs, and so sed eats pattern space a space separated field at a time.

With look again:


A B C$
A B\n C$
A C$
A C$
1 2 3 4$
1 2\n 3 4$
1 3 4$
1 3\n 4$
1 4$
1 4$

Solution 3

using bash –

    while read x ; do set $x ; first=$1 ; shift; for i in $* ; do echo $first  $1; shift; done  ; done  < /tmp/1
    A B
    A C
    1 2
    1 3
    1 4

Note: Use and implement solution 1 because this method fully tested our system.
Thank you 🙂

All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply