?
Solved

Pruning a log with Perl one-liner

Posted on 2012-03-16
3
Medium Priority
?
378 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 2000 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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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…
Background Still having to process all these year-end "csv" files received from all these sources (including Government entities), sometimes we have the need to examine the contents due to data error, etc... As a "Unix" shop, our only readily …
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…
Suggested Courses

752 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