Hello Friends,
i need help to remove "\#" sign from a file. I want to remove the "\#" marks just in the area mentioned in red. I am not so good in awk, this help would be deeply appreciated.
thanks
Ado
trap = {
# {
# trap-community = SNMP-trap
# hosts = hubble, snowbell
# {
# enterprise = "sun"
# trap-num = 0, 1, 2-5, 6-16
# }
# {
# enterprise = "3Com"
# trap-num = 4
# }
# {
# enterprise = "snmp"
# trap-num = 0, 2, 5
# }
# }
# {
# trap-community = jerry-trap
# hosts = jerry, nanak, hubble
# {
# enterprise = "sun"3
# trap-num = 1, 3
# }
# {
# enterprise = "snmp"
# trap-num = 1-3
# }
# }
#}
Output should be like
trap = {
{
trap-community = SNMP-trap
hosts = hubble, snowbell
{
enterprise = "sun"
trap-num = 0, 1, 2-5, 6-16
}
# {
# enterprise = "3Com"
# trap-num = 4
# }
# {
# enterprise = "snmp"
# trap-num = 0, 2, 5
# }
}
# {
# trap-community = jerry-trap
# hosts = jerry, nanak, hubble
# {
# enterprise = "sun"3
# trap-num = 1, 3
# }
# {
# enterprise = "snmp"
# trap-num = 1-3
# }
# }
}
sed -e 's/^# //' file_in > file_out
this did not work, i just want to remove the # marks from the red area, not from the whole file.
joeyg
February 5, 2009, 3:43pm
4
While I can see what you put in red, what is the rule to know what to remove the # character?
For instance,
lines 1-10, 20, 30
or
???
I think to remove by line number(NR) would be okay. i just want to get the file to look like as i mentioned in the output.
thanks
just a dumb question:
how do you know . ... which lines you want commented out?
apparantly you need this done during a batch program, that'll be
run many times, otherwise, just use vi and edit the thing....
If you know which lines need to be commented out based on
some criteria, then this is a rather difficult approach.
Can you do something like:
case $condition in
A) this_trap_stuff ;;
B) that_trap_stuff ;;
C) even_more_other_trap_stuff ;;
esac
joeyg
February 5, 2009, 4:09pm
7
script execution
> play162.sh
trap = {
{
trap-community = SNMP-trap
hosts = hubble, snowbell
{
enterprise = "sun"
trap-num = 0, 1, 2-5, 6-16
}
# {
# enterprise = "3Com"
# trap-num = 4
# }
# {
# enterprise = "snmp"
# trap-num = 0, 2, 5
# }
}
# {
# trap-community = jerry-trap
# hosts = jerry, nanak, hubble
# {
# enterprise = "sun"3
# trap-num = 1, 3
# }
# {
# enterprise = "snmp"
# trap-num = 1-3
# }
# }
}
script
> cat play162.sh
awk '
{ if (((NR>=2)&&(NR<=8)) ||
(NR==17) || (NR==30)) {
print substr($0,2,99)
} else {
print
}
}' file162
sed '2,8s/^.//;17s/^.//;30s/^.//' file
naaah, I don't think OP can assume a static file layout - what matters is the 'scope' of the matching {}-s (I think).
Let's see if this is what the OP is looking for:
nawk -f asi.awk myFile
asi.awk:
/trap = {/ {blockOUT++}
/^# *{/ {blockOUT++}
blockOUT<=3 {sub("^# *", " ")}
/^# *}/ {blockOUT--}
1
jhefner
February 5, 2009, 5:57pm
10
sed -i 's/^#//g' filename
once again - reread the OPs requirements:
jhefner
February 5, 2009, 6:12pm
12
oops, it would have helped if I had read your entire question
joeyg
February 6, 2009, 9:37am
14
Received a message asking me to explain my awk, so I am posting it here for the good of the community.
> cat play162.sh
awk '
{ if (((NR>=2)&&(NR<=8)) ||
(NR==17) || (NR==30)) {
print substr($0,2,99)
} else {
print
}
}' file162
awk ' ===> starts awk
{ if (((NR>=2)&&(NR<=8)) || ===> checks if line # (>=2 AND <=8) OR
(NR==17) || (NR==30)) { ===> if line # =17 OR line # =30
print substr($0,2,99) ===> if conditions met,
===> print the line beginning @ position 2
} else { ===> if conditions not met
print ===> print the line of text as-is
} ===> end the section within awk
}' file162 ===> end the awk commands, & provide the input file
danmero
February 6, 2009, 10:50am
15
vgersh99:
naaah, I don't think OP can assume a static file layout - what matters is the 'scope' of the matching {}-s (I think).
Let's see if this is what the OP is looking for:
nawk -f asi.awk myFile
asi.awk:
/trap = {/ {blockOUT++}
/^# *{/ {blockOUT++}
blockOUT<=3 {sub("^# *", " ")}
/^# *}/ {blockOUT--}
1
Nice, your way rocks
Moving to oneliner:
awk '/trap = {/ || /^# *{/ {blockOUT++};blockOUT <= cnt {sub("^# *", " ")};/^# *}/ {blockOUT--}1' cnt=3 file