Perl formatting.


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Perl formatting.
# 1  
Old 03-02-2012
Perl formatting.

I have a file which has following data:

Quote:
693t622, ZOMBIAC SERVICES, , 3267, N
985g367, DRUMBASH, , 2376, Y, 457
983y106, STEFFI, , 2344, N, 2345, B
And I want the following out of it:

Quote:
693t622, ZOMBIAC SERVICES, , 3267
985g367, DRUMBASH, , 2376
983y106, STEFFI, , 2344
So basically I want only the first four command separated fields and strip off the rest. How do I do it in perl?
I want only perl code and no shell code.

I can split it and try to do something, but the incoming data can have any number of comma separated fields so was thinking of a more better solution.

Thanks!!
# 2  
Old 03-02-2012
Try like...
Code:
perl -a -F, -n -e 'print join(q{,}, $F[0],$F[1],$F[2],$F[3]).qq{\n}' test.txt

# 3  
Old 03-02-2012
Code:
$
$ cat f63
693t622, ZOMBIAC SERVICES, , 3267, N
985g367, DRUMBASH, , 2376, Y, 457
983y106, STEFFI, , 2344, N, 2345, B
$
$ # Using autosplit
$
$ perl -F, -plane '$_=join(",",@F[0..3])' f63
693t622, ZOMBIAC SERVICES, , 3267
985g367, DRUMBASH, , 2376
983y106, STEFFI, , 2344
$
$ # Using regular expression
$
$ perl -plne 's/([^,]+(,[^,]+){3}).*$/$1/' f63
693t622, ZOMBIAC SERVICES, , 3267
985g367, DRUMBASH, , 2376
983y106, STEFFI, , 2344
$
$

tyler_durden
These 2 Users Gave Thanks to durden_tyler For This Post:
# 4  
Old 03-27-2012
Dosent seem to work with the following:

Code:
 
$ echo "60000005,MAT CON DEPT-DB,,1,N" | perl -plne 's/([^,]+(,[^,]+){3}).*$/$1/'
60000005,MAT CON DEPT-DB,,1,N

# 5  
Old 03-27-2012
Try:
Code:
perl -plne 's/([^,]*(,[^,]*){3}).*/$1/'


Last edited by Scrutinizer; 03-27-2012 at 09:22 AM.. Reason: Removed superfluous $ sign
This User Gave Thanks to Scrutinizer For This Post:
# 6  
Old 03-27-2012
Thanks Scrutinizer. Could you please explain the logic behind it how it works?
# 7  
Old 03-27-2012
I changed the + in durden tyler's suggestion from + (1 or more) to * (0 or more). It means keep 0 or more occurrences of any character but a comma ( [^,] ) followed by 3 times: the combination of a comma followed by 0 or more occurrences of any character but a comma . This is stored in $1. Replace all this and the rest of the line (.*) by $1 which contains what was previously stored...
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Perl array formatting

Experts, I'm having problems with a portion of my perl script. I have a perl array that contains the following: Port1,circuit3,2;4 Port2,circuit1,9;6 Port3,circuit2,12;5 Port4,circuit4,11;10;3 Port5,circuit7,8;7;1 I'm trying to produce the following output: (3 Replies)
Discussion started by: timj123
3 Replies

2. UNIX for Advanced & Expert Users

Help with Formatting date in Perl

Good day. I am trying to change the output on my date so that I can get the month as a numeric value. TODAY=`perl -e 'print localtime(time()) . "\n" '` Here are the results from the above perl statement Thu Feb 11 13:16:40 2016 I am not sure of the syntax to get a date more like... (2 Replies)
Discussion started by: ziggy6
2 Replies

3. Shell Programming and Scripting

Perl syntax for formatting columns and text

Dear all, Pzl let me know what is the syntax for converting the columns format to text as i have lots of values for but when i put these values in xls sheet the values are automatically converted to and one more question i have is how to call values from shell script into perl script eg. ... (3 Replies)
Discussion started by: sagar_1986
3 Replies

4. UNIX for Dummies Questions & Answers

Explain perl formatting commands

Could you please tell the meaning of Below mentioned perl script lines: format OWNER_TOTAL = @< Owner Code @<<<<<<total: @#######.## @#######.## @#######.## @#######.## @#######.## @#######.## @#######.## @#######.## @#######.## @#######.## @###### #.## $prev_cust_type, $prev_owner_code,... (2 Replies)
Discussion started by: vinothrajan55
2 Replies

5. Shell Programming and Scripting

date formatting in perl

my code: $dateformat = yyyy_mm_dd if($dateformat =~ m/yyyy/i) { print ("found"); $yyyy = strftime "%Y", localtime; print ("year is $yyyy\n"); $dateformat =~ s/yyyy/$yyyy/; print ("new date format is $dateformat\n"); } elsif($dateformat =~ m/yy/i) { print ("found"); $yy = strftime "%y",... (4 Replies)
Discussion started by: irudayaraj
4 Replies

6. Shell Programming and Scripting

perl ssh2 output formatting

Hi Experts, I am using following format for Perl SSH2 commands. #!/usr/bin/perl -w use Net::SSH::Perl; use POSIX; use Sys::Hostname; use Term::ANSIColor qw(:constants); use strict; #my $host="10.128.0.214"; my @nodeip = ("10.128.0.214","10.128.0.215") ; my %node =... (7 Replies)
Discussion started by: mtomar
7 Replies

7. Shell Programming and Scripting

Formatting Perl Output

Hi, I am getting the output from a PERL program but the output is all on different lines. My code is printf hOUT "%s\t%s\t%s\t%s\t%s\t%s",$M1_CTR, $M2_CTR, $M3_CTR, $M4_CTR, $M5_CTR, $M6_CTR; Example: My report (which I send via email) should look like this Area 1 Area 2 ... (4 Replies)
Discussion started by: nurani
4 Replies

8. Shell Programming and Scripting

PERL: Modify Excel cell formatting

Hi, I would like to modify an existing excel cell format to a custom format. Is there a way to do it with out writing into a new excel file. In-place editing? Thanks (0 Replies)
Discussion started by: sandeep78
0 Replies

9. Shell Programming and Scripting

Text formatting in Perl.

Hiho, I've got a variable $sth = `du -sh /home/$whoami`; where $whoami is actually logged user. I want to have only the size of this directory eg. 2,7G. I know its lame, but anyway.. how to do it? (2 Replies)
Discussion started by: fenox
2 Replies

10. Shell Programming and Scripting

text file formatting by perl

have a simple text file as input.i have to print that file in paragraph format.whenevr it finds "\n" in the input text it should start printing in next paragraph in output file.also a fixed amount of space should be given before start writing in every paragraph. the input and output file format... (5 Replies)
Discussion started by: avik1983
5 Replies
Login or Register to Ask a Question