LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   command to find and delete line in single shot. (https://www.linuxquestions.org/questions/linux-newbie-8/command-to-find-and-delete-line-in-single-shot-913500/)

Sha_unix 11-14-2011 12:21 PM

command to find and delete line in single shot.
 
i have a file in solaris. In which i want to delete line whenever it find "able to delete" sentence at single shot. Is there any awk command or some command to satisfy my need.

file:
i have to delete this line
i am not able to DELETE this line
i am not able to DE LETE this line
able TO delete this line
finding the command.

David the H. 11-14-2011 12:56 PM

sed is probably the best tool to use here. Deleting a line containing a set pattern is an easy thing to do with it.

Here are a few useful sed references.
http://www.grymoire.com/Unix/Sed.html
http://sed.sourceforge.net/grabbag/
http://sed.sourceforge.net/sedfaq.html
http://sed.sourceforge.net/sed1line.txt

Recognize that the version of sed you use on solaris likely has some differences to the gnu sed used on Linux. gnu has added a lot of features like the -i edit in place option that yours probably won't have.

It's generally better if you have some knowledge of regular expressions too.

grep -v is another possible option.

tronayne 11-14-2011 01:02 PM

You can do that with a combination of find, grep, sed and a little shell program (you do use KornShell, right?).
Code:

#!/bin/ksh
#
#      look for files, grep for "able to delete" and delete the line if so
#
for file in $(find your_path -type f)
do
        if [ ! grep -q "able to delete" ]
        then
                sed '/able to delete/d' ${file} > /tmp/junk
                mv /tmp/junk ${file}
        fi
done

If you want to display a bunch of messages, just add appropriate print blah in there.

Hope this helps some.

Sha_unix 11-14-2011 01:27 PM

Thank you its working fine for me. :)


All times are GMT -5. The time now is 07:18 PM.