Sponsored Content
Top Forums Shell Programming and Scripting How to grep a number in a file to find them in another file Post 302150580 by ahjiefreak on Tuesday 11th of December 2007 09:27:16 PM
Old 12-11-2007
Hi,

I miss ed out the output in earlier post.

Currently, my input file of A.txt

aaa 1
bbb 2
ccc 3
ddd 100

B.txt

2 3 4
3 4 5

My code so far is:-

#!/bin/bash

cat a.txt| while read LINE
do

#to get the second field of a.txt
char=`echo "${LINE}"| awk '{print $2}'`

echo $char

#to grep the line position of b.txt which match the element in a.txt
grep -n $char b.txt | tr ":" " "| awk '{print $1}'

done

The output is:-
1

2
1

3
1
2

100

The problem here is i) grep does not want to recoqnize only the first field of b.txt (which you can see for 3 in a.txt there is 1, 2 returned because there is "3" in b.txt for two entries.

ii)And there is problem if I applied grep -m 1 $char b.txt|awk '{print $1}'
for printing the matching element for first field of b.txt. The output would be

1

2
2

3
2

100


Ultimately, what I would like to have the desired otuput is:-

1 Not Found
2 Position 1 3+4
3 Position 2 4+5
100 Not Found


Thanks.
-Jason
 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to grep a number in a file name

Hi, I have multiple files where it starts with test1.c, test2.c,test3.c and so on. I would like to get each file separately to perform abstraction from these files. I tried something like:- for t in ./* filenumber=${t:4} # to cut the "test" in order to get the number cat... (3 Replies)
Discussion started by: ahjiefreak
3 Replies

2. UNIX for Dummies Questions & Answers

find file grep it and print file name

i am trying to search a few hundred release note text files for a certain word. however when i use the below command i can find a file that contains it but i dont know the file name. how can i change this command to output the name of the file that grep was successful in? find builds -name... (4 Replies)
Discussion started by: borderblaster
4 Replies

3. UNIX for Dummies Questions & Answers

Find number of columns in a file

Hi all, may seem a very stupid question.but me stuck up in it for long.... How to find the number of columns in a ASCII file. EX:-Demo.lst songs 1 34 45 67 Number of columns should be 5. Regards, Anindya ;) (13 Replies)
Discussion started by: rahul26
13 Replies

4. Shell Programming and Scripting

File find | xargs grep for pattern file

Folks I've been struggling this with for far too liong now and need your help! I've been happily using grep for a search of a directory, to list the files which contain a string: find . -type f -mtime -5 -print | xargs grep -l 'invoiceID=\"12345\"' Now the list of 'invoiceID' I am... (4 Replies)
Discussion started by: daveaasmith
4 Replies

5. Shell Programming and Scripting

How to use grep & find command to find references to a particular file

Hi all , I'm new to unix I have a checked project , there exists a file called xxx.config . now my task is to find all the files in the checked out project which references to this xxx.config file. how do i use grep or find command . (2 Replies)
Discussion started by: Gangam
2 Replies

6. Shell Programming and Scripting

AWK-grep from line number to the end of file

Does anyone know how to use awk to act like grep from a particular line number to the end of file? I am using Solaris 10 and I don't have any GNU products installed. Say I want to print all occurrences of red starting at line 3 to the end of file. EXAMPLE FILE: red green red red... (1 Reply)
Discussion started by: thibodc
1 Replies

7. Programming

Find the number in the file

In my system , there are text files will be generated monthly , the file name begins with xxx , then year , month ( for example xxxxx201310.txt means Oct 2013 ) I have below command to count how many abc in the month , but it only count the number in this month . NUMBER=$(cat xxxxx201310.txt... (2 Replies)
Discussion started by: ust
2 Replies

8. UNIX for Dummies Questions & Answers

Grep SQL output file for greater than number.

Hi, This is my first post. I have a korn shell script which outputs a select statment to a file. There is only one column and one row which contains a record count of the select statement. The select statement looks something like this: SELECT COUNT(some_field) AS "count_value" ... (2 Replies)
Discussion started by: MurdocUK
2 Replies

9. Shell Programming and Scripting

Grep pattern after specific line number in a file

Hi guys, I am running a while loop in a script ro read a file line by line. Now I want to run a grep only on the lines below the line I am that is being read by the while loop. Eg: If my while loop is on line 4 of the file, the grep only runs below line 4 and does not include line 1,2... (3 Replies)
Discussion started by: Junaid Subhani
3 Replies

10. UNIX for Beginners Questions & Answers

Grep: Retrieve two strings from one file to find them anyone on line in another file

I am having trouble matching *two* strings from one file anywhere in a line of a second file, and could use some help getting this figured out. My preference would be to use grep for this because I would like to take advantage of its -A option. The latter is due to the fact that I would like both... (2 Replies)
Discussion started by: jvoot
2 Replies
ZGREP(1)						      General Commands Manual							  ZGREP(1)

NAME
zgrep - search possibly compressed files for a regular expression SYNOPSIS
zgrep [ grep_options ] [ -e ] pattern filename... DESCRIPTION
Zgrep invokes grep on compressed or gzipped files. These grep options will cause zgrep to terminate with an error code: (-[drRzZ]|--di*|--exc*|--inc*|--rec*|--nu*). All other options specified are passed directly to grep. If no file is specified, then the standard input is decompressed if necessary and fed to grep. Otherwise the given files are uncompressed if necessary and fed to grep. If the GREP environment variable is set, zgrep uses it as the grep program to be invoked. EXIT CODE
2 - An option that is not supported was specified. AUTHOR
Charles Levert (charles@comm.polymtl.ca) SEE ALSO
grep(1), gzexe(1), gzip(1), zdiff(1), zforce(1), zmore(1), znew(1) ZGREP(1)
All times are GMT -4. The time now is 08:54 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy