awk to delete word when found


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk to delete word when found
# 1  
Old 08-15-2012
awk to delete word when found

i'm trying to use the following awk statement to delete every host name it finds in the hostlist file from the configuration file (conf.txt).

Code:
awk 'NR==FNR {a=(a?a"|":"")$0; next}; match ($0,a) {print $0 RS}' hostlist RS="}" conf.txt

i think something like this will be necessary but i dont know how to add it to the above statement:

Code:
sub("$a", "")

# 2  
Old 08-16-2012
please provide the sample input data and required output
# 3  
Old 08-16-2012
Looks like the continuation of your previous thread (see there for sample data); what does not work with above statement? It sucks in the hostlist to compose a large regex, then reads your conf.txt file one configuration entry at a time. If the hostname is NOT found in the regex, the entry is printed. In your above citation, the ! sign in front of the match is missing to EXclude the respective host.
sub("$a", "") definitely is not needed.

Btw, do you want to delete just the hostname or the entire conf entry for that hostname?

Last edited by RudiC; 08-16-2012 at 04:14 AM..
This User Gave Thanks to RudiC For This Post:
# 4  
Old 08-16-2012
Quote:
Originally Posted by RudiC
Looks like the continuation of your previous thread (see there for sample data); what does not work with above statement? It sucks in the hostlist to compose a large regex, then reads your conf.txt file one configuration entry at a time. If the hostname is NOT found in the regex, the entry is printed. In your above citation, the ! sign in front of the match is missing to EXclude the respective host.
sub("$a", "") definitely is not needed.

Btw, do you want to delete just the hostname or the entire conf entry for that hostname?
thank you so much for correlating this thread to the other thread.

i want to delete just the host name. what's not working is that, this time, i'm running the awk command against a different configuration file. the other configuration file in the other thread was the configuration specifically for hosts.

now, the configuration file i'm working on is the configuration file for the hostgroup(s) of which the host names in the list belong to.

below is an output of this hostgroup configuration file:

Code:
define hostgroup {
      alias		Virtual Machines Primary Site
      hostgroup_name	virt-machines-primary
      members           relay-nasty-01.net,relay-nasty-02.net,relay-nasty-03.net
      use		generic-hostgroup
}


define hostgroup {
      alias		Virtual Machines Secondary Site
      hostgroup_name	virt-machines-secondary
      members           relay-nasty-04.net,relay-nasty-05.net,relay-nasty-06.net
      use		generic-hostgroup
}

so when i run the awk command against this configuration file, if it finds, for example, relay-nasty-01.net in the hostgroup called virt-machines-primary, i want it to remove that host name from that group and leave the remaining host names in that group intact UNLESS they're also in the list of host names that the awk is looking to remove.
# 5  
Old 08-16-2012
You can't use above command as it will delete the entire conf entry. Try this:
Code:
awk 'NR==FNR {a=(a?a"|":"")$0; next}; {gsub(a,""); gsub(" ,+"," "); gsub(",+$","")} 1' host_file conf_file

It loads all host names to compose a regex, and then eliminates any member of the regex from your file. Residual leading and trailing commas have to be eliminated as well.
This User Gave Thanks to RudiC For This Post:
# 6  
Old 08-16-2012
Quote:
Originally Posted by RudiC
You can't use above command as it will delete the entire conf entry. Try this:
Code:
awk 'NR==FNR {a=(a?a"|":"")$0; next}; {gsub(a,""); gsub(" ,+"," "); gsub(",+$","")} 1' host_file conf_file

It loads all host names to compose a regex, and then eliminates any member of the regex from your file. Residual leading and trailing commas have to be eliminated as well.
this works wonderfully.

i think i need a simple modification. after it removes the host name, there is a ",," left in the place of teh host name. this usually happens if the host name is not first in the list.

example:

conf file

Code:
define hostgroup {
      alias		Virtual Machines Primary Site
      hostgroup_name	virt-machines-primary
      members           relay-nasty-01.net,relay-nasty-02.net,relay-nasty-03.net,relay-nasty-03b.net
      use		generic-hostgroup
}

if i included only "relay-nasty-02.net" in the host list, and i ran the awk command, what results is this:

Code:
define hostgroup {
      alias		Virtual Machines Primary Site
      hostgroup_name	virt-machines-primary
      members           relay-nasty-01.net,,relay-nasty-03.net,relay-nasty-03b.net
      use		generic-hostgroup
}

