LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 05-13-2008, 10:44 AM   #1
cmontr
Member
 
Registered: Sep 2007
Posts: 175

Rep: Reputation: 15
awk or sed help


Could someone help on this please:

I have a text file which contains a value, for example k=a1111, k=a2222, k=a3333 etc. But there is many other garbage between these. What I would like to do it print all k's with their values only. Could anyone help please?

Thank you in advance
 
Old 05-13-2008, 10:54 AM   #2
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374
Hi,

Could you give an example of the input file? It's a bit hard to give a solution if we don't know what to work with
 
Old 05-13-2008, 11:05 AM   #3
cmontr
Member
 
Registered: Sep 2007
Posts: 175

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by druuna View Post
Hi,

Could you give an example of the input file? It's a bit hard to give a solution if we don't know what to work with
Sure: Please see below is an example part of a big text file which contains some cn's in it which I want to print...such I wanted to print it in a format like:

cn=a00000
cn=a11111
cn=a22222


text in:

execpe10189.log:ApplNPC/cn=a000000execpe10189.log:Taters/ou=DPC/cn=a111111execpe10189.log:Tellinters%2fou=DFWNPC%2fcn=a222222 has been accepted.328876550316004567:...%

Thank you very much
 
Old 05-13-2008, 11:11 AM   #4
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374
Hi,

Would this do what you want:

grep -o "cn=a[0-9]\{6\}" infile

Example run:
Code:
$ cat infile
execpe10189.log:ApplNPC/cn=a000000execpe10189.log:Taters/ou=DPC/cn=a111111execpe10189.log:Tellinters%2fou=DFWNPC%2fcn=a222222 has been accepted.328876550316004567:...%
execpe10189.log:ApplNPC/cn=a000000execpe10189.log:Taters/ou=DPC/cn=a111111execpe10189.log:Tellinters%2fou=DFWNPC%2fcn=a222222 has been accepted.328876550316004567:...%

$ grep -o "cn=a[0-9]\{6\}" infile
cn=a000000
cn=a111111
cn=a222222
cn=a000000
cn=a111111
cn=a222222
Hope this helps.
 
Old 05-13-2008, 11:22 AM   #5
cmontr
Member
 
Registered: Sep 2007
Posts: 175

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by druuna View Post
Hi,

Would this do what you want:

grep -o "cn=a[0-9]\{6\}" infile

Example run:
Code:
$ cat infile
execpe10189.log:ApplNPC/cn=a000000execpe10189.log:Taters/ou=DPC/cn=a111111execpe10189.log:Tellinters%2fou=DFWNPC%2fcn=a222222 has been accepted.328876550316004567:...%
execpe10189.log:ApplNPC/cn=a000000execpe10189.log:Taters/ou=DPC/cn=a111111execpe10189.log:Tellinters%2fou=DFWNPC%2fcn=a222222 has been accepted.328876550316004567:...%

$ grep -o "cn=a[0-9]\{6\}" infile
cn=a000000
cn=a111111
cn=a222222
cn=a000000
cn=a111111
cn=a222222
Hope this helps.
Thank much but I got an error:

grep: illegal option -- o

any idea pls ?
 
Old 05-13-2008, 11:26 AM   #6
radoulov
Member
 
Registered: Apr 2007
Location: Milano, Italia/Варна, България
Distribution: Ubuntu, Open SUSE
Posts: 212

Rep: Reputation: 35
Quote:
Originally Posted by cmontr View Post
Thank much but I got an error:

grep: illegal option -- o

any idea pls ?
What's your OS?
 
Old 05-13-2008, 11:34 AM   #7
cmontr
Member
 
Registered: Sep 2007
Posts: 175

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by radoulov View Post
What's your OS?
sorry i forgot to mentioned..it is solaris ...SunOS 5.7
 
Old 05-13-2008, 11:42 AM   #8
radoulov
Member
 
Registered: Apr 2007
Location: Milano, Italia/Варна, България
Distribution: Ubuntu, Open SUSE
Posts: 212

Rep: Reputation: 35
Code:
perl -nle'print $1 while /(cn=.{7})/g' input
 
Old 05-13-2008, 11:47 AM   #9
cmontr
Member
 
Registered: Sep 2007
Posts: 175

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by radoulov View Post
Code:
perl -nle'print $1 while /(cn=.{7})/g' input
I am sorry but perl is not installed in this machine...
perl -v
ksh: perl: not found

Could I use awk or sed to print these? Thnx much again
 
Old 05-13-2008, 11:49 AM   #10
radoulov
Member
 
Registered: Apr 2007
Location: Milano, Italia/Варна, България
Distribution: Ubuntu, Open SUSE
Posts: 212

Rep: Reputation: 35
OK,
could you post a sample of your input file using code tags?
 
Old 05-13-2008, 11:53 AM   #11
cmontr
Member
 
Registered: Sep 2007
Posts: 175

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by radoulov View Post
OK,
could you post a sample of your input file using code tags?
I am not sure if I got it in terms of code tag...I apologize for it. It is just a big text file that contains cn=a11111, cn=a22222, etc and some junk...I just want to extract thse cn's with their values. If I am taking your time, I am so sorry.
 
Old 05-13-2008, 12:01 PM   #12
radoulov
Member
 
Registered: Apr 2007
Location: Milano, Italia/Варна, България
Distribution: Ubuntu, Open SUSE
Posts: 212

Rep: Reputation: 35
Without an exact sample I have to do it the hard way:

Code:
nawk -F'cn=' 'NF>1{for(i=2;i<=NF;i++) printf "cn=%.7s\n", $i}' input
P.S. I'm not sure if you want .6 or .7 chars after cn= ...

Last edited by radoulov; 05-13-2008 at 12:02 PM.
 
Old 05-13-2008, 12:01 PM   #13
cmontr
Member
 
Registered: Sep 2007
Posts: 175

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by cmontr View Post
I am not sure if I got it in terms of code tag...I apologize for it. It is just a big text file that contains cn=a11111, cn=a22222, etc and some junk...I just want to extract thse cn's with their values. If I am taking your time, I am so sorry.
using this awk, I am able to print all cn= but not the values...

may be you can help me on this...thnx much again


awk "{ print \"cn= \" $1 }" intext.txt > outtext.txt
 
Old 05-13-2008, 12:05 PM   #14
radoulov
Member
 
Registered: Apr 2007
Location: Milano, Italia/Варна, България
Distribution: Ubuntu, Open SUSE
Posts: 212

Rep: Reputation: 35
Quote:
Originally Posted by cmontr View Post
using this awk, I am able to print all cn= but not the values...

may be you can help me on this...thnx much again


awk "{ print \"cn= \" $1 }" intext.txt > outtext.txt
Did you try the awk code I posted?
 
Old 05-13-2008, 12:37 PM   #15
cmontr
Member
 
Registered: Sep 2007
Posts: 175

Original Poster
Rep: Reputation: 15
Smile

Quote:
Originally Posted by radoulov View Post
Did you try the awk code I posted?
Oh I missed seeing that...I actually ran that and it worked great!!!! Thank you so much!!! This rocks!!!
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to get the pattern using sed or awk? ahpin Programming 3 08-02-2007 04:16 AM
awk and/or sed linux2man Linux - General 7 01-22-2007 11:02 AM
sed/awk sort help Kvetch Programming 17 08-30-2006 08:21 PM
Sed and Awk Gins Programming 7 04-19-2006 11:32 AM
awk/sed help pantera Programming 1 05-14-2004 12:59 AM


All times are GMT -5. The time now is 04:50 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration