Shell Need help with sed

Hello. First off I am not a programmer or script kiddie of any sorts and I am having a really hard time understanding sed. One of our other engineers left behind a script we heavily used to search our Cisco config files with. We backed these files up in a directory on a FreeBSD machine. The script was called confsearch. Below is the script and all it consisted of.

Code:
confsearch () { grep -ir -B 1 -A 4 "$*" /usr/local/configs | sed -E 's#/usr/local/configs/##g' | sed -E 's#,v:?-?#: #g'; }

How it worked was, I could type confsearch Company ABC and it would output Cisco interfaces that had descriptions on them with that company's name.

I understand what grep(1) is doing, I do not understand what the two sed(1) statements are doing and the man pages for sed(1) are really confusing.

I don't know if it will help but I think we use to use Rancid to back up config files, now we use Solar Winds CAT tools and it sames the Cisco config file in a simple .txt file.

Any help in telling me what exactly is happening in the sed(1) statements would be great as I am trying to get this to work on another system.
 
The first command sed -E 's#/usr/local/configs/##g is just deleting all occurences of the pattern /usr/local/configs/ on every input line. It is using the # -sign as the delimiter between the pattern, replacement and flags -parts in the search-and-replace operator that is usually used as s/pattern/replacement/flags but since the pattern has / -characters it's better to choose another delimiter, sed(1) is flexible in that way. The second one is using the same delimiter # and the pattern is actually using the extended regular expressions (the -E option, see re_format(7)). The pattern is ,v:?-? and it seems to be matching anything that starts as ,v followed by optional (the ? operator in extended regular expressions) : and - characters (either or both can be omitted). The replacement part is just ": ". Note that the g flag at the end of the operations means "replace all occurences on each line, not just the first".

HTH
 
Well that definitely helps a lot. haha I think I will have to install Rancid and see how its copying the configs over and there is a reason for the matching of ",v". Thanks so much! I greatly appreciate it.
 
So I figured out that its not using bash as the script language. Which that helped.
Code:
#!/bin/bash

name=$1

grep -ir -B 2 -A 4 "$name" /var/lib/rancid/CiscoDevices/configs/ | sed -E 's#/var/lib/rancid/CiscoDevices/configs/##g' | sed -E 's#,v:?-?#: #g'
How can I add the
Code:
"$*"
grep wild card with that variable
Code:
"$name"
so that user doesnt have to specify a
Code:
*
wild card when passing argument to my confsearch script?
 
Hey! I actually figured it out. I'm sure my script isn't perfect but it works for what we need it for.

Thanks for your help!
 
BTW:

If you want your script to default to bash, that first line, called a magic line you have
Code:
#!/bin/bash

would be wrong on freebsdFreeBSD because bash is located at /usr/local/bin/bash

you can type which bash, which will give you the path to the command.

Also which binary such as bash, when the full path not specified, is determined by precedence in your path.

So:
Code:
# echo $PATH
/sbin:/bin:/usr/sbin:/usr/bin:/usr/games:/usr/local/sbin:/usr/local/bin:/root/bin

Let’s say you have a command named blah in /sbin and a command named blah in /usr/sbin

The which blah command would give you the path to the command in /sbin, not /usr/sbin.

However if you always specify complete path to a command than it does not matter where or if its in your path anyway.
 
Back
Top