SED printing just parts of lines that match an expression


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting SED printing just parts of lines that match an expression
# 1  
Old 04-26-2009
SED printing just parts of lines that match an expression

Hi -

I am guessing this is fairly simple for someone .. but I can not quite figure it out. I need a sed command to print just parts of lines from a file.

e.g. filea.txt
4710451 : Success : MODIFY : cn=user1,dc=org,dc=uk
Message log started
Message log ended
4710452 : Success : MODIFY : cn=user2,dc=org,dc=uk
Mesage log started
4710453 : Success : MODIFY : cn=user3,dc=org,dc=uk
Message log ended
Message log started
Message log ended
Message log started
4710454 : Success : ADD : cn=user4,dc=org,dc=uk
Message log ended
Message log started
4710456 : Success : DELETE : cn=user5,dc=org,dc=uk
Message log ended
Message log started
Message log ended
Message log started

The sed command would leave :-
cn=user1,dc=org,dc=uk
cn=user2,dc=org,dc=uk
cn=user3,dc=org,dc=uk
cn=user4,dc=org,dc=uk
cn=user5,dc=org,dc=uk

Any help appreciated.
Cheers
# 2  
Old 04-26-2009
Try with cut

cat filea.txt | cut -d ':' -f4
# 3  
Old 04-26-2009
Try this.

awk -F "cn" '{print $1}' filename



cheers,
Devaraj Takhellambam
# 4  
Old 04-26-2009
Hi Devaraj-

Thanks for the suggestion .. that actually prints everything that I dont want Smilie

awk -F "cn" '{print $1}' filename

Any ideas as to what the inverse of this command is please?
# 5  
Old 04-26-2009
Code:
$
$ cat filea.txt
4710451 : Success : MODIFY : cn=user1,dc=org,dc=uk
Message log started
Message log ended
4710452 : Success : MODIFY : cn=user2,dc=org,dc=uk
Mesage log started
4710453 : Success : MODIFY : cn=user3,dc=org,dc=uk
Message log ended
Message log started
Message log ended
Message log started
4710454 : Success : ADD : cn=user4,dc=org,dc=uk
Message log ended
Message log started
4710456 : Success : DELETE : cn=user5,dc=org,dc=uk
Message log ended
Message log started
Message log ended
Message log started
$
$ grep cn= filea.txt | awk -F" : " '{print $4}'
cn=user1,dc=org,dc=uk
cn=user2,dc=org,dc=uk
cn=user3,dc=org,dc=uk
cn=user4,dc=org,dc=uk
cn=user5,dc=org,dc=uk
$
$

Another method:

Code:
$
$ awk -F " : " '{print $4}' filea.txt | sed '/^$/d'
cn=user1,dc=org,dc=uk
cn=user2,dc=org,dc=uk
cn=user3,dc=org,dc=uk
cn=user4,dc=org,dc=uk
cn=user5,dc=org,dc=uk
$


HTH,
tyler_durden
# 6  
Old 04-26-2009
Thanks for the help.
I have gone with from all suggestions ....

sed -n '/cn=.*dc=uk/p' filea.txt | awk -F" : " '{print $4}' > new_filea.txt
# 7  
Old 04-26-2009
Quote:
Originally Posted by sniper57
Hi Devaraj-

Thanks for the suggestion .. that actually prints everything that I dont want Smilie

awk -F "cn" '{print $1}' filename

Any ideas as to what the inverse of this command is please?
Oh, I misinterpreted your requirement. for the inverser, try this
$ awk -F ":" '{print $4}' filename| sed '/^$/d'
cn=user1,dc=org,dc=uk
cn=user2,dc=org,dc=uk
cn=user3,dc=org,dc=uk
cn=user4,dc=org,dc=uk
cn=user5,dc=org,dc=uk


cheers,
Devaraj Takhellambam
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Grep or sed - printing line only with exact match

Hello. In my script, some command return : q | kernel-default | package | 3.19.0-1.1.g8a7d5f9 | x86_64 | openSUSE-13.2-Kernel_stable_standard | kernel-default | package | 3.19.0-1.1.g8a7d5f9 | i586 | openSUSE-13.2-Kernel_stable_standard | kernel-default ... (3 Replies)
Discussion started by: jcdole
3 Replies

2. Shell Programming and Scripting

sed , awk script for printing matched line before regular expression

hi All , I am having a large file with lots of modules as shown below ############################################### module KKK kksd kskks jsn;lsm jsnlsn; Ring jjsjsj kskmsm jjs endmodule module llll 1kksd11 k232skks j33sn;l55sm (6 Replies)
Discussion started by: kshitij
6 Replies

3. Shell Programming and Scripting

Regular expression to match multiple lines?

Using a regular expression, I would like multiple lines to be matched. By default, a period (.) matches any character except newline. However, (?s) and /s modifiers are supposed to force . to accept a newline and to match any character including a newline. However, the following two perl... (4 Replies)
Discussion started by: LessNux
4 Replies

4. Shell Programming and Scripting

Printing next 6 lines from of pattern match

Hi, i have a big file having many opcodes. if (opcode="01110000000100000000" ) then --fadd result.opcode := "01110000000100000000"; result.s0 := '1'; result.s1 := '1'; result.s2 := '0'; result.inst := '0'; result.scalar := '1';... (7 Replies)
Discussion started by: twistedpair
7 Replies

5. Shell Programming and Scripting

Sed: Splitting A large File into smaller files based on recursive Regular Expression match

I will simplify the explaination a bit, I need to parse through a 87m file - I have a single text file in the form of : <NAME>house........ SOMETEXT SOMETEXT SOMETEXT . . . . </script> MORETEXT MORETEXT . . . (6 Replies)
Discussion started by: sumguy
6 Replies

6. Shell Programming and Scripting

Printing several lines of a file after a specific expression

Hello, I want to print a number of lines of a file after a specific expression of a line. I have this sed command but it prints only 1 line after the expression. How could I adapt it to print for instance 10 lines after or 15 lines after ? sed -n '/regexp/{n;p;}' Thx & Regs, Rany. (5 Replies)
Discussion started by: rany1
5 Replies

7. Shell Programming and Scripting

sed not printing lines before a regular expression.

Hey, I found a way to print the lines which is just before a regular expression, not including the expression. sed -n '/regexp/{n;p;}' myfile Now I'm looking for a way to print all lines, exept the regular expression and also the line before the same regular expression. Use code tags. (1 Reply)
Discussion started by: Livio
1 Replies

8. Solaris

sed command to print lines after expression

Hi guys. I need a sed command to print like 10 lines after a regular expression is found in the log. Can anyone help me out. Thanks ---------- Post updated at 10:52 AM ---------- Previous update was at 10:34 AM ---------- never mind. I just did the search bewteen two expressions. (1 Reply)
Discussion started by: jamie_collins
1 Replies

9. Shell Programming and Scripting

sed problem - delete all lines until a match on 2 lines

First of all, I know this can be more eassily done with perl or other scripting languages but, that's not the issue. I need this in sed. (or wander if it's possible ) I got a file (trace file to recreate the control file from oracle for the dba boys) which contains some lines another line... (11 Replies)
Discussion started by: plelie2
11 Replies

10. Shell Programming and Scripting

Perl: Printing Multiple Lines after pattern match

Hello People, Need some assistance/guidance. OUTLINE: Two files (File1 and File2) File1 has some ids such as 009463_3922_1827 897654_8764_5432 File2 has things along the lines of: Query= 009463_3922_1827 length=252 (252 letters) More stufff here ... (5 Replies)
Discussion started by: Deep9000
5 Replies
Login or Register to Ask a Question