Solved

Pruning a log with Perl one-liner

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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

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

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 …
Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
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…

829 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