Output based on multiple searches


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Output based on multiple searches
# 1  
Old 03-19-2016
Output based on multiple searches

I have a file that looks like this:
Code:
>Sample 539 
GCCCAGCGCGCGILTGCCGCCGTCTCCGCCTGTCJOHNCCGCCATTGCCCCCGGTTAC

I am using the following code to search specific patterns:
Code:
awk '/^>/ { print $0 } NR==2 {if (/GIL/) { print "\t" "1" } else { print "\t" "0" }} NR==2 {if (/JOHN/) { print "\t""\t" "1" } else { print "\t""\t" "0" }} NR==2 {if (/DOE/) { print "\t""\t""\t" "1" } else { print "\t""\t""\t" "0" }} '

The script is horrible but it kinda gives me what I need.
Code:
>Sample 539
        1
                1
                        0

Ideally, this would be the output:
Code:
Sample 539          1          1          0

Any help will be greatly appreciated
# 2  
Old 03-19-2016
print prints a newline at the end.
You can save the individual results in variables then print them in one print statement.
Or you can use printf to print without a newline.
Further, this can make good use of a value-if (from the C language), for example print (/GIL/ ? "1" : "0"). And in this particular case even print (/GIL/) can do it, because the expression yields 1 for true or 0 for false.
# 3  
Old 03-19-2016
Hello Xterra,

Could you please try following and let me know if this helps.
Code:
awk '/>Sample/{print;next} {match($0,/GIL/);if(substr($0,RSTART,RLENGTH)){A++};match($0,/JOHN/);if(substr($0,RSTART,RLENGTH)){B++};;match($0,/DOE/);if(su$0,RSTART,RLENGTH)){C++};printf("%01d %01d %01d\n", A,  B, C)}'  Input_file

Output will be as follows.
Code:
>Sample 539
1 1 0

In case you have more conditions to add into your question, please show us more sample Input_file and sample output with all the clear conditions and requirement details on same.

Thanks,
R. Singh
# 4  
Old 03-19-2016
A little closer

Code:
awk '/^>/ { print;next } NR==2 { a=(/GIL/)} { b=(/JOHN/) } { c=(/DOE/) } {print a"\t"b"\t"c }'

I am still getting two lines:
Code:
>Sample 539
1 1 0

And I need a one liner:
Code:
>Sample 539 1 1 0

# 5  
Old 03-19-2016
Hello Xterra,

Could you please try following and let me know if this helps you.
Code:
awk '/>Sample/{Q=$0;next} {match($0,/GIL/);if(substr($0,RSTART,RLENGTH)){A++};match($0,/JOHN/);if(substr($0,RSTART,RLENGTH)){B++};;match($0,/DOE/);if(substr($0,RSTART,RLENGTH)){C++};printf("%s %01d %01d %01d\n",Q, A,  B, C)}'  Input_file

Output will be as follows.
Code:
>Sample 539  1 1 0

Also if you have to use your shown code then you could try as following too.
Code:
awk '/^>/ { Q=$0;next } NR==2 { a=(/GIL/)} { b=(/JOHN/) } { c=(/DOE/) } {printf("%s %01d %01d %01d\n",Q, a, b, c) }' Input_file

Thanks,
R. Singh
# 6  
Old 03-19-2016
I would be tempted to simplify that a little bit:
Code:
awk '
/^>/ {	printf("%s", substr($0, 2))
	next
}
{	printf("\t%d\t%d\t%d\n", /GIL/, /JOHN/, /DOE/)
	exit
}' file

or if you insist on a 1-liner:
Code:
awk '/^>/{printf("%s",substr($0,2));next}{printf("\t%d\t%d\t%d\n",/GIL/,/JOHN/,/DOE/);exit}' file

both of which produce the output:
Code:
Sample 539 	1	1	0

if file contains the sample input you provided.

As always, if you want to try this on a Solaris/SunOS system, change awk to /usr/xpg4/bin/awk or nawk.
# 7  
Old 03-19-2016
Yes one step closer but still two print statements i.e. two lines!
For one print it is inevitable to store the previous line in a variable. For the current line one can even directly print the expressions (as I see now)
Code:
awk '/^>/ { p=$0; next } { print p "\t" (/GIL/) "\t" (/JOHN/) "\t" (/DOE/) }'

