Welcome to the Monastery PerlMonks

### Concatenate every 5 lines of input with a comma.

by jsh (Initiate)
 on Jul 09, 2009 at 19:17 UTC Need Help??
jsh has asked for the wisdom of the Perl Monks concerning the following question:

Hi guys. i have situation that i need to have below kind manipulation done by perl.
awk 'ORS=NR%5?",":"\n"'
[download]
i cannot figure it out. I need to have output like below, print 5 values comma separated and new line.
val1;val2;val3;val4;val5
val1;val2;val3;val4;val5
val1;val2;val3;val4;val5
etc...
[download]

Replies are listed 'Best First'.
Re: Concatenate every 5 lines of input with a comma.
by linuxer (Curate) on Jul 09, 2009 at 19:24 UTC

Look for $. and$\.

But I wonder how you produce values separated with ; (semicolon), when you used , (comma) in your code? ;))

#!/usr/bin/perl
# vi:ts=4 sw=4 et:
use strict;
use warnings;

while ( <DATA> ) {
chomp;
local $\ =$. % 5 ? ";" : "\n";
print;
}

__DATA__
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
[download]
As a one-liner:
perl -ple'$\ =$.%5 ? ";" : $/' infile > outfile [download] In-place: perl -i.bak -ple'$\ = $.%5 ? ";" :$/' file
[download]

If it wasn't a one-liner, I wouldn't use $\ while ( <DATA> ) { chomp; print$_, $.%5 ? ";" :$/;
}
[download]
Re: Concatenate every 5 lines of input with a comma.
by SuicideJunkie (Vicar) on Jul 09, 2009 at 19:28 UTC
That's not a comma separating them in your example...
But regardless of the sample data, what have you tried?
Have you tried printing five values and then a newline, and what did you have trouble with during that process?

It sounds like you've distilled a decent specification, although there is still the question of where the values are coming from, and what you need to do about values that do not come in multiples of five.
If you're reading whole lines from somewhere such as a file or STDIN, make sure you chomp() off the newline before printing them.

The monks can fry your fish, and they can give you some tips and some bait, but you still need to wake up and climb onto the boat.
Hi, sorry my explanation was bad. Comma or semi-colon does not matter :) My scripts seeks serial numbers through ssh connection, return value comes from foreach loop.
round 1; 123456
round 2; 123456
etc..

and i need to have output print like i said.
12345;12345;12345
12345;12345;12345
etc..

Thanks.
[download]
Hi, awesome guys, answer was above
while ( <DATA> ) {
chomp;
print $_,$.%5 ? ";" : $/; } [download] Thanks. Re: Concatenate every 5 lines of input with a comma. by ForgotPasswordAgain (Deacon) on Jul 09, 2009 at 19:24 UTC UPDATE: Oh, sorry, I'd only read the subject. I don't understand the body of your post. Original: perl -pe's/$/,/ if not$.%5' file [download] Not even close: $ seq 20 | perl -pe's/$/,/ if not$.%5'
1
2
3
4
5,
6
7
8
9
10,
11
12
13
14
15,
16
17
18
19
20,

[download]

Create A New User
Node Status?
node history
Node Type: perlquestion [id://778667]
Approved by toolic
help
Chatterbox?
 [Corion]: thezip: start "some title" path\to\that\ application, but that will open another console window [Corion]: thezip: If you want to confuse your users, use system(1, "that\\command" );, which will make Perl launch it in the background [Corion]: That will keep the console window open even though the user can't type into it anymore [thezip]: So I have a script that generates a log file. After script completion, I want tohave VIM open this logfile. [thezip]: i don't get the command line "back" until I close VIM. No what I want to happen... [thezip]: I currently don't have access to CYGWIN, else I'd just do a tail -f on the logfile.

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (11)
As of 2017-03-27 18:49 GMT
Sections?
Information?
Find Nodes?
Leftovers?
Voting Booth?
Should Pluto Get Its Planethood Back?

Results (321 votes). Check out past polls.