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 |
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. |
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 |
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 |
sorry!!!
My intension is: find . -name "*nantz*.gc" -type f | xargs grep -il "$ID" | xargs sed -i '/$ID/d' |
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:
|
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 |
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 |
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 |
druuna,
i got your reply before i post the above comment. Just added an additional point to my question.. sorry!! |
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 |
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 |
Quote:
To answer your off-topic question: Pick up the RHCSA/RHCE Linux Certification Study Guide by Michael Jang. |
druuna,
Thank you so much!!! -Sreenivas |
All times are GMT -5. The time now is 04:42 AM. |