4

I have two files...

First File Input...

1 How many steps are in programming?
A.5
B.4
C.3
D.9
Ans.

2 How many stairs in room?
A.6
B.10
C.15
D.23
Ans.

Second File Input...

1. Who is outside of the room ?
A.john
B.Mary
C.Scot
D.Fery
Ans.

2. Which day is today ?
A.Mon
B.Wed
C.Friday
D.Sunday
Ans.

I need Output like...

1 How many steps are in programming?
A.5
B.4
C.3
D.9
Ans.

1. Who is outside of the room ?
A.john
B.Mary
C.Scot
D.Fery
Ans.

2 How many stairs in room?
A.6
B.10
C.15
D.23
Ans.

2. Which day is today ?
A.Mon
B.Wed
C.Friday
D.Sunday
Ans.

I am using command ...

sed -e '/Ans./{r file2' -e:n -e 'n;bn' -e \} file1

But the second o/p from 2 is reversed

1 How many steps are in programming?
A.5
B.4
C.3
D.9
Ans.

1. Who is outside of the room ?
A.john
B.Mary
C.Scot
D.Fery
Ans.

**2. Which day is today ?**
A.Mon
B.Wed
C.Friday
D.Sunday
Ans.

**2 How many stairs in room?**
A.6
B.10
C.15
D.23
  • If you're happy with one or several of the answers, upvote them. If one is solving your issue, accepting it would be the best way of saying "Thank You!" :-) – Kusalananda Oct 12 '17 at 19:34
1

You can get pretty close using awk in paragraph mode, with getline (it doesn't quite preserve the record separators - you may be able to do better with GNU awk, using the RT variable):

$ awk 'BEGIN{RS=""; ORS="\n\n"} {print; if( (getline < "second") > -1) print}' first
1 How many steps are in programming?
A.5
B.4
C.3
D.9
Ans.

1. Who is outside of the room ?
A.john
B.Mary
C.Scot
D.Fery
Ans.

2 How many stairs in room?
A.6
B.10
C.15
D.23
Ans.

2. Which day is today ?
A.Mon
B.Wed
C.Friday
D.Sunday
Ans.
  • its Working fine – Pawan Oct 9 '17 at 16:56
  • 1
    @Pawan Then why not accept this as an answer? – Philippos Oct 11 '17 at 7:43
  • @Pawan Good! If this solves your issue, please consider accepting the answer. – Kusalananda Oct 12 '17 at 19:36
1

You may achieve this with awk

awk -v RS='' '
    FNR == NR { block[NR] = $0; next }
    { print block[FNR] "\n\n" $0 "\n" }
' file1 file2

Blocks are separated by empty lines (RS=''). First line (FNR == NR) stores the blocks of file1 into the block array. For each block of file2, the second line prints the corresponding block in file1 then the block in file2.

Notes:

  • I'm assuming each file contains the same number of text blocks
  • For a proper formatting, ensure that the last block in each file is followed by en empty line.

If there is a different number of blocks in each file, here is a (admittedly hackish) solution using bash and GNU tools sed and paste:

paste -z -d '\n' <(sed -z 's/\n\n/\n\x00/g' file1) <(sed -z 's/\n\n/\n\n\x00/g' file2)

The sed commands append a zero byte to the block separators (empty lines). paste uses that zero byte as a delimiter and prints each block in turn, separating them with a new line.

0

Here is a script and output to illustrate the use of standard utility paste for this. First the simple case of pasting lines alternately from 2 separate files. Then all we need to do is make the paragraph, stanza input into a single line and use the paste method. The gathering of lines in a group into a "superline" is done with a short perl script that transforms each line by replacing the newline with some other character, in this case "@". Then we paste, and then we separate the superlines back into paragraphs. This is a useful tehcnique because most *nix commands operate at the line level. It is useful enough, in fact, that at our shop we have a utility "masuli" to make such superlines.

The data files are smaller in the interest of conserving visual space.

#!/usr/bin/env bash

# @(#) s1       Demonstrate modular approach to multi-line data, perl, paste.

# Utility functions: print-as-echo, print-line-with-visual-space, debug.
# export PATH="/usr/local/bin:/usr/bin:/bin"
LC_ALL=C ; LANG=C ; export LC_ALL LANG
pe() { for _i;do printf "%s" "$_i";done; printf "\n"; }
pl() { pe;pe "-----" ;pe "$*"; }
em() { pe "$*" >&2 ; }
db() { ( printf " db, ";for _i;do printf "%s" "$_i";done;printf "\n" ) >&2 ; }
db() { : ; }
C=$HOME/bin/context && [ -f $C ] && $C paste perl

pl " Input data file data[12]:"
 head data[12]

pl " Results, proof of concept:"
paste -d'\n' data[12]

pl " Input data files data[34]:"
head data[34]

pl " Results, collect, paste, separate:"
paste -d'\n' <( perl -00 -wp -e 's/\n/@/g;s/@@/=\n/' data3 ) \
             <( perl -00 -wp -e 's/\n/@/g;s/@@/=\n/' data4 ) |
tee f1 |
perl -000 -wp -e 's/@/\n/g;s/=/\n\n/g;s/\n\n\n/\n\n/g'

exit 0

producing:

$ ./s1

Environment: LC_ALL = C, LANG = C
(Versions displayed with local utility "version")
OS, ker|rel, machine: Linux, 3.16.0-4-amd64, x86_64
Distribution        : Debian 8.9 (jessie) 
bash GNU bash 4.3.30
paste (GNU coreutils) 8.23
perl 5.20.2

-----
 Input data file data[12]:
==> data1 <==
1.1
1.2

==> data2 <==
2.1
2.2

-----
 Results, proof of concept:
1.1
2.1
1.2
2.2

-----
 Input data files data[34]:
==> data3 <==
3.1a
3.1b

3.2a
3.2b


==> data4 <==
4.1a
4.1b

4.2a
4.2b


-----
 Results, collect, paste, separate:
3.1a
3.1b

4.1a
4.1b

3.2a
3.2b

4.2a
4.2b

Best wishes ... cheers, drl

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.