Grep for string and substitute if exits with a yes/no


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Grep for string and substitute if exits with a yes/no
# 1  
Old 04-30-2014
Grep for string and substitute if exits with a yes/no

Hi

I have 3 files in total. file 1 is enriched.txt file2 is repressed.txt and file 3 is my content.txt

What i need is query the content file against both enriched and repressed and wherever the gensymbol is same in both the files then add a yes value against it

file1
Code:
Gene
ABC
XYZ
MNO

file2
Code:
Gene
PQR
DEF
GHI

file3
Code:
Gene exp1 exp2 exp3 exp4 exp5 enriched repressed
ABC 2 3 4 5 7 yes no 
MNO 5.5 6.8 7.6 5.5 4.2 yes no
DEF 1 4 6 7 9 no yes
GHI 3 5 6 7 8 0 no  yes
LMN 2.2 3.4 5.7 8.9 7.2 no no
TQR 4 5 6 8 8 no no

How can i add a new column to the existing file (file3) and add yes/no against each row in awk?

Thanks,
# 2  
Old 04-30-2014
Code:
awk 'NR == FNR && FNR > 1{a[$0]; n=FNR; next}
  NR == FNR + n && FNR > 1{b[$0]; next}
  FNR == 1 {$9 = "colname"; print $0; next}
  {if($1 in a && $1 in b) {$9 = "yes"} else {$9 = "no"};
  print $0}' enriched.txt repressed.txt content.txt

# 3  
Old 04-30-2014
With more than 50 questions posted, we hope that you have been learning from our suggestions. What code have you tried to solve this problem?
# 4  
Old 04-30-2014
I tried it for a single file search and was not successful

awk 'NR==FNR{keys[$1]; next} {for (key in keys) if ($0 ~ key) {print FILENAME, $0; next} }' file1.txt file3.txt >output.txt
# 5  
Old 04-30-2014
The above given code is print header from all the files. Below is corrected code
Code:
awk 'NR == FNR{if(FNR > 1) a[$0]; n=FNR; next}
  NR == FNR + n{if(FNR > 1) b[$0]; next}
  FNR == 1 {$9 = "colname"; print $0; next}
  {if($1 in a && $1 in b) {$9 = "yes"} else {$9 = "no"};
  print $0}' enriched.txt repressed.txt content.txt

# 6  
Old 04-30-2014
awk -f di.awk enriched.txt repressed.txt content.txt
(the order matters)
where di.awk:
Code:
FILENAME == ARGV[1] {
   if(FNR==1) next
   enr[$1]
   next
}
FILENAME == ARGV[2] {
   if(FNR==1) next
   rep[$1]
   next
}
{
  if (FNR==1) {print $0, "enriched", "repressed";next}
  print $0, ($1 in enr)?"yes":"no", ($1 in rep)?"yes":"no"
}

# 7  
Old 04-30-2014
Thanks for the email. When I apply it to real data I see only one column with no. Also all the values are no. I should be seeing 2 columns one for enriched and one for repressed where it says(yes no) or (no yes) or (no no). Also what is column 9. I have enriched and repressed in columns 7 and 8 respectively.

Thanks,
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Substitute grep command at run time

HI I am trying to use the following code in the shell script (using grep) usage() { echo "Usage: ./$0 <file name> <interval> <pattern>" } METRICS_FILE=$1 INTERVAL=$2 PATTERN="$3" .. if then PATTERN="grep Gx" fi COUNT=`cat ${METRICS_FILE} | "${PATTERN}" |egrep... (8 Replies)
Discussion started by: asifansari
8 Replies

2. Shell Programming and Scripting

[sed]: Substitute a string with a multiline value

Dear all, I try to replace a string of characters in a file (MyFile.txt) by a multiline value of the variable "Myvar": $ cat MyFile.txt DESCRIPTION '@TargetTable SCHEMA' ( @InputFlowDef ); $ The content of Myvar: $ echo "$Myvar" col1 , col2 , col3 $ (4 Replies)
Discussion started by: dae
4 Replies

3. Shell Programming and Scripting

Grep and substitute?

I have to parse ASCII files, output the relevant data to a comma-delimited file and load it into a database table. The specs for the file format have been recently updated and one section is causing problems. This is the original layout for that section. ... (2 Replies)
Discussion started by: alan
2 Replies

4. Shell Programming and Scripting

Substitute string with an index number

Objective is to substitute Jan with 01, Feb with 02 and so on. The month will be provided as input. I could construct below awk and it worked. echo Jun | \ awk 'BEGIN{split("Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec",mon," ")}{ for (i=1;i<=12;i++){ if ($1==mon) printf("%02d\n",i)} }' ... (4 Replies)
Discussion started by: krishmaths
4 Replies

5. UNIX for Dummies Questions & Answers

Find and substitute a string

Hi, I started exploring unix recently. Now i have got a requirement like i have a input file where i am having some strings line by line (One string Might be single line or multiple lines). Now i need find these strings in another file and if its found i have to replace it with another string... (2 Replies)
Discussion started by: Sivajee
2 Replies

6. Shell Programming and Scripting

How to substitute brackets in the beginning of string in perl?

Hi, I have a string like this user can specify different query sets that is why "or" is mentioned: $string="]("; or $string="](("; or $string="]((("; or $string="]((((("; (1 Reply)
Discussion started by: vanitham
1 Replies

7. Shell Programming and Scripting

To substitute a string in a line to another string

Suppose, d=ABC*.BGH.LKJ Now I want to replace 'DEFGHIJ' instead of '*.B' and store the value in d. Any Idea? Can we use sed here? The outout should be like this: d=ABCDEFGHIJGH.LKJ Please help.. (4 Replies)
Discussion started by: Niroj
4 Replies

8. Shell Programming and Scripting

Match keyword on string and substitute under vi

Hi guys, with sed when I need to make a substitution inside a line containing a specific keyword, I usually use: sed '/keyword/ s/cat/dog/g' This will substitute "cat" with "dog" on those lines containing "keyword". Now I want to use this inside vi, for several reason that I cannot... (2 Replies)
Discussion started by: lycaon
2 Replies

9. Shell Programming and Scripting

substitute the grep output

I have a file name called fruits. In this file the prices keep on changing & the order in which fruits are listed keep on changing. $ cat fruits fruitname price/pound redapples 30 grapes 50 oranges 20 $echo $custom_price 35 What I want to do is that if the file "fruits" contains... (1 Reply)
Discussion started by: jasmeet100
1 Replies

10. Shell Programming and Scripting

substitute string according line number

Hi all, I have an xml file which have several sections as the following: <process-type id="NIR" module-id="OC4J"> <module-data> <category id="start-parameters"> <data id="java-options" value="-server... (4 Replies)
Discussion started by: nir_s
4 Replies
Login or Register to Ask a Question