Results 1 to 6 of 6
Hi I have the following data:
Code:
define host {
hostname win2k3.example.com
alias domain server
ip address 10.0.0.1
}
define host {
alias domain server
hostname popie.example.com
ip address 10.0.0.1
...
- 01-20-2010 #1Just Joined!
- Join Date
- Dec 2005
- Posts
- 2
Deleting a block of text
Hi I have the following data:
and need help on how to delete a a block based on a match. For example if I wanted to delete win2k3.example.com how would I get rid of the entire block ofCode:define host { hostname win2k3.example.com alias domain server ip address 10.0.0.1 } define host { alias domain server hostname popie.example.com ip address 10.0.0.1 } define host { alias domain server ip address 10.0.0.20 hostname srv123.example.com }
I believe this can be done with awk but not sure how since awk is field based rather than line based?Code:define host { hostname win2k3.example.com alias domain server ip address 10.0.0.1 }
Any tips would be great.
Thanks
- 01-20-2010 #2Linux User
- Join Date
- Nov 2009
- Location
- France
- Posts
- 292
There's probably a cleaner way to do it, I'm eager to learn it.
sed can work with adjacent lines to a searched string, but I don't know how to handle it really.
- 01-20-2010 #3Just Joined!
- Join Date
- Dec 2005
- Posts
- 2
Thank you nmset, can you let me know what the ${tmpID%%:*} is? I understand you are assigning it to the $ID variable but not sure exactly what this is. In addition, how would you loop this through all UNIQUE_STRING matches in a file? I was thinking a for loop but you are using a while.
Thanks
- 01-20-2010 #4Linux User
- Join Date
- Nov 2009
- Location
- France
- Posts
- 292
${tmpID%%:*} is to get the line number of the line containing the search string, i.e, win2k3.example.com. It is a bash buil-in functionality for string manipulation. The line in NSOURCE (numbered source file) looks like this from grep -n :
xy: hostname win2k3.example.com
${tmpID%%:*} returns all characters preceding the first match of the split character, here ':' .
I understand from your sample data that you wish to delete one block containing an identifier that is unique in the dataset. If there are more occurences of the identifier, the script is no longer valid. Yet it could be updated.how would you loop this through all UNIQUE_STRING matches in a file
- 01-21-2010 #5Linux Engineer
- Join Date
- Apr 2006
- Location
- Saint Paul, MN, USA / CentOS, Debian, Solaris, SuSE
- Posts
- 1,117
Hi.
For many "window", "paragraph", "segment" searches, copies, extractions, deletions, etc., I find the utility cgrep to be of value. It allows one to specify a regular expression to match a line of interest, and also to specify RE's for matching the boundary lines preceding and succeeding it. For example, on your data in file "data1":
which produces the following, showing how one might takes steps to solve: extract the segment within which the string of interest is found, then invert the action -- copy everything except that segment, and finally clean things up a bit:Code:#!/usr/bin/env bash # @(#) s1 Demonstrate window extraction with cgrep. # http://www.bell-labs.com/project/wwexptools/cgrep/ echo set +o nounset LC_ALL=C ; LANG=C ; export LC_ALL LANG echo "Environment: LC_ALL = $LC_ALL, LANG = $LANG" echo "(Versions displayed with local utility \"version\")" version >/dev/null 2>&1 && version "=o" $(_eat $0 $1) cgrep set -o nounset echo FILE=${1-data1} echo " Data file $FILE:" cat $FILE echo echo " Results, extract line in window of interest:" cgrep -w "^define host" +w "^}" "win2k3.example.com" $FILE echo echo " Results, invert the behavior:" cgrep -V -w "^define host" +w "^}" "win2k3.example.com" $FILE echo echo " Results, invert the behavior, remove the separator:" cgrep -D -V -w "^define host" +w "^}" "win2k3.example.com" $FILE exit 0
The code is found at the URL mentioned at the top of the script source. I have compiled it in both 32 and 64-bit modes.Code:% ./s1 Environment: LC_ALL = C, LANG = C (Versions displayed with local utility "version") OS, ker|rel, machine: Linux, 2.6.26-2-amd64, x86_64 Distribution : Debian GNU/Linux 5.0 GNU bash 3.2.39 cgrep - (local: ~/executable/cgrep May 29 2009 ) Data file data1: define host { hostname win2k3.example.com alias domain server ip address 10.0.0.1 } define host { alias domain server hostname popie.example.com ip address 10.0.0.1 } define host { alias domain server ip address 10.0.0.20 hostname srv123.example.com } Results, extract line in window of interest: define host { hostname win2k3.example.com alias domain server ip address 10.0.0.1 } Results, invert the behavior: ======================================== define host { alias domain server hostname popie.example.com ip address 10.0.0.1 } define host { alias domain server ip address 10.0.0.20 hostname srv123.example.com } Results, invert the behavior, remove the separator: define host { alias domain server hostname popie.example.com ip address 10.0.0.1 } define host { alias domain server ip address 10.0.0.20 hostname srv123.example.com }
If you do not like to download, compile, and install code, then you will likely need to learn something like awk or perl to solve this kind of problem.
Best wishes ... cheers, drl
( edit 1: minor typo )Last edited by drl; 01-21-2010 at 08:18 PM.
Welcome - get the most out of the forum by reading forum basics and guidelines: click here.
90% of questions can be answered by using man pages, Quick Search, Advanced Search, Google search, Wikipedia.
We look forward to helping you with the challenge of the other 10%.
( Mn, 2.6.n, AMD-64 3000+, ASUS A8V Deluxe, 1 GB, SATA + IDE, Matrox G400 AGP )
- 01-21-2010 #6Linux User
- Join Date
- Nov 2009
- Location
- France
- Posts
- 292
cgrep
I haven't yet tried it but from what you say, I find it a wonderful tool which can save much time. Thank you, drl.


Reply With Quote