Use curly braces to group a set of statements to execute when the expression evaluates to true:
Ok, thanks. That was making me nuts.
Quote:
[code]
awk -v remove=XMPP_ec2-184-2-2-2 '
/define host/ { # start of new block; print last section
if( snarfed )
print snarfed;
snarfed = $0; # start capture
next;
}
/host_name/ { # check to see if unwanted target
if( $2 == remove ) # if it is then
snarfed = ""; # unset to trash the whole block
next;
}
snarfed { # if we are snarfing from current section
snarfed = snarfed "\n" $0; # add next line
next;
}
END { # must do one last for the last buffered section
if( snarfed )
print snarfed;
}
' config-file
Cool. I knew there was a way to do this without 2 awk statements. What you wrote here is almost what I need. It was removing the "host_name" record from all the host definitions so I removed the "next;" line from the /host_name/ section.
Your comment about other text in the file made me realize I need to support that. I added in some text and any text before the first "define host" is not output. Here's the test file:
And this is the awk:
and this is the result:
Any idea why the text before the first host definition gets snipped?
I have this Korn script that I wrote (with some help) that is run by cron. I basically watches a file system for a specific filename to be uploaded (via FTP), checks to make sure that the file is no longer being uploaded (by checking the files size), then runs a series of other scripts. The... (2 Replies)
I need to remove the '&' from a file.
In each line of the file, the fields are separated by ^K.
I only want to remove '&' if it exists in field number 9. (example of field 9: abc&xyz)
I need to do an in place/in line edit.
So far I have accomplished the following:
awk -F '^K' '{print... (6 Replies)
I have a large list of filenames from an Excel sheet, which I then translate into a simple text file. I'd like to use this list, which contains various file extensions , to archive these files and then remove them recursively through multiple directories and subdirectories. So far, it looks like... (5 Replies)
Dear all,
I have a file which have let us say records from A-Z.
Now I want to remove multiple letter from this file using single command.. let us say I want to remove A,F,K,Y,U,P,B,S,D.
I can use grep -v command but for this case i need to rerun the file several time i wana avoid using... (3 Replies)
I need to replace (delete) a text block in a bunch of files, its a html table, almost at the end of pages but the location varies.
In Windows I used Filemonkey, but nothing like that in Unix?
There is replace from mysql, but how does it deal with newlines?
sed only works with single lines,... (6 Replies)
Hi,
I lack the utter fundamentals on how to craft an awk script.
I have hundreds of text files that were mangled by .doc format so all the lines are broken up so I need to join all of the lines of text into a single line. Normally I use vim command "ggVGJ" to join all lines but with so many... (3 Replies)
Hello all,
I have a text files that consists of blocks of text. Each block of text represents a set of Cartesian coordinates for a molecule. Each block of text starts with a line that has a only a number, which is equal to the total number of atoms in the molecule. After this number is a line... (15 Replies)
Hello,
Although I have found similar questions, I could not find advice that
could help with our problem.
The issue:
We have several hundreds text files containing repeated blocks of text
(I guess back at the time they were prepared like that to optmize
printing).
The block of texts... (13 Replies)
Hi,
I have file with all the lines as following format
<namebindings:StringNameSpaceBinding xmi:id="StringNameSpaceBinding" name="ENV_CONFIG_PATH" nameInNameSpace="COMP/HOD/MYSTR/BACKOFFICE/ENV_CONFIG_PATH" stringToBind="test"/>
I want to replace (all the lines) value of... (8 Replies)
Hi
I have a text file named main.txt with 10,000 lines. I have another file with a list of line numbers (around 1000) of the lines to be deleted from main.txt file.
I tried with sed but it removes only a range of line numbers.
Thanks for any help!! (1 Reply)