Solved

Pruning a log with Perl one-liner

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

I've just discovered very important differences between Windows an Unix formats in Perl,at least 5.xx.. MOST IMPORTANT: Use Unix file format while saving Your script. otherwise it will have ^M s or smth likely weird in the EOL, Then DO NOT use m…
I have been pestered over the years to produce and distribute regular data extracts, and often the request have explicitly requested the data be emailed as an Excel attachement; specifically Excel, as it appears: CSV files confuse (no Red or Green h…
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…

705 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

21 Experts available now in Live!

Get 1:1 Help Now