problem with grep in combination with xargs


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers problem with grep in combination with xargs
# 8  
Old 12-22-2008
First try to avoid calling a file "test". Sooner or later it will give you trouble because it is the name of a unix command.

Hope this script helps:

Code:
(
#!/bin/ksh
cat file_2|while read TAG
do
        grep \^"${TAG} " file_1
done
) > test_file

cat test_file
TAG_1 Boston
TAG_2 New York
TAG_4 London
TAG_6 Madrid


Last edited by methyl; 12-22-2008 at 02:09 PM.. Reason: Omitted shell
# 9  
Old 12-22-2008
Note the use of a space after ${TAG} in the grep to avoid mismatches. You need to distinguish TAG_1 from TAG_12 .

Last edited by methyl; 12-22-2008 at 02:10 PM.. Reason: Typo
# 10  
Old 12-23-2008
Thanks Methyl,
as you know it works on my example files. What do I have to do if my tags are different e.g.
NY5876_1 ... NY5876_9999
If tried myself but failed.

Thank you very much for your help.
# 11  
Old 12-23-2008
The script does not need changing.
In my little script, "TAG" is the just name of a variable.
# 12  
Old 12-23-2008
But it doesn't seem to work for me with any other TAG, I end up with an empty test_file, and if I don't leave a space after the expression it kind of works but it puts the whole file_1 into test
Here is your script (amended hopefully correctly for my 2nd example) that I used
(
#!/bin/ksh
cat list|while read NY5876
do
grep \^"${NY5876} " NY5876_cds.csv
done
) > test_file
# 13  
Old 12-23-2008
Please post sample data from the file called "list" and the file called "NY5876_cds.csv".
# 14  
Old 12-23-2008
Hi Methyl,
here are some sample data
Thanks again for going to so much trouble.
Bruno
list looks like this
NY5876_104
NY5876_5
NY5876_899
NY5876_9999
NY5876_1899
NY5876_10

file NY5876_cds.csv looks like this
>NY5876_1 chromosomal replication initiation protein
>NY5876_2 DNA polymerase III subunit beta
>NY5876_3 recombination protein F
>NY5876_4 DNA gyrase subunit B
>NY5876_5 type I restriction-modification system, M subunit, putative
>NY5876_6 type I restriction-modification system S subunit
>NY5876_7 hypothetical protein PSPTO_0007
>NY5876_8 HsdR family type I site-specific deoxyribonuclease
>NY5876_9 ISPsy3, transposase
>NY5876_10 hypothetical protein PSPTO_0011
...
...
...
>NY5876_9999 hypothetical protein
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Problem with xargs

I entered the following <zzz.list xargs showtell |more which does a echo "<<<<<<<<<<<<<<<<<<<<<<<<< $1 >>>>>>>>>>>>>>>>>>" head -20 $1 The file zzz.list contains 525 lines representing user scripts (1 per line), but only the first, 181st, and 399th lines were processed. What am I missing? TIA (2 Replies)
Discussion started by: wbport
2 Replies

2. Shell Programming and Scripting

Looping grep and xargs

In a directory I have two lists, all files which could contain certain fields (they all share copied code) and a list of fields. My two scripts are <appl.list xargs grep $1 where appl.list contains files (source programs) with the fields. This script is named YY <fields.list xargs YY ... (4 Replies)
Discussion started by: wbport
4 Replies

3. Shell Programming and Scripting

Problem using xargs with rm

I am trying to call xargs, however rm is complaining find . -maxdepth 1 -name "*tests*" -print0 | xargs -0 rm rm: missing operand Try `rm --help' for more information. (3 Replies)
Discussion started by: kristinu
3 Replies

4. Shell Programming and Scripting

Find common terms in two text file, xargs, grep

Hello, I'm interested in finding all occurrences of the terms in file1 in file2, which are both csv files. I can do this with a loop but I'm interested in knowing if I can also do it with the help of xargs and grep. What I have tried: cat file1 | xargs grep file2 The problem is that... (15 Replies)
Discussion started by: eon
15 Replies

5. Shell Programming and Scripting

Using a combination of sort/cut/grep/awk/join/paste/sed

I have a file and need to only select users that have a shell of “/bin/bash” in the line using awk or sed please help (4 Replies)
Discussion started by: boyboy1212
4 Replies

6. Shell Programming and Scripting

xargs problem

Hi From the xargs man page (Solaris): ls $1 | xargs -I {} -t mv $1/{} $2/{} This would move all the files in directory $1 to directory $2 Problem 1: In a shell script if I want to move files in d1 to d2 dir1=~/d1 dir2=~/d2 ls $dir1 | xargs -I {} -t mv $dir1/{} $dir2/{} does not... (3 Replies)
Discussion started by: encrypted
3 Replies

7. Shell Programming and Scripting

Sed Awk Cut Grep Combination Help ?

I have been reading for a few hours trying to educate myself enough to accomplish this task, so please know I have performed some research. Unfortunately, I am not a *NIX scripting expert, or a coder. I come from a network background instead. SO, here is my desired outcome. I have some Cisco... (5 Replies)
Discussion started by: abbzer0
5 Replies

8. UNIX for Dummies Questions & Answers

Grep and find combination

Hello All, I'm trying the following:find . -name "*" -exec grep -ln "IsAlpha" {} \; It gives me file names only (having string "IsAlpha"), I want to get line numbers also, something like this: test 1: Line 52 test 1: Line 95 etc Is it possible to obtain using grep & find only. (5 Replies)
Discussion started by: nervous
5 Replies

9. UNIX for Dummies Questions & Answers

Combination of find -xargs & wc -l

Dear all, I have to calculate sum of record count of files of the specified directory. First I tried the following way which prints one or more outputs. How can I sum of this output? find /home/work/tmp/1/O/ -type f -print0 | xargs -0 wc -l | grep total 1666288 total 1073908 total ... (4 Replies)
Discussion started by: mr_bold
4 Replies

10. Shell Programming and Scripting

grep and xargs

guys... I wanna use xargs in such a way that i can use it in grepping the fileds.. something like this: grep -p <xargs values> * lemme know how to do this.. (5 Replies)
Discussion started by: freakygs
5 Replies
Login or Register to Ask a Question