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


Login or Register for Dates, Times and to Reply

 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Grep for string and substitute if exits with a yes/no
# 8  
Quote:
Originally Posted by Diya123
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,
not sure who this statement is for.......
# 9  
Quote:
Originally Posted by Diya123
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
Maybe I'm confused about what you're trying to do. I assumed that your file3.txt on is something like:
Code:
Gene exp1 exp2 exp3 exp4 exp5
ABC 2 3 4 5 7
MNO 5.5 6.8 7.6 5.5 4.2
DEF 1 4 6 7 9
GHI 3 5 6 7 8 0 
LMN 2.2 3.4 5.7 8.9 7.2
TQR 4 5 6 8 8

and that the contents of file3 you showed us in the 1st message in this thread was the desired output. You could do that with something like:
Code:
awk '
FNR == 1 {
	if(++f > 2) print $0, "enriched", "repressed"
	next
}
f < 3 {	gene[f,$1]
	next
}
{	print $0, (1,$1) in gene ? "yes" : "no", (2,$1) in gene ? "yes" : "no"
}' file[1-3].txt

The test in your code:
Code:
 if ($0 ~ key)

would never match. For example, on the 1st data line in file3.txt when key is ABC, the test would expand to:
Code:
if ("ABC 2 3 4 5 7" ~ "ABC")

Perhaps you meant:
Code:
if ($1 == key)

But, I thought you were trying to add two columns of "yes" or "no" to the contents of file3.txt at the end of the lines. Instead you're adding the string "file3.txt" to the start of a line from file3.txt???

Please explain more clearly what file3.txt is on input and exactly what output you want to produce.
This User Gave Thanks to Don Cragun For This Post:
# 10  
Thanks vgersh99. It was for Srini.

Your code worked. but I need the enriched and repressed results column to be tab delimited rather than space.
# 11  
Quote:
Originally Posted by Diya123
Thanks vgersh99. It was for Srini.

Your code worked. but I need the enriched and repressed results column to be tab delimited rather than space.
awk -v OFS='\t' -f di.awk enriched.txt repressed.txt content.txt
# 12  
I misunderstood your requirement.
And the email you received must be an automated from unix.com Smilie
Code:
awk 'NR == FNR{if(FNR > 1) a[$0];
    n=NR;
    next}
  NR == FNR + n{if(FNR > 1) b[$0];
    next}
  FNR == 1{$7="enriched";
    $8="repressed";
    print $0;
    next}
  {$7 = $8 = "no";
  if($1 in a) $7 = "yes";
  if($1 in b) $8 = "yes";
  print $0}' OFS='\t' enriched.txt repressed.txt content.txt

# 13  
thank you Srini and vgersh99. Both of your codes worked.
Login or Register for Dates, Times and to Reply

Previous Thread | Next Thread
Thread Tools Search this Thread
Search this Thread:
Advanced Search

Test Your Knowledge in Computers #440
Difficulty: Medium
The term "GPU" was coined by Nvidia in 1994.
True or False?

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. Programming

Substitute string using location (preferably perl).

I have a string like. ATATATATTATTATATTATATTATT I want to substitute the characters to "C" by using these locations 3 7 10 18 15 20 desired Output: ATCCCCCTTACCCCCCCCCCTTATT any clue will be great help. :wall: thanks in advance. (2 Replies)
Discussion started by: admax
2 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

Featured Tech Videos