Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

grab particular text from file 1

Status
Not open for further replies.

dbase77

Technical User
Apr 23, 2002
591
IE
Hi All,

I have a file which contains many line of below. Is there any way just to get "USER=ksmith" part? The username will be varied.

===============
23-JAN-2008 10:26:40 * (CONNECT_DATA=(SID=P)(CID=(PROGRAM=C:\Program Files\Quest Software\Toad for Oracle\TOAD.exe)(HOST=A002168)(USER=ksmith))) * (ADDRESS=(PROTOCOL=tcp)(HOST=10.200.100.196)(PORT=1319)) * establish * P * 0

23-JAN-2008 09:41:31 * (CONNECT_DATA=(SID=P)(CID=(PROGRAM=N:\Program Files\Microsoft Office\Office10\MSACCESS.EXE)(HOST=CITRIX3)(USER=dcantona))) * (ADDRESS=(PROTOCOL=tcp)(HOST=10.200.10.15)(PORT=2633)) * establish * P * 0
================

Thank you for your help.
 
You may try this:
awk '/\(USER=/{x=$0;sub(/.*\(USER=/,"USER=",x);sub(/).*/,"",x);print x}' /path/to/input

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
Hi,

Thanks for the input. But I got an error:

awk: syntax error near line 1
awk: illegal statement near line 1
awk: syntax error near line 1
awk: illegal statement near line 1

I'm on solaris 8 OS.

Thanks.
 
Something like
Code:
sed 's/.*(\(USER=[^)]*\).*/\1' < /path/to/input
Tested in bash

On the internet no one knows you're a dog

Columb Healy
 
columb,

Error sed: command garbled: s/.*(\(USER=[^)]*\).*/\1 .

Im on ksh shell by the way.

Thanks.
 
There are serveral possible solutions, depending on your knowledge and preferences.
Sure it can be done with awk or sed, but I cannot remember all of those syntax rules, and it seems it is not easy for others either. [wink]
Because I am a bit lazy, I often use cut for this type of tasks:

Count the ('s, and cut out all between the 7th and 8th, or whatever:
cut -d\( -f8 filename

This will give you something like:
USER=ksmith))) *
USER=dcantona))) *

And how you get rid of the unneeded bytes at the end is left as an exercise to the reader.
[wink]
hope this helps
 
Sorry, cut and paste error with the sed command
it should have been
Code:
sed 's/.*(\(USER=[^)]*\).*/\1/' < /path/to/input
Which should work in ksh as well.
What it does is break the line down to
[ol]
[li]Take:-
[ol]
[li]lots of characters - .*[/li]
[li]followed by an opening bracket - ([/li]
[li]then start the capture - \([/li]
[li]then the string USER=[/li]
[li]then anything except a closing bracket - [^)]*[/li]
[li]then close the capture - \)[/li]
[li]then lots of anything - .*[/li]
[/ol][/li]
[li]and replace it with the capture - \1[/li]
[/ol]

On the internet no one knows you're a dog

Columb Healy
 
I'm on solaris 8 OS
So, use nawk.

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
BTW
hoinz has a point, if the line format is fixed then cut may well do the job with a lot less agro.

Personally, at first I thought I'd never get the hang of awk or sed expressions but, having lived with the O'Reilly sed and awk book on my desk for many years, I'm now glad I took the time to add such powerful tools to my toolset.

On the internet no one knows you're a dog

Columb Healy
 
PHV,

Nawk produced this error:

nawk: illegal primary in regular expression ).* at .*
source line number 1
context is
>>> /\(USER=/{x=$0;sub(/.*\(USER=/,"USER=",x);sub(/).*/,"",x) <<<
 
Columb,

The line is not fixed. If it is fixed, I can use cut. I got it done using cut btw, but too many piping to cut command which is not good. Awk or sed are the best solution. Hence I asked question in here.

Thank you for the sed command by the way. It worked.
 
Into the realms of the absurd, you could try
cat file|xargs -I {} expr "{}" : ".*USER=\(.*\)))).*
 
Solaris seems very funny ...
what about this ?
nawk '/\(USER=/{x=$0;sub(/.*\(USER=/,"USER=",x);sub(/\).*/,"",x);print x}' /path/to/input

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
columb,

thanks for explaining your command.
My knowledge of sed is very limited, and you helped me to understand a bit more of it.
(And yes, I do know that man sed will help a lot, too.)
[smile]
 
My answer:

cat << EOF |
===============
23-JAN-2008 10:26:40 * (CONNECT_DATA=(SID=P)(CID=(PROGRAM=C:\Program Files\Quest Software\Toad for Oracle\TOAD.exe)(HOST=A002168)(USER=ksmith))) * (ADDRESS=(PROTOCOL=tcp)(HOST=10.200.100.196)(PORT=1319)) * establish * P * 0

23-JAN-2008 09:41:31 * (CONNECT_DATA=(SID=P)(CID=(PROGRAM=N:\Program Files\Microsoft Office\Office10\MSACCESS.EXE)(HOST=CITRIX3)(USER=dcantona))) * (ADDRESS=(PROTOCOL=tcp)(HOST=10.200.10.15)(PORT=2633)) * establish * P * 0
================

EOF
sed -e 's/[^A-Za-z0-9=]/ /g' |
tr ' ' '\012' |
grep USER |
cut -d= -f2 |
while read user_nm ; do

echo USER: $user_nm

done
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top