if i were to add relay-nasty-03.net to the list of hosts to remove, i get this:

Code:
define hostgroup {
      alias		Virtual Machines Primary Site
      hostgroup_name	virt-machines-primary
      members           relay-nasty-01.net,,,relay-nasty-03b.net
      use		generic-hostgroup
}


Last edited by SkySmart; 08-16-2012 at 07:56 AM..
# 7  
Old 08-16-2012
Rats! I knew I forgot sth. Put a
Code:
;gsub(",+",",")

after the last gsub. Sorry!
This User Gave Thanks to RudiC For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

How to search for a word in column header that fully matches the word not partially in awk?

I have a multicolumn text file with header in the first row like this The headers are stored in an array called . which contains I want to search for each elements of this array from that multicolumn text file. And I am using this awk approach for ii in ${hdr} do gawk -vcol="$ii" -F... (1 Reply)
Discussion started by: Atta
1 Replies

2. Shell Programming and Scripting

Delete only if duplicates found in each record

Hi, i have another problem. I have been trying to solve it by myself but failed. inputfile ;; ID T08578 NAME T08578 SBASE 30696 EBASE 32083 TYPE P func just test func chronology func cholesterol func null INT 30765-37333 INT 37154-37318 Link 5546 Link 8142 (4 Replies)
Discussion started by: redse171
4 Replies

3. Shell Programming and Scripting

How to print few lines before and after matching word is found suing grep?

Hi, here are few lines present in the logs. I want to grep on Error and print few lines before and after Error word is found line1 Line2 Line3 Error Line4 Line5 Line6 Line7 I want the output to be Line2 Line3 Error Line5 (1 Reply)
Discussion started by: arghadeep adity
1 Replies

4. Shell Programming and Scripting

Search the word to be deleted and delete lines above this word starting from P1 to P3

Hi, I have to search a word in a text file and then I have to delete lines above from the word searched . For eg suppose the file is like this: Records P1 10,23423432 ,77:1 ,234:2 P2 10,9089004 ,77:1 ,234:2 ,87:123 ,9898:2 P3 456456 P1 :123,456456546 P2 abc:324234 (2 Replies)
Discussion started by: vsachan
2 Replies

5. Shell Programming and Scripting

print next word after found pattern

Hi all, I'd like to print the next word after a found pattern. example text: word1 word2 word3 word4 pattern word5 pattern word1 word2 word3 word4 word1 word2 pattern word4 basiclly the word after pattern. Thanks (9 Replies)
Discussion started by: stinkefisch
9 Replies

6. Shell Programming and Scripting

prompt to delete each record when pattern is found

Hello!. I am working on a very simple program and I have been trying different things. This is so far what I have done and there is one small detail that still does not work. It finds all the records in a phonebook per say: ./rem Susan More than one match; Please select the one to remove: ... (3 Replies)
Discussion started by: bartsimpsong
3 Replies

7. Shell Programming and Scripting

Delete everything after/before a word in a file

I'm looking for a command that will read a file listing information and delete everything after a certain word is found. I also may need to search the file and delete everything before a certain word. The file would contains fields of information like below repeating for the entire file; Name... (5 Replies)
Discussion started by: daveisme
5 Replies

8. Shell Programming and Scripting

Delete line if pattern not found

I thought that this was going to be quit simple using sed but i wasn't able to find a way to delete the second line of a text file if my pattern was not found in the line With awk i am completly useless :rolleyes: Any ideas? (2 Replies)
Discussion started by: jepeto
2 Replies

9. UNIX for Dummies Questions & Answers

Delete a word

i have created the following input script CREATE OR REPLACE VIEW MMSSENDAVGUPLOADTIME_GN AS sp_strip_mean_value_type.get_val(MMSSENDAVGUPLOADTIME) MMSSENDAVGUPLOADTIME_val sp_strip_mean_value_type.get_val(MMSSENDAVGUPLOADTIME) MMSSENDAVGUPLOADTIME_counter select... (1 Reply)
Discussion started by: gseptember
1 Replies

10. UNIX for Dummies Questions & Answers

How to delete all character before certain word

Hi, For example, i have a string "123 456 789 abc 111 222 333" and I would like to delete all the characters before abc so that it becomes "abc 111 222 333" how can i do that in unix? using sed? note: I actually don't know how many words/charachters before "abc", so the "cut"... (9 Replies)
Discussion started by: phandy
9 Replies
Login or Register to Ask a Question