Visit Our UNIX and Linux User Community


Search for the two patterns and print everything in between


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Search for the two patterns and print everything in between
# 1  
Old 01-16-2012
Search for the two patterns and print everything in between

Hi all,
I have a file having data:
Code:
@HWUSI-EAS1727:19:6:1:3674:984:0:1#GTTAATA
NTTGGGTTTTCT
@HWUSI-EAS1727:19:6:1:3674:984:0:1#GTTA...
NTTGGGTTTTCT
@HWUSI-EAS1727:19:6:1:3674:984:0:1#.....CT
NTTGGGTTTTCT

I want to print everything starting from # till line ends.
can you please help me how to do that??
I am trying in perl, able to parse "#......." pattern but not able to understand how to make it print??

Thanks...

Last edited by Franklin52; 01-17-2012 at 03:16 AM.. Reason: Please use code tags for code and data samples, thank you
# 2  
Old 01-16-2012
Code:
awk -F'#' '{ print "#"$2 }' inputfile > outputfile

If not on linux, try nawk or gawk.
This User Gave Thanks to Corona688 For This Post:
# 3  
Old 01-16-2012
In sh or ksh or bash
Code:
IFS="#"
while read a b
echo "$b"
done <inputfile

This User Gave Thanks to jgt For This Post:
# 4  
Old 01-16-2012
Code:
sed -n 's/.*#//p' infile

Code:
GTTAATA
GTTA...
.....CT

or do you need to combine the next line?
Code:
sed -n 'N;s/\n//;s/.*#//p' infile

Code:
GTTAATANTTGGGTTTTCT
GTTA...NTTGGGTTTTCT
.....CTNTTGGGTTTTCT

This User Gave Thanks to Scrutinizer For This Post:
# 5  
Old 01-16-2012
Bug

Finally I used this code...by Scrutinizer...
thank you very much for replies...

I didnt know sed can be so useful and easy...now along with perl need to learn sed...

Quote:
Originally Posted by Scrutinizer
Code:
sed -n 's/.*#//p' infile


Last edited by jim mcnamara; 01-16-2012 at 11:09 PM.. Reason: [/quote]
# 6  
Old 01-17-2012
Using Perl, you could search for all text from "#" to end of line and replace the entire line by the result -

Code:
$
$ cat f31
@HWUSI-EAS1727:19:6:1:3674:984:0:1#GTTAATANTTGGGTTTTCT
@HWUSI-EAS1727:19:6:1:3674:984:0:1#GTTANTTGGGTTTTCT
@HWUSI-EAS1727:19:6:1:3674:984:0:1#CTNTTGGGTTTTCT
$
$
$ perl -lne 's/^.*#(.*)$/$1/ and print' f31
GTTAATANTTGGGTTTTCT
GTTANTTGGGTTTTCT
CTNTTGGGTTTTCT
$
$

Or you could remove everything up to and including the "#" character from each line -

Code:
$
$ perl -lne 's/^.*#// and print' f31
GTTAATANTTGGGTTTTCT
GTTANTTGGGTTTTCT
CTNTTGGGTTTTCT
$
$

Or you could split each line on "#" as the delimiter, assign the chunks to an array and print just the 2nd element of the array -

Code:
$
$ perl -lne '@x = split/#/ and print $x[1]' f31
GTTAATANTTGGGTTTTCT
GTTANTTGGGTTTTCT
CTNTTGGGTTTTCT
$
$
$ # Or more succintly...
$
$ perl -lne 'print ((split/#/)[1])' f31
GTTAATANTTGGGTTTTCT
GTTANTTGGGTTTTCT
CTNTTGGGTTTTCT
$
$ # Or even more succintly...
$
$ perl -plne '$_=(split/#/)[1]' f31
GTTAATANTTGGGTTTTCT
GTTANTTGGGTTTTCT
CTNTTGGGTTTTCT
$
$

Or you could even find out the index of the "#" character in each line, extract the substring of each line that starts from that index onwards, and then print that substring, like so -

Code:
$
$
$ perl -lne 'print substr($_,index($_,"#")+1)' f31
GTTAATANTTGGGTTTTCT
GTTANTTGGGTTTTCT
CTNTTGGGTTTTCT
$
$

tyler_durden
This User Gave Thanks to durden_tyler For This Post:

