Whats a good way to group (by adding a new integer to the front of each line) pairs of lines, such that lines 1 & 2 are group 1, lines 3 & 4 are group 2, etc...
ex input:
A
B
C
D
etc...
ex output:
1A
1B
2C
2D
etc...
Whats a good way to group (by adding a new integer to the front of each line) pairs of lines, such that lines 1 & 2 are group 1, lines 3 & 4 are group 2, etc...
ex input:
A
B
C
D
etc...
ex output:
1A
1B
2C
2D
etc...
Hi
# cat a
A
B
C
D
E
# awk '{print i $0;}!(NR%2){i++}' i=1 a
1A
1B
2C
2D
3E
#
Guru.
loop through each line of the file using a counter variable but only increment the counter if the line count modulus 2 equals 0
here is a Perl example. you can port the logic to shell.
#!/usr/bin/perl
use strict;
use warnings;
my $counter=1;
my $line_count=0;
while(<DATA>) {
print "${counter}$_";
$counter++ if $line_count % 2;
$line_count++;
}
__DATA__
A
B
C
D
E
F
G
H
ruby -ne 'BEGIN{i=0};($.-1)%2==0 and i+=1; print "#{i} #{$_}"' file
awk '{print int(NR/2+0.5) $0}' file
awk '$0=int(i+=0.5)$0' i=0.5 file
awk 'sub(/^/,(NR%2)?++i:i)' file
awk '$0=((NR%2)?++i:i)$0' file
awk 'NR%2{++i}$0=i$0' file
awk '$0=(i+=NR%2)$0' file