Reputation: 3542
I have a file which has the data in the following manner :
1
2
3
end
4
5
6
I want an output in the form :
1 2 3
4 5 6
here the string "end" represents the record separator. I am new to UNIX and used Awk to achieve this, I am sure there can be much easier and a better way. Here is my attempt:
awk 'BEGIN {RS="end\n"; FS="\n"} { print $1 "\t" $2 "\t" $3 "\t" $4'} atestfile.awk
Upvotes: 2
Views: 535
Reputation: 53478
I'd probably use perl
(and yes, I know that's not quite what you asked, but given it's standard on Unix, I'll offer it anyway).
use strict;
use warnings;
#read STDIN or a filename designated on command line.
while (<>) {
#replace linefeeds with spaces.
s/\n/ /g;
#Check for presence of "end"
if (m/end/) {
#insert linefeed
print "\n";
}
else {
print;
}
}
Upvotes: 0
Reputation: 74615
Personally I'd write it like this:
awk -v RS='end' -v OFS='\t' '{$1=$1}1' file
The differences are that I've used -v
to specify the record separator RS
and the output field separator OFS
. I've also used $1=$1
to make awk "touch" each record (so that the format is changed to use OFS
) and 1
at the end as a shorthand for {print}
(since 1
is always True and the default action of awk
on True is to print the record).
Upvotes: 6