Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

perl: print line for each 10 data

Status
Not open for further replies.

diera

Programmer
Mar 21, 2011
28
DE
Hi,

i have 1200 line of data as below:

user1 date time @Glebe2037 Ha! but of course.....
user2 date time @aerynea It mostly missed Fort Collins
user3 date time Lansons is 20 years old this year
....

current output:
"@Glebe2037 Ha! but of course.....","10001","@aerynea It mostly missed Fort Collins","10002","Lansons is 20 years old this year","10003",............."11200".

i need your help to organize this output which is they will print \n everytime the id increase by 10.

require output:
"aaaaaaa","10001",..............."10010"
"bbbbbb","10011",..............."10020"

Code:
#! /usr/bin/perl -w

use strict;

open( my $fh, '<', 'tweetdata.txt' )
  or die "Ops sorry $!\n";

open MYFILE, ">output.csv";
select MYFILE;

my $lineno = 10001;

while (<$fh>) {
    /^(\S+)\t([^\t]+)\t(.*)$/
      or die "Invalid input:  $_";
    my $text = $3;
    print "\"$text\",\"";

    #print "$text\",\"";
    print $lineno++;
    print "\",";
}

Any help much appreciated.
 
You're question is unclear to me. It would be helpful if you supplied fake data that works with the code you provided, along with an example of how you want the data outputted.

Nevertheless, if all you want is to group sets of 10 lines in the output, then just add an incrementer.

Code:
my $i = 0;

while (<DATA>) {
	chomp;
	print "$_,";
	
	$i = ($i + 1) % 4;
	print "\n" if !$i;
}

__DATA__
line1
line2
line3
line4
line5
line6
line7
line8
line9
line10
line11
line12
line13
 
my real data

1LovellsLager 2009-07-10 12:46:50 @Glebe2037 Ha! but of course.....
3Kevin_Curry 2009-04-17 23:43:50 @aerynea It mostly missed Fort Collins, so far at least, I'll take the blame so long as the weather here stays relatively nice.
4therealprmoment 2009-09-16 09:50:45 Lansons is 20 years old this year but even better is this pic of CEO Tony Langham and Chairman Clare Parsons from 1989 5purplefroggie 2009-07-02 19:48:51 Just made @auberginefedora a Top Friend on Power Twitter
6JonnieMoney 2009-05-13 13:42:36 Unity is power,to assist the powerless,to aide & abett the ,forgotten ,to reach the\
7SearchRevLLC 2009-10-08 15:34:43 On Saturday, October 10th, 2009 the AdWords system will be unavailable from approximately 10AM to 2PM PDT, for maintenance.
8AMeLane 2009-09-02 02:57:04 @nachojohnny I LOVE that song. Def in my top 10!!
9mrwinkythepug 2009-09-07 16:28:44 Eugh. Liason Pysch shifts are the worst, though I am currently holed up in a mcdonalds awaiting 'the call'
10snapsummit 2009-08-25 00:28:36 wanna come to #finance4founders wednesday? Gonna be awesome! ping me
12GabbleLink2 2009-09-29 22:06:21 Compare the Top 10 Web Hosting. All offer Free Setup & Domain Name. Visit 13RobertStover 2009-07-11 03:16:41 @socommoncents And I look forward to some common cents! I mean sense...
14JaneRocks 2009-10-12 13:59:51 By the way 2009 is also a great year for white wine in the Bordeaux and the good news is we can drink that next year!!
15alans 2007-09-29 03:21:08 ...make that the west end
16mlmmoney_twit 2009-10-03 22:45:44 BRAND NEW LAUNCH!......POWER BAR CLUB...Mega Fortunes will be made...POWERBARCLUB... 17education_rec 2009-05-26 00:37:37 Job: Special Needs Education Tutor (Part Time Evening) Wexford, Swords, Portlaoise: Special Needs Education .. 19DayAirCU 2009-09-11 13:36:09 Financial Tip Friday: ""Newlyweds, devise a realistic budget! Consider even routine purchases."" 20UKComedyNews 2009-09-28 18:55:13 Ricky Gervais interview in The Guardian: 'Before The Office I never tried hard at anything': 21jeremymullman 2009-09-17 17:41:11 Burger King CMO takes leave. He recently promised PO'd BK franchisees he'd ""tone down"" ads. 22savethemanatee 2009-07-21 13:58:56 Chesapeake Bay manatee is identified as Ilya. Read more, see photos: 23Cobrakiss67 2009-08-25 22:31:51 I'm tired of depressed/miserable people tryin' 2 make me depressed/miserable...