Previous Thread | Next Thread
Test Your Knowledge in Computers #228
Difficulty: Easy
According to NetMarketShare, in September 2019 Linux had a 5% global market share of the desktop / laptop computer market.
True or False?

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Find matched patterns and print them with other patterns not the whole line

Hi, I am trying to extract some patterns from a line. The input file is space delimited and i could not use column to get value after "IN" or "OUT" patterns as there could be multiple white spaces before the next digits that i need to print in the output file . I need to print 3 patterns in a... (3 Replies)
Discussion started by: redse171
3 Replies

2. Shell Programming and Scripting

Print between multiple patterns

Hello Gurus, I have a file this Dir Path 1 Connection pool="somename"; "DataSource Name"="DS name"; Password="pwd"; User Id="uid";some other fields Dir Path2 Password="pwd2"; User id="uid2"; Connection pool="somename2"; "datasource name"="DS name2";some other fields. Under each dir... (14 Replies)
Discussion started by: sirababu
14 Replies

3. Shell Programming and Scripting

Print all lines between patterns

Hi Gurus, I have a requirement where I need to display all lines between 2 patterns except the line where the first pattern in it. I tried the following command using awk but it is printing all lines except the lines where the 2 patterns exist. awk '/TRANSF_/{ P=1; next } /Busy/ {exit} P'... (9 Replies)
Discussion started by: svajhala
9 Replies

4. Shell Programming and Scripting

How to print only lines in between patterns?

Hi, I want to print only lines (green-italic lines) in between first and last strings in column 9. there are different number of lines between each strings. 10 AUGUSTUS exon 4558 4669 . - . 10.g1 10 AUGUSTUS exon 8771 8889 . ... (6 Replies)
Discussion started by: jamo
6 Replies

5. Shell Programming and Scripting

Search two patterns using awk to print the variable sum

Coins.txt: gold 1 1986 USA American Eagle gold 1 1908 Austria-Hungary Franz Josef 100 Korona silver 10 1981 USA ingot gold 1 1984 Switzerland ingot gold 1 1979 RSA Krugerrand gold 0.5 1981 RSA Krugerrand gold 0.1 1986 PRC Panda silver 1 1986 USA Liberty dollar gold 0.25 1986 USA Liberty... (2 Replies)
Discussion started by: Ramesh M
2 Replies

6. Shell Programming and Scripting

Need to print between patterns AND a few lines before

I need to print out sections (varying numbers of lines) of a file between patterns. That alone is easy enough: sed -n '/START/,/STOP/' I also need the 3 lines BEFORE the start pattern. That alone is easy enough: grep -B3 START But I can't seem to combine the two so that I get everything between the... (2 Replies)
Discussion started by: Finja
2 Replies

7. Shell Programming and Scripting

To print certain patterns in a column

Hi, From my input files, I want to print $1, $2 and only certain pattern in $4 (EC). I use this code but it print all the words in $4 awk -F"\t" '$4 {print $1,$2,$4}I just want EC follows by the numbers in $4 The input file as follows:- Entry Entry name Status Names Q01284 ... (7 Replies)
Discussion started by: redse171
7 Replies

8. Shell Programming and Scripting

awk: Multiple search patterns & print in an one liner

I would like to print result of multiple search pattern invoked from an one liner. The code looks like this but won't work gawk -F '{{if ($0 ~ /pattern1/) pat1=$1 && if ($0 ~ /pattern2/) pat2=$2} ; print pat1, pat2}' Can anybody help getting the right code? (10 Replies)
Discussion started by: sdf
10 Replies

9. Shell Programming and Scripting

Perl print between 2 patterns

I have been unable to find this anywhere; I have a multiline variable, and I want to print the text between two patterns in that variable. So the variable is My real name is not DeadmanAnd I need the output to be this, by printing between "real" and "not" name is or including the two... (10 Replies)
Discussion started by: killer54291
10 Replies

10. Shell Programming and Scripting

search patterns

hello, i have an input file of about 50,00,000 lines. few of its lines are as follows: <CR:0023498789,TPO-14987084;BO=IC&SUB=ALLP <CF:0023498789,CB=YES;BIL&NC=NO <CF:0023498789,CW=NO;NS=NO <GC:0023498789,CG=YES;TPO&NC=YES <CR:0024659841,TPO-14484621;BO=NO&BA=OC&SUB=ALLH... (1 Reply)
Discussion started by: rochitsharma
1 Replies

Featured Tech Videos