Perl : split flat line's to $


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Perl : split flat line's to $
# 1  
Old 07-20-2007
Perl : split flat line's to $

Hi,

below is the software list of lines I have,

MozillaSrc 1.7.8.00.00 Mozilla 1.78 Source Distribution
NFS B.11.11 ONC/NFS; Network-File System,Information Services,Utilities
NParProvider B.11.11.01.04.01.01 nPartition Provider
NPartition A.01.02 Enhanced NPartition Commands
Networking B.11.11 HP-UX_Lanlink_Product
NisLdapServer B.04.00.02 The NIS/LDAP Gateway (ypldapd)

I would like to split this as follows.

"MozillaSrc" "1.7.8.00.00" "Mozilla 1.78 Source Distribution"

I'm unable to use substr - because the field's are of varying length. btw, the above lines are 'swlist' output from hpux box.

any help could be appreciated, thanks.
# 2  
Old 07-20-2007
Code:
perl -e ' while (<>) { chomp; my @arr = split(/ /); print "\"$arr[0]\" \"$arr[1]\" \""; for ( my $i = 2; $i <= $#arr; $i++ ) { print " $arr[$i]" }; print " \"\n" } ' filename

# 3  
Old 07-20-2007
Code:
#!/usr/bin/perl
while( <> )
{
        chomp;
        if ( / ([^\s]+) / ) {
                print "($`)($1)($')\n";
        }
}

# 4  
Old 07-20-2007
print line should have been as,

Code:
 print "\"$`\" \"$1\" \"$'\"\n";

Smilie
# 5  
Old 07-20-2007
A sed version:
Code:
sed 's/ /|/2;s/\(.*\) \(.*\)|\(.*\)/"\1" "\2" "\3"/' input_file

# 6  
Old 07-21-2007
thank you for all your inputs,

it did work, if the lines are as below (one space between FEATURE11-11<>B.11.11.0209.5<>Feature Enablement Patches for HP-UX 11i, Sept 2002)

but the actual line had some whitespace's in between (which were lost while pasting)

I have attached the line's as text file.

Purpose: 'swlist' gives software pkgs on a hp box, which has 3 fields as NAME, REVISION, INFO, I need to put as example -

$name = FEATURE11-11
$revision = B.11.11.0209.5
$info = Feature Enablement Patches for HP-UX 11i, Sept 2002

code for solaris works fine, as it has fixed string, hpux output is not fixed.

open(INFO, "pkginfo|") or die "an error occured: $!";
while (<INFO>) {
$CATEGORY = substr $_, 0, 11;
$PKGINST = substr $_, 12, 32;
$NAME = substr $_, 45, 100;

<process a few things here..>
}
# 7  
Old 07-21-2007
try this,

Code:
perl -e ' while (<>) { chomp; s/ */ /; my @arr = split(/  +/); print "\"$arr[0]\" \"$arr[1]\" \""; for ( my $i = 2; $i <= $#arr; $i++ ) { print " $arr[$i]" }; print " \"\n" } ' filename

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Perl split and add new line

Hi All, I find the below code printing the output of a particular field in same line, which i want it to be printed on a new line. ---CODE START--- foreach $defectRec (@outLs) { my($def_id,$status,$files_mod) = split(/,/, $defectRec); print "DEFECT ID: $def_id, Status:... (3 Replies)
Discussion started by: nmattam
3 Replies

2. Shell Programming and Scripting

Get line number in flat file

Hi, Is there a way to find out the line number from where the data starts? like if the data contains column header, irrespective of the text in the column header we should get the line number from which contains the column header. I am sorry if I haven't explained the problem clearly. ... (8 Replies)
Discussion started by: kedar_laveti
8 Replies

3. Shell Programming and Scripting

gnuplot flat line graph

Hi, I'm not able to find a solution because I cant find the exact keyword for this. I wanna make a graph like ive shown in the attachment. Could someone please share ideas on how to do this. Thanks! (3 Replies)
Discussion started by: jamie_123
3 Replies

4. Shell Programming and Scripting

perl: Read array from a flat file

Hello Guru's I want to read an array into a flatfile Please let me know how to do the same So far this the below code use strict; use warnings; open (my $data , '<', $ARGV)|| die "could not open $ARGV:\n$!"; my @array=(<$data>); my @sorted=sort... (8 Replies)
Discussion started by: Pratik4891
8 Replies

5. Shell Programming and Scripting

Sort flat file by 3rd column in perl

Hello Guys I want to sort a flat file by the third column (numeric ) and store it in some other name I/P 9924873|20111114|00000000000013013|130|13|10/15/2010 12:36:22|W860944|N|00 9924873|20111114|00000000000013009|130|09|10/15/2010 12:36:22|W860944|N|00... (12 Replies)
Discussion started by: Pratik4891
12 Replies

6. Shell Programming and Scripting

[Perl] Split lines into array - variable line items - variable no of lines.

Hi, I have the following lines that I would like to see in an array for easy comparisons and printing: Example 1: field1,field2,field3,field4,field5 value1,value2,value3,value4,value5Example 2: field1,field3,field4,field2,field5,field6,field7... (7 Replies)
Discussion started by: ejdv
7 Replies

7. Shell Programming and Scripting

Split positional flat file.

Hi, I need to split positional flat file, based on value at position 43-45.( in red "410") Example: 12345678907886421689 200920184820410200920020092002007 12345678907886421689 200920184820411200920020092002007 12345678907886421689 200920184820411200920020092002007... (6 Replies)
Discussion started by: meetmedude
6 Replies

8. Shell Programming and Scripting

Split a line on positions before reading complete line

Hi, I want to split before reading the complete line as the line is very big and its throwing out of memory. can you suggest. when i say #cat $inputFile | while read eachLine and use the eachLine to split its throwing out of memory as the line size is more than 10000000 characters. Can you... (1 Reply)
Discussion started by: vijaykrc
1 Replies

9. UNIX for Dummies Questions & Answers

how to remove the first line from a flat file ?

Hi, I want to remove the first line from a flat file using unix command as simple as possible. Can anybody give me a hand ? Thanks in advance. xli (21 Replies)
Discussion started by: xli
21 Replies

10. Shell Programming and Scripting

remove specific lines from flat file using perl

Hi, Here is wat im looking for.. i have a flat file which looks like this.. 00 * * * * .. .. * * text text text COL1 COL2 ----- ----- 1 a (12 Replies)
Discussion started by: meghana
12 Replies
Login or Register to Ask a Question