You can do this entirely within the shell:
while read -r f1 f2
while read -r f3 f4
printf "%s %s\n" "$f1$f3" "$f2$f4"
done < your_file
done < your_file
"Each entry can consist of any characters except for newline and space."
If you really mean that entries can contain tab characters,
IFS=" " read instead of
read (both times).
The “fine print”:
A command like
read f1 f2
will read the first “word” on the line of input into variable
and the rest of the line into
f2. For example,
The quick brown fox
would result in
f2="quick brown fox".
If you’re sure that your file will never have three (or more) columns
(i.e., never have more than two words on any line),
then there’s nothing to worry about.
If you’re happy with everything that’s not part of the first word
being treated as part of the second word,
then the above code should be OK.
But, if you want
The quick brown fox to be treated as
brown fox being discarded (ignored),
then add a third variable to each of the
f1 f2 would become
f1 f2 x;
this will result in
Simply by not using
$x, we discard the input after the second word.
read can similarly be changed to
read -r f3 f4 x —
since we’re not using
$x, it doesn’t matter if we overwrite it.
If you’d rather use a different throwaway variable — e.g.,
… f3 f4 y —
that’s OK, too.
read command, by default,
treats the backslash (
\) character specially.
Basically, backslash followed by any other character
merge into a special version of the second character.
\C\C would be read as
But, more importantly,
backslash followed by space is not treated as a word separator,
and backslash followed by newline (i.e., a backslash at the end of a line)
is not treated as a line separator/terminator.
When we invoke
read with the
that stops, and backslash becomes an ordinary character.
Here are some practical examples of the differences:
Without -r (default) __ With -r __
_Input_ f1 f2 f1 f2
A\B\\C AB\C A\B\\C
D\ E F D E F D\ E F (or f2="E" and x="F")
G\ (this doesn’t count as a line) G\
H GH H
So I’ve added
-r flags to my first version of my answer.
If you want to be able to handle
D\ E as a single word, don’t use