LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   i have to delete the complete line that match to the strings specified by file (https://www.linuxquestions.org/questions/linux-newbie-8/i-have-to-delete-the-complete-line-that-match-to-the-strings-specified-by-file-4175438737/)

raosr020 11-26-2012 03:21 AM

i have to delete the complete line that match to the strings specified by file
 
Hi Everyone,

Need help please!!

I have a file named gc_ids that contains list of strings that needs to be deleted from all the files that are given by "find" command.

Actually i have to delete the complete line that match to the string specified by gc_ids file.

The below script is not working!! I am new to scripting world. This is very much useful for my day to day work. Please help with this..

#vi gc_ids_del
#!/bin/ksh
while read ID
do
find . -name "*nantz*.gc" -type f | xargs sed -i '/$ID/d'
done < gc_ids

Thanks in advance!!

Regards,
Sreenivas

shivaa 11-26-2012 03:35 AM

Change your working shell to bash and try again.
But one thing, this script will print all lines in all maching files, exect those lines containing $ID. For deletion, you should save output in a seperate file.
Anyway, once share the error message you're getting, if any.

raosr020 11-26-2012 04:04 AM

Thank you for you reply!!

My requirement is the file names should be same. I can't change the file names because it is in the server. Those *nantz*.gc files are mandatory files.

I tried changing to bash..Its not working..

Will the below one works?
find . -name "*nantz*.gc" -type f | grep -il "$ID" |xargs sed -i '/$ID/d'

Am not getting any error..It is directing to prompt..Thats all!!

Thanks,
Sreenivas

druuna 11-26-2012 04:41 AM

You really should provide examples of the input files, without them you might get answers that might or might not work.

That said; sed -i '/$ID/d' is incorrect, the literal $ID is used (due to the single quotes), not the content of $ID.

Have a look at this:
Code:

while read ID
do
find . -type f -name "*nantz*.gc" -exec sed -i.org '/'$ID'/d' {} \;
done < gc_ids

The above also creates backups of the original file (remove the blue part [.org] if all works as desired).

raosr020 11-26-2012 04:46 AM

sorry!!!

My intension is:

find . -name "*nantz*.gc" -type f | xargs grep -il "$ID" | xargs sed -i '/$ID/d'

druuna 11-26-2012 04:49 AM

Like I said before: Post examples, and adding to that: describe what it is you want, don't post a (none-working?) one-liner.

From your first post:
Quote:

Originally Posted by raosr020
I have a file named gc_ids that contains list of strings that needs to be deleted from all the files that are given by "find" command.

That's what my command does.

raosr020 11-26-2012 05:10 AM

druuna,

Thank you so much for your help!!

I ran with -i option without .org itself. It worked well..Also, I don't find any backup files created for the original files..Please help me in understanding why do you use .org?

find . -type f -name "*nantz*.gc" -exec sed -i '/'$ID'/d' {} \;

-SREENIVAS

druuna 11-26-2012 05:19 AM

In general: creating a backup of the original file is the smart thing to do. If something goes wrong you can always restore it.

Sed's -i option is for in-place changes (the original file is changed). If you want/need a backup you can tell sed to make one using -i.bak.

In the above example a copy, with the extension .bak is made that holds the original file (in my previous post I used .org).

This link might help understanding sed a bit better: GNU sed, a stream editor

raosr020 11-26-2012 05:20 AM

druuna,

Just an add to the above post!!

Actually i don't need backup files.Just the original files (*nantz*.gc) is enough. Changes should happen to the original files..That is all what i need..

Thank you again!!

Regards,
Sreenivas

raosr020 11-26-2012 05:22 AM

druuna,

i got your reply before i post the above comment. Just added an additional point to my question..

sorry!!

druuna 11-26-2012 05:22 AM

You can remove the backups after you made sure the originals are changed the way you want/need. Better be safe then sorry.....

Can you put up the [SOLVED] tag.

- above the first post -> Please Mark this thread as solved if you feel a solution has been provided.
- -or- -
- first post -> Thread Tools -> Mark this thread as solved

raosr020 11-26-2012 05:35 AM

druuna,

Thank you so much for solving my issue..I'm scratching my head from the past 4 hours.

Your help is much appreciated!!! I definitely need your help in future..I'm planning for RHCE certification. Could you please send me any material if you have that will help me in preparing for the RHCE EXAM.

Thanks again!!

-Sreenivas

druuna 11-26-2012 06:46 AM

Quote:

Originally Posted by raosr020 (Post 4837158)
I'm planning for RHCE certification. Could you please send me any material if you have that will help me in preparing for the RHCE EXAM.

My gmail ID: deleted

You shouldn't post your email address (spam etc). Besides that, I don't send information to individuals (all info provided should be searchable by others).

To answer your off-topic question: Pick up the RHCSA/RHCE Linux Certification Study Guide by Michael Jang.

raosr020 11-26-2012 09:24 AM

druuna,

Thank you so much!!!

-Sreenivas


All times are GMT -5. The time now is 04:42 AM.