awk to delete word when found


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk to delete word when found
# 8  
Old 08-16-2012
i have a slightly different scenario now. i want the awk command to omit every hostgroup configuration that does not have anything set for the members section, as in below:

Code:
define hostgroup {
        alias                         Virtual Machines Primary Site
        hostgroup_name                virt-machines-primary
        members
        use                           generic-hostgroup
}

when members is set, it will look like this:


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
}

so in other words, i want the awk command to omit every hostgroup configuration that is memberless and only output configurations that has some hostname(s) set in the member section. make sense?
# 9  
Old 08-16-2012
OK, changing target, needs different approach, a combination of the former two. Try
Code:
awk 'NR==FNR {a=(a?a"|":"")$0; next}; {gsub(a,""); gsub(" ,+"," "); gsub(",+$",""); gsub(",+",",")} !/members[ \t\n]*use/ {print $0 RS}' host_file RS="}\n"  conf_file

Pls test thoroughly and come back with results.

A question to the awk pundits: is there a trick to remove commas at the beginning of a string, its end, and multiple commas in the middle, all in one go?
# 10  
Old 08-17-2012
commas can be removed in awk using gsub
Code:
gsub(",","",string)

# 11  
Old 08-17-2012
Quote:
Originally Posted by raj_saini20
commas can be removed in awk using gsub
Code:
gsub(",","",string)

Yes, thank you, this is what I did in my post, but in three gsubs. The question was for an intelligent/tricky regex to remove all leading, trailing, and double commas in ONE go.
# 12  
Old 08-17-2012
by double comma you mean inverted comma.
also please provide sample data for which you want to remove these
# 13  
Old 08-17-2012
Quote:
Originally Posted by raj_saini20
by double comma you mean inverted comma.
No. Pls. see post #6 in this thread for sample of repeated commas remaining after removal of conf entries.
Quote:
also please provide sample data for which you want to remove these
post #6.
Right now, I use repeated gsubs to eliminate leading, trailing, and/or repeated commas. Looking for a trick to do it in one gsub only.
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