- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - Linux
- >
- search and delete script
Categories
Company
Local Language
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Discussions
Discussions
Forums
Forums
Discussions
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
- BladeSystem Infrastructure and Application Solutions
- Appliance Servers
- Alpha Servers
- BackOffice Products
- Internet Products
- HPE 9000 and HPE e3000 Servers
- Networking
- Netservers
- Secure OS Software for Linux
- Server Management (Insight Manager 7)
- Windows Server 2003
- Operating System - Tru64 Unix
- ProLiant Deployment and Provisioning
- Linux-Based Community / Regional
- Microsoft System Center Integration
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Community
Resources
Forums
Blogs
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО01-25-2007 05:39 AM
тАО01-25-2007 05:39 AM
I want to search and delete using sed some lines from a text files containing some config info.
Say for example, file.txt has content like:-
|abc |123|
|bcg 345989|
|fdk |8977r89q|
|jkhssdf |987q89r89|
|iuhuihsfdah |897q89r|
|sadjfiouoas |jnasdfjkhasdjk|
|jkhjkfasfa| 901590fasklfja
I want to search "jkhssdf" and "sadjfiouoas" and delete them and put the content in a file.
Can someone suggest a quick shell or perl script ?
Appreciate your help.
Thanks,
Shiv
Solved! Go to Solution.
- Tags:
- sed
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО01-25-2007 05:58 AM
тАО01-25-2007 05:58 AM
SolutionThis will find the tokens you indicated and funnel those matching lines into the STDERR stream. The non-matching lines will be directed to STDOUT:
# perl -ne 'if (m{^\|jkhssdf|^\|sadjfiouoas}) {print STDERR} else {print}' file > file.1 2> file.2
...Thus the original file ("file") is left intact and "file.1" contains the non-matching lines; "file.2" contains the matching lines.
Regards!
...JRF...
- Tags:
- Perl
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО01-25-2007 06:18 AM
тАО01-25-2007 06:18 AM
Re: search and delete script
#!/usr/bin/sh
ex -s file <<-EOF
/^|jkhssdf/w file.out | /^|jkhssdf/d
/^|sadjfiouoas/w >> file.out | /^|sadjfiouoas/d
wq
EOF
- Tags:
- ex
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО01-25-2007 12:50 PM
тАО01-25-2007 12:50 PM
Re: search and delete script
Try the below....
awk '{ if ($1=="|abc" || $1=="|bcg") print $2; else print}' file > file.out
- Tags:
- awk
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО01-25-2007 01:28 PM
тАО01-25-2007 01:28 PM
Re: search and delete script
$ grep -e jkhssdf -e sadjfiouoas file.txt > file-extract.txt
$ grep -v -e jkhssdf -e sadjfiouoas file.txt > new_file.txt
- Tags:
- grep
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО01-25-2007 05:03 PM
тАО01-25-2007 05:03 PM
Re: search and delete script
You can do this in simple vi.
Open your config file in vi editor
press "Esc"
you will be given with the edit prompt :
The type the below to replace jkhssdf
%s/jkhssdf//g
and to replace the sadjfiouoas
%s/sadjfiouoas//g
g stands to do the change globally in the whole file.
Be aware the change wil be made in the current file. If you want to have old file also for reference take a copy of the same before doing the change.
sed would be the best option to find and replace and keep the original intact
sed -e "s/jkhssdf//" -e "s/sadjfiouoas//" original_file_name >new_file_name
TQ
Perumal
- Tags:
- vi
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО01-26-2007 04:42 AM
тАО01-26-2007 04:42 AM
Re: search and delete script
I assume you will specify your string to delete being a complete content of a field.
I that is right, I recommend NOT using grep for this filtering, as Dennis suggested.
Having data
|abc |123|
|bcg 345989|
|fdabck |8977r89q|
and trying to drop 'abc'
will lead to a deletion of the last line as well.
Question: I see '|' as delimiter, and your content contains sometimes space as well.
Does it have any meaning - so you see it as a part of the content as well - or is it just a formatting character which could be dropped?
The procedure we have to use to detect - and remove - a field depending on user supplied strings will depend heavily on that.
mfG Peter
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО01-26-2007 04:52 AM
тАО01-26-2007 04:52 AM
Re: search and delete script
Regards,
Shiv
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО01-26-2007 05:33 AM
тАО01-26-2007 05:33 AM
Re: search and delete script
preface:
- the first field is always empty (before the first delimiter), so I deal with fieldnumbers >1 only.
- a space is a field delimiter (and not just to ignore), a string
|a |b|
has five fields: EMPTY,a,EPMTY,b,EMPTY
case 1) The search string has to be looked up in the second field only
awk -F'[ |]' -v sea='str1 str2 ...' 'BEGIN {
s=split(sea,ars," ") }
{ for(j=1;j<=s;j++) if($2==ars[j]) next }
{print}' inputfile
case 2) If you want to lookup in all fields, enclose the 2nd line with an additional loop, e.g.
{ for(i=NF;i>1;i--) if($i) { for(j=1;j<=s;j++) if($2==ars[j]) next } }
mfG Peter
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО01-26-2007 06:38 AM
тАО01-26-2007 06:38 AM
Re: search and delete script
> Yes. pipe and space both are delimiter here.
In that case, I might use:
# perl -ne 'if (m{([\s|])(jkhssdf|sadjfiouoas)([\s|])}) {print STDERR} else {print}' file > file.1 2> file.2
The '\s' is any whitespace character.
Now, if you want this generalized a bit, so that you could *pass* the argument(s) for which you want to search and delele, do this:
# perl -ne 'BEGIN{$re=shift};if (m{([\s|])($re)([\s|])}) {print STDERR} else {print}' jkhssdf\|sadjfiouoas file > file.1 2> file.2
Notice that now you pass the pattern to be found and then the name of the input file.
Notice too, that I escaped the "|" in the pattern so that the shell would not interpret it. The notation "a|b" means match "a" OR "b".
Regards!
...JRF...