Joining especific lines in "2n" lines file

Hi to everybody.

I have a "2n" lines file. I would like to create a
new file with only "n" lines, each line in the new
file formed by the proper odd line of the old file
joined with the following even line (separated by
a space) of the old file. I'd prefer using sed or
bash .

-example-

Old file:

line-01
line-02
line-03
line-04

...

New file:

line01 line02
line03 line04

...

Thank you in advance.

felino
(Cuban, Lousy Internet, New to this site)

Welcome to the forum.

Does it HAVE to be sed or bash ?

awk 'ORS=NR%2?"\t":"\n"' file
line-01    line-02
line-03    line-04

OK, sed is possible, and simple, too:

sed 'N; s/\n/\t/' file
line-01    line-02
line-03    line-04
1 Like

Or can it be paste?

paste - - < file

--
Note: \t in the replacement part of the "s" command is GNU sed only. For regular sed use actual TAB characters (CTRL-V TAB).

2 Likes

Or bash :

while read L1; do read L2; echo $L1 $L2; done < file
line-01 line-02
line-03 line-04
1 Like

To do it in shell it is best to turn off field splitting (that also removes leading and trailing whitespace) and other interpretations by the shell:

  • switch off splitting IFS (IFS=)
  • ignore \ characters in the input ( -r option)
  • protect variables against field splitting when printing (use double quotes)
  • protect against special characters when printing (use printf instead of echo )
while IFS= read -r L1 
do
  IFS= read -r L2
  printf "%s\t%s\n" "$L1" "$L2"
done < file

I want to thank to Mr RudiC
and Mr. Scrutinizer for their
precise and accurate answers
to my problem.

And to Mr. RudiC for correcting
my improper post.

Thank you again.

Felino
(Cuban, Lousy Internet, New to this site)