Solved

Pruning a log with Perl one-liner

Posted on 2012-03-16
3
376 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 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
html form to write data to csv 24 168
Internal Server Error on 1&1 Apache Server running PERL 3 66
Shell script issue 4 54
Unix Command (AIX) 3 42
This Windows batch file is useful for organizing image files from a digital camera or other source, but can have many other uses.  It simply renames the file(s) to match their create date.  For example, if you took a picture today at 1:40pm and the …
Over the years I've spent many an hour playing on hardened, DMZ'd servers, with only a sub-set of the usual GNU toy's to keep me company; frequently I've needed to save and send log or data extracts from these server back to my PC, or to others, and…
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…

749 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