Visit Our UNIX and Linux User Community


Print two matched words from the same line


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Print two matched words from the same line
# 1  
Old 11-02-2009
Print two matched words from the same line

Hi experts

I need to pick 2 matched words from the same line.....
I have given below an example file

eg:
Code:
O14757 hsa04110 hsa04115 2 P38398 hsa04120 1 
O15111 hsa04010 hsa04210 hsa04920 hsa04620 hsa04660 hsa04662 hsa05200 hsa05212 hsa05221 hsa05220 hsa05215 hsa05222 hsa05120 13 O14920 hsa04010 hsa04210 hsa04910 hsa04920 hsa04620 hsa04660 hsa04662 hsa05200 hsa05212 hsa05221 hsa05220 hsa05215 hsa05222 hsa04930 hsa05120 15 
O15264 hsa04010 hsa04370 hsa04912 hsa04620 hsa04664 hsa04670 hsa05014 hsa05120 8 Q15139 
O15297 hsa04115 1 Q00987 hsa04120 hsa04110 hsa04115 hsa05200 hsa05214 hsa05220 hsa05218 hsa05219 hsa05215 9 
O43464 hsa05012 1 P98170 hsa04120 hsa04210 hsa04510 hsa05200 hsa05222 5

From the above file i have to pick only the 6 letter alphanumerical words(highlighted words) in the given below format

ie,
Code:
 O14757    P38398 
 O15111    O14920

Pls reply me asap

Last edited by pludi; 11-02-2009 at 06:20 AM.. Reason: code tags, please...
# 2  
Old 11-02-2009
Assuming that the number of spaces between the matches in the line don't matter:
Code:
perl -lne '@a=(/([A-Z0-9]{6})/g); print "@a"' test.txt
O14757 P38398
O15111 O14920
O15264 Q15139
O15297 Q00987
O43464 P98170

# 3  
Old 11-02-2009
Hi,

Try this

Code:
#!/usr/bin/perl

while (<>) {
        chomp ;
        @array = split;
        for (@array) {
        print "$_\t" if (length($_) == 6);
        }
        print "\n";
}

# 4  
Old 11-02-2009
Code:
grep -o '[A-Z][0-9]\{5\}' infile|xargs -n2
O14757 P38398
O15111 O14920
O15264 Q15139
O15297 Q00987
O43464 P98170

# 5  
Old 11-03-2009
Code:
while(<DATA>){
	my @tmp = split;
	print join " ", grep {length($_) eq 6} @tmp;
	print "\n";
}
__DATA__
O14757 hsa04110 hsa04115 2 P38398 hsa04120 1 
O15111 hsa04010 hsa04210 hsa04920 hsa04620 hsa04660 hsa04662 hsa05200 hsa05212 hsa05221 hsa05220 hsa05215 hsa05222 hsa05120 13 O14920 hsa04010 hsa04210 hsa04910 hsa04920 hsa04620 hsa04660 hsa04662 hsa05200 hsa05212 hsa05221 hsa05220 hsa05215 hsa05222 hsa04930 hsa05120 15 
O15264 hsa04010 hsa04370 hsa04912 hsa04620 hsa04664 hsa04670 hsa05014 hsa05120 8 Q15139 
O15297 hsa04115 1 Q00987 hsa04120 hsa04110 hsa04115 hsa05200 hsa05214 hsa05220 hsa05218 hsa05219 hsa05215 9 
O43464 hsa05012 1 P98170 hsa04120 hsa04210 hsa04510 hsa05200 hsa05222 5


Previous Thread | Next Thread
Test Your Knowledge in Computers #360
Difficulty: Medium
The find command first appeared in Version 4 AT&T UNIX.
True or False?

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to print previous line of multiple pattern matched line?

