Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie
Hi there,
There is an issue with role permissions that is being worked on at the moment.
If you are having trouble with access or permissions on regional forums please post here to get access: https://www.boards.ie/discussion/2058365403/you-do-not-have-permission-for-that#latest

Unix command help: find string pattern and delete line

  • 06-05-2009 12:59pm
    #1
    Registered Users, Registered Users 2 Posts: 13


    Need to delete a line in a large number of files that contains a certain pattern. The file line that the pattern appears varies from file to file. The line that will contain the pattern will also vary slightly. IM sure you can probably do this with sed but I dont know how to pipe the line number from the find command into sed to delete the line.

    In pseudocode:

    If line of file contains partial string 'abcdef'
    then delete that line.

    Ideally Id like to be able to run this from the command line.


    Any help appreciated.


Comments

  • Registered Users, Registered Users 2 Posts: 1,606 ✭✭✭djmarkus


    cronjob78 wrote: »
    Need to delete a line in a large number of files that contains a certain pattern. The file line that the pattern appears varies from file to file. The line that will contain the pattern will also vary slightly. IM sure you can probably do this with sed but I dont know how to pipe the line number from the find command into sed to delete the line.

    In pseudocode:

    If line of file contains partial string 'abcdef'
    then delete that line.

    Ideally Id like to be able to run this from the command line.


    Any help appreciated.


    grep -v abcdef file


  • Registered Users, Registered Users 2 Posts: 432 ✭✭Catch_22


    for file in `find . -type f`
    do
    grep -v abcdef $file >/tmp/$$
    mv /tmp/$$ $file
    done


  • Closed Accounts Posts: 12,807 ✭✭✭✭Orion


    and output to another file:

    grep -v abcdef file > newfile

    or script it to
    grep -v "$1" "$2" > outputfile
    rm "$1"
    mv outputfile "$1"
    

    [edit]or catch_22's solution for all files in a dir.


  • Registered Users, Registered Users 2 Posts: 3,721 ✭✭✭E39MSport


    try: -
    sed -e '/abcdef/d' filename > newfile
    mv newfile filename

    if you have a bunch of files, you could write a small shell like
    fileList="`ls ${fileDir}/*`"
    for yourFile in $fileList ; do
    sed -e '/abcdef/d' $yourFile > $newFile
    mv $newFile $yourFile
    done

    google is your best friend for stuff like this


  • Registered Users, Registered Users 2 Posts: 1,110 ✭✭✭Skrynesaver


    perl -ei 'while(<>){print $_ unless (/REGEX/)};' FILENAMES


  • Advertisement
Advertisement