grep a variable from list


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting grep a variable from list
# 1  
Old 11-16-2011
grep a variable from list

I have a script to create a variable from a list, B.txt and then search it in another file, file.txt and then print the pattern line and next line.

Code:
#!/bin/bash
while read a
do
echo "$a" | grep -A 1 $a file.txt > $a\.txt    
done < B.txt

I always get that no such file or directory exists for any $a.txt
Any suggestions

Moderator's Comments:
Mod Comment Suggestion: Watch this Video on how to use code tags

Last edited by pludi; 11-16-2011 at 06:55 PM..
# 2  
Old 11-16-2011
What's in B.txt? What's in file.txt?

The "echo $a" is useless, since you've told grep to look inside file.txt, not standard input. I'm not sure what its purpose was intended to be.

$a.txt should work just as well as $a\.txt.

Quote:
I always get that no such file or directory exists for any $a.txt
What do you mean? The script prints those messages? Or that it doesn't create the files you want?

If $a is ever a string with spaces in it, it will split on those spaces, which will cause the string search string with spaces to make grep look for the string search inside the files "string", "with", "spaces", and "file.txt"

Quote it like "$a" to prevent it splitting.
# 3  
Old 11-17-2011
Each $a in the B.txt are single lines that look like
Code:
GA2_0019_FC:7:1:2190:1031#0/1
file.txt looks like over a million lines of DNA sequence data called a fasta format
>GA2_0019_FC:7:1:2190:1031#0/1
CCTAGCAGATAATTTGAGCACGTTGAGCAGTAAAAC
>GA2_0019_FC:7:1:10764:1034#0/1
CAGTCAAATAATAATATCAGCAAGCCACCAAAAAAA
>GA2_0019_FC:7:1:11778:1033#0/1
CGTCACGACCAACGAATTCCTTGAACTTCAAGAGCT

Each output line for each $a looks like
Code:
program.sh: line 4: GA2_0019_FC:7:1:2190:1031#0/1.txt: No such file or directory

When I remove the > $a.txt from line 4, then the program works and sends it to stdout. The problem is with the redirection but I can't seem to get it to redirect to a file

Last edited by Franklin52; 11-17-2011 at 03:32 AM.. Reason: Code tags
# 4  
Old 11-17-2011
are you telling you want to create different files for each search with $a.txt?

--ahamed
# 5  
Old 11-17-2011
Hello ahamed101. You are correct. I want to make a subset of $a.txt files from the larger file.txt. The problem is that the program only works to stdout but won't work when directed output to a file.
# 6  
Old 11-17-2011
You won't be able to create a file with the character "/" present in the file name

--ahamed
# 7  
Old 11-18-2011
Thanks. The problem was that the character / could not be tolerated in a variable used by grep. I changed it to _ and then the script worked. Thanks for all the input.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

How to use $variable in grep?

hi i have a file which contains some messages counters. below is the snippet on the file. 17-05-29::22:36:21|message|231 17-05-29::22:36:31|message|222 17-05-29::22:36:41|message|213 17-05-30::22:36:51|message|221 17-05-30::22:37:01|message|227 17-05-30::22:37:11|message|207... (5 Replies)
Discussion started by: scriptor
5 Replies

2. Shell Programming and Scripting

Help grep one variable over other

Hi, I am trying to grep one variable over the other variable Example: i=abc j=ab grep $j $i I am getting this error: The error is due to $i being variable and not file. I know I could do it by putting the value of abc in a file and then greping it. (1 Reply)
Discussion started by: pinnacle
1 Replies

3. Shell Programming and Scripting

grep variable

I've got a file that I'm trying to grep through that looks like this: alpha1 alpha2 alpha3 beta1 beta2 gamma5 gamma6 gamma7 gamma8 gamma9 and I want the output to only contain the line with the highest value for each, so the output I want is: alpha3 beta2 gamma9 I also need... (11 Replies)
Discussion started by: tiberione
11 Replies

4. Shell Programming and Scripting

grep in a variable

Hello, I usually search extensively and have to date found what I've needed. However, this one's got me stumped. I need to create a variable as follow. The issue however is that upon execution, it freezes. $var1 isn't always present in usage.log and this is fine but I'd like it to continue with... (6 Replies)
Discussion started by: shadyuk
6 Replies

5. Shell Programming and Scripting

grep using variable

I have a pattern like: column "5" is missing PS: the no is in double quotes. The number usally changes, so we use a loop to grep. grep 'column "$number" is missing' filename.txt But it is not working.... How to solve this? (2 Replies)
Discussion started by: karumudi7
2 Replies

6. Shell Programming and Scripting

Grep through a variable

I want to search a text in file but that file is pointing to variable. ex: file=there was nothing special grep "there was nothing" $file but its not working . Can u let me know that how we can use variable($file) in grep command. Please use code tags (6 Replies)
Discussion started by: allthanksquery
6 Replies

7. Shell Programming and Scripting

grep a variable

can i grep a variable say i have a variable var=`hostname` and I want to make an if statement like if grep "esp-ueh" $var;then...... how can i do this I dont want to store this variable in a file and the grep it because my script will be used at the same time on multiple stations and then that... (9 Replies)
Discussion started by: lassimanji
9 Replies

8. Shell Programming and Scripting

grep a variable

Hi all, I am trying to do a simple thing in my mind. However I am fairly new to bash. What I need to do is create a folder for each partition on each CD, and each partition has a unique name (with spaces in it, do not ask why, it is already done :confused: ) . All CD's will show up... (2 Replies)
Discussion started by: sgstuart
2 Replies

9. Shell Programming and Scripting

grep with variable

Hi, I can't get this script to work (returns 0, should return 3): $ cat A.lst | \ while read LINE do echo "$LINE" grep -c "$LINE" B.tmp done> > > > > Socket 0 $ but in contrast this one works fine (returns 3 as expected): $ LINE=Socket $ grep -c $LINE B.tmp 3 $ (5 Replies)
Discussion started by: ozvena
5 Replies

10. Shell Programming and Scripting

How to grep a variable?

Hi, I'd like to grep a variable that I saved in the program. Like grep '0\$variable1' file1 Does someone know what's wrong with this command? Thanks a lot! (2 Replies)
Discussion started by: whatisthis
2 Replies
Login or Register to Ask a Question