Perl regexp help


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Perl regexp help
# 1  
Old 01-18-2013
Power Perl regexp help

Hi,

I have file like below:
Code:
1|1212|34353|5fdf 6575||dfgdg sfsdf |afsf||4|aasfbc|~1213~~~~~
1|1212|34353|5fdf 6575||dfgdg sfsdf |affsf| |4|abc|~rwarw~~asa~~~123~312313
1|1212|34353|5fdf 6575||dfgdg sfsdf |afasfs||4|aasfdbc|~564564~~~~
1|1212|34353|5fdf 6575||dfgdg sfsdf ||sfsdf|4|abdfdc|~1~~~
1|1212|34353|5fdf 6575||dfgdg sfsdf ||adfadf|4|dfdabc|~rwarw~~~

I am trying to print only upto last pipe like below:
Code:
1|1212|34353|5fdf 6575||dfgdg sfsdf |afsf||4|aasfbc|
1|1212|34353|5fdf 6575||dfgdg sfsdf |affsf| fsd|4|abc|
.
.
.

I am using the code below:

Code:
#!/usr/local/bin/perl
while ( <DATA>) { 

        s/^(.*.\|)/$1\n/;
        print; 
} 

__DATA__ 
1|1212|34353|5fdf 6575||dfgdg sfsdf |afsf||4|aasfbc|~1213~~~~~
1|1212|34353|5fdf 6575||dfgdg sfsdf |affsf| |4|abc|~rwarw~~asa~~~123~312313
1|1212|34353|5fdf 6575||dfgdg sfsdf |afasfs||4|aasfdbc|~564564~~~~
1|1212|34353|5fdf 6575||dfgdg sfsdf ||sfsdf|4|abdfdc|~1~~~
1|1212|34353|5fdf 6575||dfgdg sfsdf ||adfadf|4|dfdabc|~rwarw~~~

But I am not gettind the required output.

Thanks for your help in advance.
# 2  
Old 01-18-2013
try:
Code:
#!/usr/local/bin/perl
while ( <DATA>) {
        s/^(.*\|).*/$1/;
        print;
}

__DATA__
1|1212|34353|5fdf 6575||dfgdg sfsdf |afsf||4|aasfbc|~1213~~~~~
1|1212|34353|5fdf 6575||dfgdg sfsdf |affsf| |4|abc|~rwarw~~asa~~~123~312313
1|1212|34353|5fdf 6575||dfgdg sfsdf |afasfs||4|aasfdbc|~564564~~~~
1|1212|34353|5fdf 6575||dfgdg sfsdf ||sfsdf|4|abdfdc|~1~~~
1|1212|34353|5fdf 6575||dfgdg sfsdf ||adfadf|4|dfdabc|~rwarw~~~

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

find and Replace String in Perl - Regexp

Trying to find and replace one string with another string in a file #!/usr/bin/perl $csd_table_path = "/file.ntab"; $find_str = '--bundle_type=021'; $repl_str = '--bundle_type=021 --target=/dev/disk1s2'; if( system("/usr/bin/perl -p -i -e 's/$find_str/$repl_str/' $csd_table_path")... (2 Replies)
Discussion started by: cillmor
2 Replies

2. Shell Programming and Scripting

Perl regexp to extract first and second column

Hi, I am trying with the below Perl one-liner using regular expression to extract the first and second column of a text file: perl -p -e "s/\s*(\w+).*/$1/" perl -p -e "s/\s*.+\s(.+)\s*/$1\n/" whereas the text file's data looks like: Error: terminated 2233 Warning: reboot 3434 Warning:... (3 Replies)
Discussion started by: royalibrahim
3 Replies

3. Shell Programming and Scripting

replace awk with a perl one liner (REGEXP and FS)

hello, I want to replace awk with a perl one liner in unix. i use in awk REGEX and FS ( field separator) because awk syntaxes in different unix os versions have not the same behaviour. Awk, Nawk and GNU Awk Cheat Sheet - good coders code, great reuse i have a file named "file" and want... (5 Replies)
Discussion started by: bora99
5 Replies

4. Shell Programming and Scripting

perl regexp: no match across newlines

Hi. Here's a tricky one (at least to me): I have a file named theFile.txt (UTF-8) that contains the following: a b cWhen I execute perl -pe 's|a.*c|d|sg' theFile.txtin bash 3.2 on MAC OS X 10.6, I get no match, i.e. the result is a b cagain. Any clues why? (2 Replies)
Discussion started by: BatManWSL
2 Replies

5. Shell Programming and Scripting

perl regexp matching

Hello, I cannot see what's wrong in my code. When I run code below, it just print an empty string. my $test = "SWER~~ERTGSDFGTHAS_RTAWGA_DFAS.x4-234253454.in"; if ($test = ~ m/\~{1,2}.*4/) { print "$1\n"; } else { print "No match...\n"; } Anyone know what I'm doing wrong? ... (4 Replies)
Discussion started by: urandom
4 Replies

6. Shell Programming and Scripting

perl regexp

What is the easiest way to get full address of *.jpg images from html file using perl? example: http://farm3.static.flickr.com/2397/2126443111_65a810004c.jpg (1 Reply)
Discussion started by: mirusnet
1 Replies

7. Shell Programming and Scripting

Perl RegExp to remove last character from strings

I use SAS (a statistical software) and have to remove last character or the last 1/2 numbers that appear after characters from the string using Perl Regular Expression (which is recognized by SAS). Input: f183ii10 f183ii2 f182ii1 f182ii2 f183iim f22ii f22ii11 f22ii12 pmh4 pmhm Desired... (2 Replies)
Discussion started by: ospreyeagle
2 Replies

8. Shell Programming and Scripting

perl regexp error , I cant understand what is wrong

perl regexp error , I cant understand what is wrong Hello all I have simple perl regexp that is searching for pattern in string and replace it with the same string + addition string here is what I have : my $rec = q| new Array("Attributes Management" ... (4 Replies)
Discussion started by: umen
4 Replies

9. Shell Programming and Scripting

loop throw perl regexp selection

hello all i have string that i need to parse something list that : <form name="CustomerStatus" action="<% env.GetURI %>" method="post"<$if(blah)%> name="<% env.get("StatusList") %>" ><% hello %><input type="hidden" name="<% env.get("Operation") %>" value=""> now im selecting the the string... (6 Replies)
Discussion started by: umen
6 Replies

10. Shell Programming and Scripting

Need Help with Perl REGEXP

I need help with a Perl regular expression. The following string blows up my program: <david(greg jim)> If I type this string, there is no problem: <david(greg_jim)> or type david(gregjim) or type <david greg jim> the CGI program does not complain. For some reason that I do not understand the... (1 Reply)
Discussion started by: mh53j_fe
1 Replies
Login or Register to Ask a Question