2

I'd like to merge two consecutive lines in a Windows text file using external bash environment like the one that for instance Cygwin or MobaXTerm provide.

I know there are similar questions asked and already set to solved but for some reason they don't work with my environment. Is it perhaps cos Windows is adding some invisible characters unrecognized by bash tools?

Here are the solutions from other similar questions which I already tried:

awk 'NR%2{a=$0;next}{print a","$0}' test.txt

grep "line"  test.txt |awk 'NR==0{prefix=$0;next} {print prefix, $0}'

sed '$!N;s/\n/,/' test.txt

Input I'm working with:

first line
second line
third line
fourth line
fifth line
sixth line

Expected result:

first line,second line
third line,fourth line
fifth line,sixth line

Actual result with any of the code I tried so far:

1)

➤ sed '$!N;s/\n/,/' test.txt

,second line

,fourth line

,sixth line

2)

➤ grep "line"  test.txt |awk 'NR==0{prefix=$0;next} {print prefix, $0}'

first line

second line

third line

 fourth line

 fifth line

 sixth line

Any help here will be greatly appreciated.

  • You showed expected output but not the input that would produce that output. [edit] your question to include the sample input for that output. Wrt `Is it perhaps cos Windows is adding some invisible characters unrecognized by bash tools` - probably, see https://stackoverflow.com/q/45772525/1745001 for how to identify and handle that. – Ed Morton May 22 '19 at 13:03
  • I do believe it's pretty obvious, isn't it? It's just 6 consecutive lines, ie. practically the same thing you see as my output with grep. But okay, I'll add it to the question since people seem to be missing it. – Martin Ledermann May 22 '19 at 13:05
  • 1
    Someone has to create an input file for us to test a potential solution against. The choices are: 1) you do it, or 2) everyone trying to help you does it. `1` makes much more sense than `2`. – Ed Morton May 22 '19 at 13:07
  • Yeah, I understand that, I'm just saying that the input file was already there. :) – Martin Ledermann May 22 '19 at 13:10

4 Answers4

1
$ cat -v file
first line^M
second line^M
third line^M
fourth line^M
fifth line^M
sixth line^M

With GNU awk for multi-char RS:

$ awk -v RS='\r?\n' -v OFS=',' 'NR%2{p=$0; next} {print p, $0}' file
first line,second line
third line,fourth line
fifth line,sixth line

With any awk:

$ awk -v OFS=',' '{sub(/\r$/,"")} NR%2{p=$0; next} {print p, $0}' file
first line,second line
third line,fourth line
fifth line,sixth line
Ed Morton
  • 188,023
  • 17
  • 78
  • 185
1

with GNU sed -

$: cat in
0
1
2
3
4
5
6
7
8
9

$: sed 'N; s/\r*\n/,/; p; d;' in # no dash-options needed
0,1
2,3
4,5
6,7
8,9

N; - add the next line to the current one
s/\r*\n/,/; - replace any CR's and the new line character with a comma
p; - print the result
d; - delete it so sed won't autoprint.

Same output with

$: sed -En 'N; s/\r?\n/,/; p;' in 

that's -Extended pattern matching, -no autoprint.

Paul Hodges
  • 13,382
  • 1
  • 17
  • 36
0

A very simple solution is to use paste:

cat > FILE <<EOF
first line
second line
third line
fourth line
fifth line
sixth line

Then:

▶ paste -d, - - < FILE
first line,second line
third line,fourth line
fifth line,sixth line

Further explanation:

  • The paste utility concatenates the corresponding lines of given input files. If - is specified for any the files, STDIN is used, and it is read circularly, one line at a time, for each instance of -.

  • The -d, tells paste to use , as the field separator, instead of the default tab character.

Alex Harvey
  • 14,494
  • 5
  • 61
  • 97
0

here is one awk solution

awk 'NR%2==1{x=$0;next}{print x "," $0}' file

the output

first line,second line
third line,fourth line
fifth line,sixth line
JBone
  • 1,724
  • 3
  • 20
  • 32