Hello, I have below format log file, Comparing csv_converted_files/2201/9747.1012H67126.5077292103609547345.csv and csv_converted_files/22019/97447.1012H67126.5077292103609547345.csv Comparing csv_converted_files/2559/9447.1012H67126.5077292103609547345.csv and... (6 Replies)
Discussion started by: arvindshukla81
6 Replies

2. Shell Programming and Scripting

How to print two matched patterns only from each line?

My input looks like this. # Lot Of CODE Before AppType_somethinglese=$(cat << EOF AppType_test1='test-tool/blatest-tool-ear' AppType_test2='test/blabla-ear' # Lot Of CODE After I want to print text betwen 1) _ and = and 2)/ and ' from each line and exclude lines with "EOF". Output... (2 Replies)
Discussion started by: kchinnam
2 Replies

3. UNIX for Advanced & Expert Users

To print from the first line until pattern is matched

Hi I want to print the line until pattern is matched. I am using below code: sed -n '1,/pattern / p' file It is working fine for me , but its not working for exact match. sed -n '1,/^LAC$/ p' file Input: LACC FEGHRA 0 LACC FACAF 0 LACC DARA 0 LACC TALAC 0 LAC ILACTC 0... (8 Replies)
Discussion started by: Abhisrajput
8 Replies

4. Shell Programming and Scripting

Regex: print matched line and exact pattern match

Hi experts, I have a file with regexes which is used for automatic searches on several files (40+ GB). To do some postprocessing with the grep result I need the matching line as well as the match itself. I know that the latter could be achieved with grep's -o option. But I'm not aware of a... (2 Replies)
Discussion started by: stresing
2 Replies

5. Shell Programming and Scripting

Print line between two patterns when a certain pattern matched

Hello Friends, I need to print lines in between two string when a keyword existed in those lines (keywords like exception, error, failed, not started etc). for example, input: .. Begin Edr ab12 ac13 ad14 bc23 exception occured bd24 cd34 dd44 ee55 ff66 End Edr (2 Replies)
Discussion started by: EAGL
2 Replies

6. Shell Programming and Scripting

Print only matched string instead of entire line

Hi, I have a file whose lines are something like Tchampionspsq^@~^@^^^A^@^@^@^A^A^A^@^@^@^@^@^@^@^@^@^@^A^@^@^@^@^?^@^@^@^@^@^@^@?^@^@^@^@^@^@pppsq^@~^@#@^@^@^@^@^@^Hw^H^@^@^@^K^@^@^@^@xp^At^@^FTtime2psq^@ ~^@^^^A^@^@^@^B^A I need to extract all words matching T*psq from the file. Thing is... (4 Replies)
Discussion started by: shekhar2010us
4 Replies

7. Shell Programming and Scripting

print number of words in each line

Hi, Please suggest a way to print number of words in the end of each line. <input file> red aunt house blue sky bat and ball game <output file> red aunt house 3 blue sky 2 bat and ball game 4 Thanks! (2 Replies)
Discussion started by: mira
2 Replies

8. Shell Programming and Scripting

AWK Print Line If Specific Character Is Matched

Hello, I have a file as such: FFFFFFF6C000000 225280 225240 - - rwxs- FFFFFFFF79C00000 3240 3240 - - rwxs- FFFFFFFF7A000000 4096 4096 - - rwxs- FFFFFFFF7A400000 64 64 ... (3 Replies)
Discussion started by: PointyWombat
3 Replies

9. Shell Programming and Scripting

How to print the words in the same line with space or to the predefined line?

HI, cat test abc echo "def" >> test output is cat test abc def the needed output is cat test abc def and so on (5 Replies)
Discussion started by: jobycxa
5 Replies

10. Shell Programming and Scripting

print only last two words of a line

can u help me out to print last two words of each sentence of a file. for example. contents of input file: i love songs my favourite songs sent songs all kind good buddy Ouput file should contain: love songs favourite songs sent all kind good buddy (5 Replies)
Discussion started by: pradeepreddy
5 Replies

Featured Tech Videos