Find a pattern and print next all character to next space

Hi,
I have a big inventory file that is NOT sorted is any way.
The file is have "tagged" information like the ip address "*IP=" or the name "*NM=" .
How do I get just the ip address or the name and not the whole line?

I have tried to use AWK without any success. I always get the whole line or just the wrong pattern.

Before:
inventory_number network_device_type placment *IP=ipaddress *NM=name *sv=severity *login=login cred.
inventory_number network_device_type *NM=name placment *IP=ipaddress *sv=severity *login=login cred.
inventory_number network_device_type placment *IP=ipaddress *NM=name *sv=severity *login=login cred.
inventory_number network_device_type placment *sv=severity *IP=ipaddress *NM=name *login=login cred.

After:
ipaddress
ipaddress
ipaddress
ipaddress

Can anyone help me with this?
Thanks!!!

You should show what you have done rather than tell us the results of your efforts. This way it would be easier to tell you where you have gone wrong. Recommended reading: How to ask questions the smart way

Back to your problem: The following little sed script might do what you need, on the presumption that each line only contains one IP-address and one name, like in your example:

getting the IP address:

sed -n 's/.*\*IP=\([^ ]*\) .*$/\1/p' /path/to/inputfile

similar, for name:

sed -n 's/.*\*NM=\([^ ]*\) .*$/\1/p' /path/to/inputfile

I hope this helps.

bakunin

Thanks.
I'll giv it a try.

I forgot to tell you what I am trying to do this on a Windows machine with UnxUtils "installed".

Your right!!!
I will do better in the future to show what I have done.
I'm new to scripting in shell code.

I have tried the following code without any real progress.:confused:

cat file.txt |gawk "match($0,/*IP=/){print substr($0,RSTART+4,15) "IP" }"

The problem is that I want to print to the next white space... not only 15 characters...

/Pierre

---------- Post updated 22-04-10 at 12:47 AM ---------- Previous update was 21-04-10 at 11:15 AM ----------

Hi Again,

Due to the fact that I'm is forced to work with windows I can't get it to work.
I have changed the ' to " and I still donsen't work.
I think it has to be somthing with the windows formating.

sed -n "s/.*\*IP=\([^ ]*\) .*$/\1/p" /path/to/inputfile

Result:
1
1
1
1
1
1
....

/Pierre

edit by bakunin: i edited into the text the code-tags you should have used yourself when posting code or code-snippets. Thanks for doing this yourself from now on.

The command i wrote would have worked on the text you provided as example, at least it works here. If this is not the case on your side please provide a sample of your text and some version information about your system (OS, version, ...)

bakunin

Hi agian,

sorry but I have tried everything so here are a pice of my orignal file.

i3421 cisco_2960 ty-3 *IP=148.138.210.226 *NM=typ_2960_ty_3 *CDP3 *sv1 *login_typass l_090212 
i5432 cisco_2950 ty-3 *CDP3 *IP=148.138.210.9 *NM=typ_2950_ty_5 *sv1 *login_typass  l_0211xx
i3465 cisco_2950 ty-3 *CDP3 *NM=typ_2950_ty_6 *IP=148.138.210.10  *sv1 *login_typass  l_0211xx
i2342 cisco_2950 ty-3 *  CDP3 *IP=148.138.211.204 *NM=typ_2950_ty_11 *sv1 *login_typass l_050105 
i7653 cisco_2960 ty-3 *CDP3  *IP=148.138.210.18 *NM=typ_2960_ty_2 *sv1 *login_typass l_090212 
i1234 cisco_29608 ty-3  *NM=typ_29608_ty_1 *IP=148.138.203.112 *sv1 *login_typass l_080930 
i6342 cisco_29608 ty-3 *IP=148.138.203.113 *NM=typ_29608_ty_2 *sv1 *login_typass l_080930 

I have tried to use the following combinations to solve it.

sed -n "s/.*\*IP=\([^ ]*\) .*$/\1/p" result.txt
sed -n "s/.*\*IP=\([^ ]*\) .//p" result.txt

The closest I get is this

NM=typ_2960_ty_3 *CDP3 *sv1 *login_typass l_0
NM=typ_2960_ty_2 *sv1 *login_typass l_090212
sv1 *login_typass l_080930
NM=typ_29608_ty_2 *sv1 *login_typass l_080930

I'm using Windows XP Pro. SP3 with UnxUtils.

/Pierre

The version of sed in UnxUtils is quite old, i.e. 3.0.2, according to the UnxUtils website. It might be worth downloading the latest version of sed for Windows, i.e. 4.2, from Sed for Windows.

That did the trick !!!
Why didn't I think of that??

Thanks anyway..

/Pierre

Now I need to search for two patterns on the same line.

*IP= + *NM=

I have tried this without any success.

Can you advice?

It might help a little to learn a few basics of regular expressions. You might want to read this thread for a (very short) introduction on regular expressions.

Here is what extracts from your sample above what you want:

sed -n '/IP=.*NM=/ {
            s/^.*IP=\([^ ]*\) .*NM=\([^ ]*\) .*$/\1 \2/p
            }
        /NM=.*IP=/ {
            s/^.*NM=\([^ ]*\) .*IP=\([^ ]*\) .*$/\2 \1/p
            }' /path/to/file

I hope this helps.

bakunin