CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
Working with Text Files

 



weddallas
New User

May 13, 2014, 12:24 PM

Post #1 of 4 (3238 views)
Working with Text Files Can't Post

I have a file in the format below

S!!!!!!-15V_FUSED!53!!!-1221.32!-3028.40!!TOP!BOTTOM!

The exclamation points are field delimiters.
I would like to search each line between the 7th and 8th delimiter for a specific numerical value, and delete the entire line if it meets this condition.

Thanks.


BillKSmith
Veteran

May 13, 2014, 7:29 PM

Post #2 of 4 (3230 views)
Re: [weddallas] Working with Text Files [In reply to] Can't Post


Code
#!perl -n 
use strict;
use warnings;
$condition = qr//; # Add your condition as a regex.
my $field = (split /\!/, $_)[7];
print unless /$condition/;


Usage:

Code
perl script.pl in.txt >out.txt

Good Luck,
Bill


weddallas
New User

May 15, 2014, 1:00 PM

Post #3 of 4 (2950 views)
Re: [BillKSmith] Working with Text Files [In reply to] Can't Post

Thanks for the feedback Bill. I was actually working on this before I read your post and discovered a one liner that did the trick.

perl -ne '!/regex/ && print' file1 > file2

This will print to a new file whatever does NOT match the regex.

Thanks again.


BillKSmith
Veteran

May 16, 2014, 5:08 AM

Post #4 of 4 (2938 views)
Re: [weddallas] Working with Text Files [In reply to] Can't Post

Very good! Remember your regex should skip over the first seven field separators before starting the real match.
Good Luck,
Bill

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives