Sponsored Content
Top Forums Shell Programming and Scripting Grep for string and substitute if exits with a yes/no Post 302899686 by Don Cragun on Wednesday 30th of April 2014 04:06:01 PM
Old 04-30-2014
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 More Discussions You Might Find Interesting

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

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

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

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

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

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

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

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

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

10. 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
All times are GMT -4. The time now is 10:04 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy