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
# 1  
Old 12-19-2008
problem with grep in combination with xargs

Dear all,
I have tried the following 2 lines

xargs -t -i -exec grep -i -w {} file_1 >>test < file_2

cat -s file_2| xargs -t -i -exec grep -i -w {} file_1 >> test

They were meant to search for the contents of file_2 in file_1 and write the respective lines of file_1 into file "test" . But instead it writes a standard error output
grep -i -w "string1" File_1
grep -i -w "string2" File_1
grep -i -w "string3" File_1
and creates an empty file "test"
Any ideas why this doesn't work?
I f I copy each of the above lines from the standard error output
they work and produce the expected result
Your help is very much appreciated

Thanks
Bruno
# 2  
Old 12-19-2008
I didn't check the lines but basically you can have this easier with
Code:
grep -vf file_2 file_1 > test


Last edited by zaxxon; 01-08-2009 at 12:31 PM.. Reason: Just saw this thread is still alive, corrected the missing -f
# 3  
Old 12-19-2008
problem with grep in combination with xargs

Hi Zaxxon,
thanks for the quick reply. Unfortunately that doesn' work. I think the problem is that the info I am looking for looks like this

file_2
TAG_1
TAG_2
TAG_4
TAG_6
...

file_1
TAG_1 Boston
TAG_2 New York
TAG_3 Munich
TAG_4 London
TAG_5 Paris
TAG_6 Madrid
TAG_7 Lisbon
...
Your command line puts the whole content of file_1 into test and not only
TAG_1 Boston
TAG_2 New York
TAG_4 London
TAG_6 Madrid
# 4  
Old 12-19-2008
Ah, you want grep to hunt for multiple things. Giving grep multiple parameters won't make it hunt for multiple strings, it will take every parameter after the first one as a filename to search in. You need to use egrep, and assemble the tags you want to look for into a single string like
Code:
egrep 'TAG_1|TAG_2|TAG_3|TAG_4' file > output

# 5  
Old 12-22-2008
problem with grep in combination with xargs

Hi Corona688,
thanks for the reply. Unfortunately that doesn' work either. The result is the same as the one I get when I use Zaxxon suggestion.
file_2
TAG_1
TAG_2
TAG_4
TAG_6
...

file_1
TAG_1 Boston
TAG_2 New York
TAG_3 Munich
TAG_4 London
TAG_5 Paris
TAG_6 Madrid
TAG_7 Lisbon
...
Your command line puts the whole content of file_1 into test and not only
TAG_1 Boston
TAG_2 New York
TAG_4 London
TAG_6 Madrid

Thanks for trying

Bruno
# 6  
Old 12-22-2008
wouldn't
Code:
fgrep -f file2 file1 > test

Do what you want?
# 7  
Old 12-22-2008
Dear reborg,
that doesn't work it just produces an empty file "test".

Thanks for trying

Bruno
 
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