Perl Split for text in file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Perl Split for text in file
# 1  
Old 02-03-2010
Perl Split for text in file

Hi all

I have written Perl script to swap the strings in the second a third column from a text file.

My input file format is :


Code:
the|empty|the|det lake|empty|lake|conj_and was|empty|was|auxpass drained|empty|drained|conj_and birds|empty|bird|s|nn


The expected output file format is :
Code:
the|the|empty|det lake|lake|empty|conj_and was|was|empty|auxpass drained|drained|empty|conj_and birds|bird|s|nn

There are four strings in each text units separated by "|". In some text units there is string "empty" and for some it' s not there. For every occurrence of string "empty" in the second column, I want to swap with the third column.

I am unable to sort out the problem with my script below.

Code:
#!/usr/bin/perl


open FILE1, '<', "My_text.txt" or die "$!";

while (<FILE1>) {
        my @words = grep {$_ ne ' ' and $_ ne '' and $_ ne "\n" } split /([,.()]|(?: ))/;
        while (my $word = shift @words) {
        my $empt = 0;

                        my ($col_1, $col_2, $col_3, $col_4) = split('|',$word);
                        if ($col_2 eq 'empty') {
                                $empt = 0;
                                print "col_3|$col_2";
                                last;
                        } else {
                                $empt = 1;
                        }


        }
        print "\n";
}

close FILE1;


Thanks in advance.

Last edited by my_Perl; 02-03-2010 at 06:49 AM..
# 2  
Old 02-03-2010
use below:-

Code:
perl -nlaF'\|' -e '
if ($F[1] eq "empty" ) {$temp=$F[1] ; $F[1]=$F[2] ; $F[2]=$temp } ; 
$end=@F ; 
$"="\|" ; 
print "@F[0..$end]"  if (@F) ; 
eof and close ARGV
'  File1 File2 ....

SmilieSmilieSmilie
# 3  
Old 02-03-2010
Hi ahmad.diab

I am getting the following output:

Code:
the|the|empty|det lake|empty|lake|conj_and was|empty|was|auxpass drained|empty|drained|conj_and birds|empty|bird|s|nn

Means it works for the first text unit but not for the following units.
One more additional problem, please look at the last text unit (i.e., birds|empty|bird|s|nn) . There are five strings separated by "|". I want to remove the second string (i.e, empty) from this kind of unit so that it produces birds|bird|s|nn only.

I need help on this.
# 4  
Old 02-03-2010
after the below modification use below code:-

Code:
perl -nlaF'\|' -e '
if ($F[2] eq "empty" ) {
           $temp=$F[1] ;
           $F[1]=$F[2] ;
           $F[2]=$temp ;
           $end=@F ;
           $"="\|" ;
           print "@F[0..$end-5,$end-3..$end-1] "  if (@F) ;
               }
else { print $_ ; } ;
eof and close ARGV ;
' File.txt

# 5  
Old 02-03-2010
I am getting the output same as the input.
# 6  
Old 02-03-2010
code
Code:
perl -nlaF'\|' -e '
if ($F[2] eq "empty" ) {
           $temp=$F[1] ;
           $F[1]=$F[2] ;
           $F[2]=$temp ;
           $end=@F ;
           $"="\|" ;
           print "@F[0..$end-5,$end-3..$end-1] "  if (@F) ;
               } else { print $_ ; } ;
eof and close ARGV ;
'  input.txt


input.txt:-

Code:
the|the|empty|det lake|empty|lake|conj_and was|empty|was|auxpass drained|empty|drained|conj_and birds|empty|bird|s|nn

the|the|uuuuuuuu|det lake|empty|lake|conj_and was|empty|was|auxpass drained|empty|drained|conj_and birds|empty|bird|s|nn


output.txt:-

Code:
the|empty|the|det lake|empty|lake|conj_and was|empty|was|auxpass drained|empty|drained|conj_and birds|bird|s|nn 

the|the|uuuuuuuu|det lake|empty|lake|conj_and was|empty|was|auxpass drained|empty|drained|conj_and birds|empty|bird|s|nn


Iam getting the correct o/p , so where is the problem?????
# 7  
Old 02-03-2010
My input is :

Code:
the|empty|the|det lake|empty|lake|conj_and was|empty|was|auxpass drained|empty|drained|conj_and birds|empty|bird|s|nn

Expected output is:

Code:
the|the|empty|det lake|lake|empty|conj_and was|was|empty|auxpass drained|drained|empty|conj_and birds|bird|s|nn

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Split the text file into two

OS : RHEL 7.3 I have a file like below. I want to move (cut and paste) the first 7 lines of file1 to another file (file2). How can I do this ? In my real life scenario, I will be moving first 12 millions lines of file1 to file2 $ cat file1.txt 7369|SMITH |CLERK | ... (5 Replies)
Discussion started by: kraljic
5 Replies

2. Shell Programming and Scripting

How to target certain delimiter to split text file?

Hi, all. I have an input file. I would like to generate 3 types of output files. Input: LG10_PM_map_19_LEnd_1000560 LG10_PM_map_6-1_27101856 LG10_PM_map_71_REnd_20597718 LG12_PM_map_5_chr_118419232 LG13_PM_map_121_24341052 LG14_PM_1a_456799 LG1_MM_scf_5a_opt_abc_9029993 ... (5 Replies)
Discussion started by: huiyee1
5 Replies

3. Shell Programming and Scripting

Split a text file into multiple text files?

I have a text file with entries like 1186 5556 90844 7873 7722 12 7890.6 78.52 6679 3455 9867 1127 5642 ..N so many records like this. I want to split this file into multiple files like cluster1.txt, cluster2.txt, cluster3.txt, ..... clusterN.txt. (4 Replies)
Discussion started by: sammy777
4 Replies

4. Shell Programming and Scripting

PERL: split 2 part from singel file.

Hi, I would like to split single fine into two array .. Example: file.txt --------------Installation -------------------- #GXTOOL=GxTools-20130501.tar.gz GCSS=GExpLinux-BE-3700.0.12.37.tar.gz TOP=TOPLinux-BE-3700.0.6.21.tar.gz GHDER=GHDERLinux-BE-3700.0.6.20.tar.gz... (2 Replies)
Discussion started by: Mani_apr08
2 Replies

5. Shell Programming and Scripting

Perl : to split the tags from xml file

I do have an xml sheet as below where I need the perl script to filter only the hyperlink tags. <cols><col min="1" max="1" width="30.5703125" customWidth="1"/><col min="2" max="2" width="7.140625" bestFit="1" customWidth="1"/> <col min="3" max="3" width="32.28515625" bestFit="1"... (3 Replies)
Discussion started by: scriptscript
3 Replies

6. Shell Programming and Scripting

perl script to split the text file after every 4th field

I had a text file(comma seperated values) which contains as below 196237,ram,25-May-06,ram.kiran@xyz.com,204183,Pavan,4-Jun-07,Pavan.Desai@xyz.com,237107,ram Chandra,15-Mar-10,ram.krishna@xyz.com ... (3 Replies)
Discussion started by: giridhar276
3 Replies

7. Shell Programming and Scripting

Split text file

Hi all, I am very new to shell scripting and some help is greatly appreciated. I have 10 column based text files, i would like to split each of them into 6 files ; the 1st one having columns 1, 2 ,3,4 , the second one having columns 1,2,8,9 etc. Is there a way I could get 60 files out my... (3 Replies)
Discussion started by: shreymuk
3 Replies

8. Shell Programming and Scripting

How do I split file into pieces with PERL?

How do I split file into pieces with PERL? IE file.txt head 1 2 3 4 end head 5 6 7 8 9 end n so on (7 Replies)
Discussion started by: 3junior
7 Replies

9. Shell Programming and Scripting

how to get split output of a file, using perl script

Hi, I have file: data.log.1 ### s1 main.build.3495 main.build.199 main.build.3408 ###s2 main.build.3495 main.build.3408 main.build.199 I want to read this file and store in two arrays in Perl. I have following command, which is working fine on command prompt. perl -n -e... (1 Reply)
Discussion started by: ashvini
1 Replies

10. UNIX for Dummies Questions & Answers

Split text file by pages

Hello! Firts of all, I'm sorry for my English. My problem: I have text file with few Form Feed symbols (FF, ASCII code =12) inside (for example - some report, consists of some pages for printing). I want to split this text by pages - each page (until FF symbol) in single file. I... (2 Replies)
Discussion started by: ranri
2 Replies
Login or Register to Ask a Question