Solved

Pruning a log with Perl one-liner

Posted on 2012-03-16
3
374 Views
Last Modified: 2012-03-16
Can someone give me a Perl one-liner that will go through a log and delete all lines which match a certain regex?  I'm running this on Solaris, btw.

For instance, given the content below:

this is one line
this is two lines 2011
this is three lines 2011
this is four lines

If the regex is 2011, I would like to have the file only contain:

this is one line
this is four lines


I would like to try to do this without writing to a temp file if possible.

Thanks,
David
0
Comment
Question by:David Aldridge
  • 2
3 Comments
 
LVL 1

Author Comment

by:David Aldridge
ID: 37730450
I was hoping to use some version of this one-liner have a ksh script wrapped around.


#!/bin/ksh
# SCRIPT: sandr
# AUTHOR: David
# DATE:   02/13/12
# REV:    1.0.T
#
# PLATFORM: Solaris
#
# PURPOSE: Search and replace. Provide a search string, replace string
#          and filename. It will replace all instances of the search
#          string with the replace string in the file passed to it. The
#          syntax is:  sandr <search string> <replace string> <filename>
#

if [ "$#" -ne 3 ]
then
        echo "\nIncorrect usage. Wrong number of arguments."
        echo "SYNTAX: <search term> <replace term> <filename>\n"
        exit 1;
else
        SEARCH=$1
        REPLACE=$2
        FILE=$3


        perl -pi -e "s/$SEARCH/$REPLACE/g" "$FILE"
        exit 0;
fi
0
 
LVL 84

Accepted Solution

by:
ozo earned 500 total points
ID: 37730648
perl -ni -e "print unless /$SEARCH/" "$FILE"
0
 
LVL 1

Author Closing Comment

by:David Aldridge
ID: 37730873
Sweet... thanks ozo.
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

The following is a collection of cases for strange behaviour when using advanced techniques in DOS batch files. You should have some basic experience in batch "programming", as I'm assuming some knowledge and not further explain the basics. For some…
Utilizing an array to gracefully append to a list of EmailAddresses
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

815 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

7 Experts available now in Live!

Get 1:1 Help Now