current output
"@Glebe2037 Ha! but of course.....","10001","@aerynea It mostly missed Fort Collins, so far at least, I'll take the blame so long as the weather here stays relatively nice.","10002","Lansons is 20 years old this year but even better is this pic of CEO Tony Langham and Chairman Clare Parsons from 1989 made @auberginefedora a Top Friend on Power Twitter","10004","Unity is power,to assist the powerless,to aide & abett the ,forgotten ,to reach the\","10005","On Saturday, October 10th, 2009 the AdWords system will be unavailable from approximately 10AM to 2PM PDT, for maintenance.","10006","@nachojohnny I LOVE that song. Def in my top 10!!","10007","Eugh. Liason Pysch shifts are the worst, though I am currently holed up in a mcdonalds awaiting 'the call'","10008","wanna come to #finance4founders wednesday? Gonna be awesome! ping me","10009","Compare the Top 10 Web Hosting. All offer Free Setup & Domain Name. Visit And I look forward to some common cents! I mean sense...","10011","By the way 2009 is also a great year for white wine in the Bordeaux and the good news is we can drink that next year!!","10012","...make that the west end","10013","BRAND NEW LAUNCH!......POWER BAR CLUB...Mega Fortunes will be made...POWERBARCLUB... Special Needs Education Tutor (Part Time Evening) Wexford, Swords, Portlaoise: Special Needs Education .. Tip Friday: ""Newlyweds, devise a realistic budget! Consider even routine purchases."" Gervais interview in The Guardian: 'Before The Office I never tried hard at anything': King CMO takes leave. He recently promised PO'd BK franchisees he'd ""tone down"" ads. Bay manatee is identified as Ilya. Read more, see photos: tired of depressed/miserable people tryin' 2 make me depressed/miserable...","10020"

what i want is
line1: "@Glebe2037 Ha! but of course.....","10001","@aerynea It mostly missed Fort Collins, so far at least, I'll take the blame so long as the weather here stays relatively nice.","10002","Lansons is 20 years old this year but even better is this pic of CEO Tony Langham and Chairman Clare Parsons from 1989 made @auberginefedora a Top Friend on Power Twitter","10004","Unity is power,to assist the powerless,to aide & abett the ,forgotten ,to reach the\","10005","On Saturday, October 10th, 2009 the AdWords system will be unavailable from approximately 10AM to 2PM PDT, for maintenance.","10006","@nachojohnny I LOVE that song. Def in my top 10!!","10007","Eugh. Liason Pysch shifts are the worst, though I am currently holed up in a mcdonalds awaiting 'the call'","10008","wanna come to #finance4founders wednesday? Gonna be awesome! ping me","10009","Compare the Top 10 Web Hosting. All offer Free Setup & Domain Name. Visit
line2: "@socommoncents And I look forward to some common cents! I mean sense...","10011","By the way 2009 is also a great year for white wine in the Bordeaux and the good news is we can drink that next year!!","10012","...make that the west end","10013","BRAND NEW LAUNCH!......POWER BAR CLUB...Mega Fortunes will be made...POWERBARCLUB... Special Needs Education Tutor (Part Time Evening) Wexford, Swords, Portlaoise: Special Needs Education .. Tip Friday: ""Newlyweds, devise a realistic budget! Consider even routine purchases."" Gervais interview in The Guardian: 'Before The Office I never tried hard at anything': King CMO takes leave. He recently promised PO'd BK franchisees he'd ""tone down"" ads. Bay manatee is identified as Ilya. Read more, see photos: tired of depressed/miserable people tryin' 2 make me depressed/miserable...","10020"

my code now, generate the output in single line. i want to split it as i mentioned above. i'm sorry, my english not good.

thanks
 
Code:
#! /usr/bin/perl -w

use strict;
use warnings;

#open( my $fh, '<', 'tweetdata.txt' )
#  or die "Ops sorry $!\n";

#open MYFILE, ">output.csv";
#select MYFILE;

my $i = 0;
my $lineno = 10001;

while (<DATA>) {
	/^(\S+)\t([^\t]+)\t(.*)$/
	  or die "Invalid input:  $_";
	my $text = $3;
	print qq{"$text","} . $lineno++ . qq{",};
	
	$i = ($i + 1) % 10;
	print "\n" if !$i;
}

__DATA__
1LovellsLager	2009-07-10 12:46:50	@Glebe2037 Ha! but of course.....
3Kevin_Curry	2009-04-17 23:43:50	@aerynea It mostly missed Fort Collins, so far at least, I'll take the blame so long as the weather here stays relatively nice.
4therealprmoment	2009-09-16 09:50:45	Lansons is 20 years old this year but even better is this pic of CEO Tony Langham and Chairman Clare Parsons from 1989 [URL unfurl="true"]http://bit.ly/2lERPP[/URL]
5purplefroggie	2009-07-02 19:48:51	Just made @auberginefedora a Top Friend on Power Twitter
6JonnieMoney	2009-05-13 13:42:36	Unity is power,to assist the powerless,to aide & abett the ,forgotten ,to reach the\
7SearchRevLLC	2009-10-08 15:34:43	On Saturday, October 10th, 2009 the AdWords system will be unavailable from approximately 10AM to 2PM PDT, for maintenance.
8AMeLane	2009-09-02 02:57:04	@nachojohnny I LOVE that song. Def in my top 10!!
9mrwinkythepug	2009-09-07 16:28:44	Eugh. Liason Pysch shifts are the worst, though I am currently holed up in a mcdonalds awaiting 'the call'
10snapsummit	2009-08-25 00:28:36	wanna come to #finance4founders wednesday?  Gonna be awesome! ping me
12GabbleLink2	2009-09-29 22:06:21	Compare the Top 10 Web Hosting. All offer Free Setup & Domain Name. Visit [URL unfurl="true"]http://short.to/s2an[/URL]
13RobertStover	2009-07-11 03:16:41	@socommoncents And I look forward to some common cents! I mean sense...
14JaneRocks	2009-10-12 13:59:51	By the way 2009 is also a great year for white wine in the Bordeaux and the good news is we can drink that next year!!
15alans	2007-09-29 03:21:08	...make that the west end
16mlmmoney_twit	2009-10-03 22:45:44	BRAND NEW LAUNCH!......POWER BAR CLUB...Mega Fortunes will be made...POWERBARCLUB... [URL unfurl="true"]http://hopurl.com/70360[/URL]
17education_rec	2009-05-26 00:37:37	Job: Special Needs Education Tutor (Part Time Evening) Wexford, Swords, Portlaoise: Special Needs Education .. [URL unfurl="true"]http://twurl.nl/5zgfo3[/URL]
19DayAirCU	2009-09-11 13:36:09	Financial Tip Friday: ""Newlyweds, devise a realistic budget! Consider even routine purchases."" [URL unfurl="true"]http://bit.ly/tXwSQ[/URL]
20UKComedyNews	2009-09-28 18:55:13	Ricky Gervais interview in The Guardian: 'Before The Office I never tried hard at anything': [URL unfurl="true"]http://bit.ly/23bt1[/URL]
21jeremymullman	2009-09-17 17:41:11	Burger King CMO takes leave. [URL unfurl="true"]http://bit.ly/DIeRv.[/URL] He recently promised PO'd BK franchisees he'd ""tone down"" ads. [URL unfurl="true"]http://tinyurl.com/l3buxg[/URL]
22savethemanatee	2009-07-21 13:58:56	Chesapeake Bay manatee is identified as Ilya. Read more, see photos: [URL unfurl="true"]http://bit.ly/kVauh[/URL]
23Cobrakiss67	2009-08-25 22:31:51	I'm tired of depressed/miserable people tryin' 2 make me depressed/miserable...

also, you regex can be simplified to just

Code:
/^(.*?)\t(.*?)\t(.*)$/
	  or die "Invalid input:  $_";

Or even better would be to just use split

Code:
my ($name, $date, $tweet) = split /\t/, $_, 3;
 
thank you very much miller. finally it works as i want.
you also teach me alot: make code n use perl::tidy.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top