LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 11-26-2012, 04:21 AM   #1
raosr020
LQ Newbie
 
Registered: Nov 2012
Posts: 17

Rep: Reputation: Disabled
Unhappy 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
 
Old 11-26-2012, 04:35 AM   #2
shivaa
Senior Member
 
Registered: Jul 2012
Location: Grenoble, Fr.
Distribution: Sun Solaris, RHEL, Ubuntu, Debian 6.0
Posts: 1,800
Blog Entries: 4

Rep: Reputation: 286Reputation: 286Reputation: 286
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.
 
1 members found this post helpful.
Old 11-26-2012, 05:04 AM   #3
raosr020
LQ Newbie
 
Registered: Nov 2012
Posts: 17

Original Poster
Rep: Reputation: Disabled
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
 
Old 11-26-2012, 05:41 AM   #4
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2390Reputation: 2390Reputation: 2390Reputation: 2390Reputation: 2390Reputation: 2390Reputation: 2390Reputation: 2390Reputation: 2390Reputation: 2390Reputation: 2390
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).
 
1 members found this post helpful.
Old 11-26-2012, 05:46 AM   #5
raosr020
LQ Newbie
 
Registered: Nov 2012
Posts: 17

Original Poster
Rep: Reputation: Disabled
sorry!!!

My intension is:

find . -name "*nantz*.gc" -type f | xargs grep -il "$ID" | xargs sed -i '/$ID/d'
 
Old 11-26-2012, 05:49 AM   #6
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2390Reputation: 2390Reputation: 2390Reputation: 2390Reputation: 2390Reputation: 2390Reputation: 2390Reputation: 2390Reputation: 2390Reputation: 2390Reputation: 2390
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.
 
1 members found this post helpful.
Old 11-26-2012, 06:10 AM   #7
raosr020
LQ Newbie
 
Registered: Nov 2012
Posts: 17

Original Poster
Rep: Reputation: Disabled
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
 
Old 11-26-2012, 06:19 AM   #8
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2390Reputation: 2390Reputation: 2390Reputation: 2390Reputation: 2390Reputation: 2390Reputation: 2390Reputation: 2390Reputation: 2390Reputation: 2390Reputation: 2390
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
 
2 members found this post helpful.
Old 11-26-2012, 06:20 AM   #9
raosr020
LQ Newbie
 
Registered: Nov 2012
Posts: 17

Original Poster
Rep: Reputation: Disabled
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
 
Old 11-26-2012, 06:22 AM   #10
raosr020
LQ Newbie
 
Registered: Nov 2012
Posts: 17

Original Poster
Rep: Reputation: Disabled
druuna,

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

sorry!!
 
Old 11-26-2012, 06:22 AM   #11
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2390Reputation: 2390Reputation: 2390Reputation: 2390Reputation: 2390Reputation: 2390Reputation: 2390Reputation: 2390Reputation: 2390Reputation: 2390Reputation: 2390
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

Last edited by druuna; 11-26-2012 at 06:24 AM. Reason: fixed typo: the -> then
 
1 members found this post helpful.
Old 11-26-2012, 06:35 AM   #12
raosr020
LQ Newbie
 
Registered: Nov 2012
Posts: 17

Original Poster
Rep: Reputation: Disabled
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

Last edited by raosr020; 11-26-2012 at 10:26 AM.
 
Old 11-26-2012, 07:46 AM   #13
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2390Reputation: 2390Reputation: 2390Reputation: 2390Reputation: 2390Reputation: 2390Reputation: 2390Reputation: 2390Reputation: 2390Reputation: 2390Reputation: 2390
Quote:
Originally Posted by raosr020 View Post
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.
 
1 members found this post helpful.
Old 11-26-2012, 10:24 AM   #14
raosr020
LQ Newbie
 
Registered: Nov 2012
Posts: 17

Original Poster
Rep: Reputation: Disabled
druuna,

Thank you so much!!!

-Sreenivas
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] SED/AWK - Delete all lines until empty line is found after pattern match vikas027 Programming 13 03-28-2012 09:33 AM
[SOLVED] AWK: match multiple strings in the file, print 1 when match and 0 when not cristalp Programming 12 11-15-2011 11:18 AM
Perl: Match part of a line and replace with another line from the same file briana.paige Linux - Newbie 8 06-27-2009 07:35 AM
Perl question: delete line from text file with duplicate match at beginning of line mrealty Programming 7 04-01-2009 07:46 PM
How to delete the file using pattern match? nishanthhampali Programming 3 04-16-2008 01:50 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 01:52 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration