File1:
A
B
C
D
File2:
E
F
G
H
How would I get Output:
A
BF
C
DH
File1:
A
B
C
D
File2:
E
F
G
H
How would I get Output:
A
BF
C
DH
You could do:
paste -d '\n' file1 file2 | sed -n 'p;n;n;N;s/\n//p' > output
paste zips the two files, alternating one line of each, and sed prints the first, discards the second, appends the 4th to the 3rd and joins them and starts again with the next lines.
Or with GNU sed:
paste -d '\n' file1 file2 | sed '2~4d' | paste -sd '\n\0\n' - > output
Where sed just discards the 2nd out of every 4 lines from the output of the zipping paste and the second paste does the joining.
Or, still with GNU sed:
sed 'z;n' file2 | paste -d'\0' file1 - > output
Where sed zaps one line (in other sed implementations, you can use s/.*//) and gets and prints the next so we can paste it to file1.
$ awk '{ getline other <"file2" } { print $0 (FNR % 2 == 0 ? other : "") }' file1
A
BF
C
DH
For each line of file1 read, this awk script also reads a line from file2 and stores it in the variable other. It then proceeds to print the line from file1 concatenated with the variable other, or nothing if the line number is odd.
A shell loop that would do the same thing:
n=0
while IFS= read -r a; do
n=$(( n + 1 ))
IFS= read -r b <&3
[ "$(( n % 2 ))" -ne 0 ] && b=""
printf '%s%s\n' "$a" "$b"
done <file1 3<file2
This loop loops over file1, each line being read into $a, and for each iteration also reads a line from file2 (over file descriptor 3) into $b. If the number of lines read from file1 so far is odd, $b is set to an empty string. Both $a and $b are then printed.
Using paste and GNU sed, and assuming that neither file contains tab characters (as they don't do in the question):
paste file1 file2 | sed '1~2s/\t.*//;s/\t//'
The paste will produce the contents of the two files side by side, separated by a tab character. The sed expression would first delete everything from the tab onwards on odd lines, and then delete all remaining tab characters.
With the same assumptions, the whole sed command above could be replaced by awk:
paste file1 file2 | awk -F '\t' '{ print $1 (FNR % 2 == 0 ? $2 : "") }'
Use
awk 'NR==FNR {a[NR]=$0;next} FNR%2==0 { a[FNR]=a[FNR]$0} END{for ( i in a) print a[i]}' file1 file2
E.g.
#!/bin/bash
echo "a
B
C
D" > file1
echo 'aa
Gd
Er
Yu
Ee
Tt' > file2
awk 'NR==FNR {a[NR]=$0;next} FNR%2==0 { a[FNR]=a[FNR]$0} END{for ( i in a) print a[i]}' file1 file2
You will get output
a
BGd
C
DYu
Tt
With perl also it is possible to interleave the lines from two files in the desired manner. File 2 is on std in and file 1 is an argument to perl. The line from file2 is added alternately, then the newline is removed.
$ perl -pe '($_ .= ($,,$,.<STDIN>)[$|--]) =~ s/\n(?!\z)//' File1 < File2
using GNU sed, we may do it as follows:
sed -e 'R file2' file1 | sed -Ee 'N;N;N;s/\n.*(\n.*)\n/\1/'
wherein we emulate the paste via sed and then string the 4 lines together and manipulate in such a manner so as to delete the 2nd and remove the newline between the 3rd and 4th. Repeat this process for the next 4-line slot.
Assuming input is on files z7 z8:
paste -d'\0' <(cat z7) <(sed '1~2s/^.*$//' z8)
produces:
A
BF
C
DH
This first deletes all characters on odd lines, leaving the newline. The paste then concatenates the files using a NULL character (thanks to thread paste files without delimiter for the NULL tip).
This was on a system like:
OS, ker|rel, machine: Linux, 3.16.0-7-amd64, x86_64
Distribution : Debian 8.11 (jessie)
bash GNU bash 4.3.30
sed (GNU sed) 4.2.2
paste (GNU coreutils) 8.23
Best wishes ... cheers, drl