The next jumps to the next cycle so you do not need to filter for line 2.
This User Gave Thanks to MadeInGermany For This Post:
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Execute multiple files in multiple folders and also output on same folder

How to execute multiple files in multiple folders and also output to be generated in the same folder? Hello Team, I have a path like Sanity_test/*/* and it has around 100+ folders inside with files. I would like to run/execute those files and output of execution to be placed on same /... (1 Reply)
Discussion started by: pushpabuzz
1 Replies

2. Shell Programming and Scripting

Need help in awk for multiple searches

I have a below file RCS File name : abc.txt something something .... symbolic names: implemented : 1.1 ssssssumthing Revision 1.2 date : 12/12/12 author : abc Revision 1.1 date : 11/11/11 author xyz So now , in this file i have to first look for the implemented... (1 Reply)
Discussion started by: ashishagg2005
1 Replies

3. Shell Programming and Scripting

Create Multiple UNIX Files for Multiple SQL Rows output

Dear All, I am trying to write a Unix Script which fires a sql query. The output of the sql query gives multiple rows. Each row should be saved in a separate Unix File. The number of rows of sql output can be variable. I am able save all the rows in one file but in separate files. Any... (14 Replies)
Discussion started by: Rahul_Bhasin
14 Replies

4. Shell Programming and Scripting

Grep from multiple patterns multiple file multiple output

Hi, I want to grep multiple patterns from multiple files and save to multiple outputs. As of now its outputting all to the same file when I use this command. Input : 108 files to check for 390 patterns to check for. output I need to 108 files with the searched patterns. Xargs -I {} grep... (3 Replies)
Discussion started by: Diya123
3 Replies

5. Shell Programming and Scripting

Nawk help searching for multiple lines and multiple searches

I use this command to find a search (Nr of active alarms are) and print one line before and 10 lines after the search keywords. nawk 'c-->0;$0~s{if(b)for(c=b+1;c>1;c--)print r;print;c=a}b{r=$0}' b=1 a=10 s="Nr of active alarms are:" *.log However, I would like to know how to tell it to print... (3 Replies)
Discussion started by: tthach830
3 Replies

6. Shell Programming and Scripting

Converting line output to column based output

Hi Guys, I am trying to convert a file which has a row based output to a column based output. My original file looks like this: 1 2 3 4 5 6 1 2 3 1 2 3 (8 Replies)
Discussion started by: npatwardhan
8 Replies

7. Shell Programming and Scripting

Find multiple patterns on multiple lines and concatenate output

I'm trying to parse COBOL code to combine variables into one string. I have two variable names that get literals moved into them and I'd like to use sed, awk, or similar to find these lines and combine the variables into the final component. These variable names are always VAR1 and VAR2. For... (8 Replies)
Discussion started by: wilg0005
8 Replies

8. Shell Programming and Scripting

Perl syntax for sed searches

I am aware that Perl has a lot of features that originally came from sed and awk. I have a pattern that I am using like this: sed -n '/|Y|/p' I want to do the same thing in Perl and be able to either save that value in some kind of variable or array or potentially write it out to a file. ... (11 Replies)
Discussion started by: masinick
11 Replies

9. Shell Programming and Scripting

Creating searches?

Hello. Could do with some help on where to get started really. If anyone could help me it would be greatly appreciated. I have been working on this for a while now and I don't really know where to start but I am looking into creating a script that will process website hit files and output... (2 Replies)
Discussion started by: amatuer_lee_3
2 Replies

10. UNIX for Dummies Questions & Answers

grep: do multiple searches?

I want to search the file /etc/passwd for all lines containing 'csh' but exlude all those lines that have '/usr' in them and dump the results into the file result. IMPORTANT: I need to do this in one command line. The following does not work: grep -v \(\/usr\) \(csh\) /etc/passwd >... (4 Replies)
Discussion started by: sdemba
4 Replies
Login or Register to Ask a Question