Help with grepping data from a text file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Help with grepping data from a text file
# 1  
Old 09-04-2012
Help with grepping data from a text file

Hello,
I have a text file which contains a list of strings which I want to grep from another file where these strings occur and print out only these lines.
I had earlier used the grep command
Quote:
grep -f File1 File2
where File1 was the file containing the strings to be grepped (Source File) and File2 the Target File from which the strings had to extracted.
And this had worked, but this time the above syntax does not work.
I am in a windows environment and tried both GREP for DOS as well as GREP32 but in each case I do not get the desired results.
Any help would be appreciated since the amount of strings to be grepped is quite large.
# 2  
Old 09-04-2012
Quote:
Originally Posted by gimley
Hello,
I have a text file which contains a list of strings which I want to grep from another file where these strings occur and print out only these lines.
I had earlier used the grep command

where File1 was the file containing the strings to be grepped (Source File) and File2 the Target File from which the strings had to extracted.
And this had worked, but this time the above syntax does not work.
I am in a windows environment and tried both GREP for DOS as well as GREP32 but in each case I do not get the desired results.
Any help would be appreciated since the amount of strings to be grepped is quite large.
You say that you're looking for strings, but with the options you've given you're not looking for fixed strings; you're looking for matches to basic regular expressions contained in File1. If you want fixed strings, you need to add the -F option as ingrep -F -f File1 File2. If this doesn't solve your problem, we need to know what the contents of File1 and File2 look like, and any messages you're getting when you run the command. If you're running in a Windows environment, where are you getting your grep and grep32 utilities?
# 3  
Old 09-05-2012
Many thanks. Am out at the moment but will test the syntax you have provided as soon as I get back. In case of a glitch I will put a small sample of both files as attachments.

---------- Post updated 09-05-12 at 12:06 AM ---------- Previous update was 09-04-12 at 09:57 PM ----------

Hello,
I check out the suggestion you gave me and typed the command as under
Quote:
grep -F -f dupes pers2.lng
dupes is the file containg the words to be grepped and pers2.lng is the target file
I got the following output
Quote:
Unknown option: F
usage: grep.exe [-CVbchilnsvwx] [-<num>] [-AB <num>] [-f file] [-e] expr [files]
The -F option is treated as unknown. I tried also with standard GREP under windows and got the same answer.
I am giving below a small sample
LIST of words to grep from file called DUPES
Quote:
vijendar
yudhvir
Small file containing the words to be grepped the original filename is Pers2.lng
Quote:
vijendar=vijendara=vijender=vijendera=vijendr=vijendra
vijendar=vijendara=vijender=vijendera=vijinder=vijindera
yuddhaveer=yuddhaveera=yuddhveer=yuddhavir=yuddhavira=yuddhveera=yuddhvir=yuddhvira=yudhveer=yudhvee ra=yudhvir=yudhvira
yudhbir=yudhbira=yudhvir=yudhvira
Many thanks for your kind help
# 4  
Old 09-05-2012
With the example you're showing for the contents of DUPES (or dupes) with no "special" characters included, using grep, grep -F, fgrep, grep -E, or egrep shouldn't matter.

What is the exact command line you are using? When you do an ls in that directory, what are the names of the files (exact names with uppercase and lowercase letters as displayed by ls)? Are you using the same filenames shown by ls when you run the command?

What output do you get on stdout when you run that command?

What output do you get on stderr when you run that command?

What is the exit status of the command that you run?

If the above doesn't make sense to you, run the following commands and show us the output:
Code:
grep -f dupes pers2.lng > grep.stdout 2> grep.stderr
echo exit code is $?
ls -l dupes pers2.lng
echo grep.stdout contains:
cat grep.stdout
echo grep.stderr contains:
cat grep.stderr

# 5  
Old 09-05-2012
Hello,
Sorry for the delay in answering. My broadband was down the whole day because of heavy rains.
I found out the stupid thing I was doing. The command works fine. But if I want to locate an exact match from the source file, I should have added a regex or looked for word preceded and followed by an equal to sign. I tried the former and it worked.
Many thanks for your concern and your suggestions
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Grepping the data between 2 date ranges

Hi There, Good Day !! I have txt file containing data in the below format. There are many lines, here i have mentioned for example. cat remo.txt 2/3/2017 file1 3/4/2016 file2 6/6/2015 file5 1/1/2018 file3 4/3/2014 file4 - - - I need to grep the file names for given date rage... (11 Replies)
Discussion started by: kumar85shiv
11 Replies

2. Shell Programming and Scripting

How to handle grepping variable data containing wildcards?

I have a lot of files with keywords and unique names. I'm using a shell script to refer to a simple pattern file with comma separated values in order to match on certain keywords. The problem is that I don't understand how to handle the wildcard values when I want to skip over the unique names. ... (5 Replies)
Discussion started by: abercrom
5 Replies

3. Shell Programming and Scripting

Compare 2 text file with 1 column in each file and write mismatch data to 3rd file

Hi, I need to compare 2 text files with around 60000 rows and 1 column. I need to compare these and write the mismatch data to 3rd file. File1 - file2 = file3 wc -l file1.txt 58112 wc -l file2.txt 55260 head -5 file1.txt 101214200123 101214700300 101250030067 101214100500... (10 Replies)
Discussion started by: Divya Nochiyil
10 Replies

4. Shell Programming and Scripting

Grepping data using awk

Hello, I have a data in file 1 2000000024776 2000000026979 2000000033355 2000000036309 2000000041291 2000000042679 2000000067221 and in file 2 its like this 2000000024776 16 2000000026979 16 2000000033355 16 (2 Replies)
Discussion started by: mirwasim
2 Replies

5. Shell Programming and Scripting

Grepping text from one file in another file

Hello, I have a file with a large number of words each listed in sequential order one word per line. I want to search these words in another file which has the structure Both the files are large, but the words in the sourcefile are all available in the target file. I tried to grep... (2 Replies)
Discussion started by: gimley
2 Replies

6. UNIX for Dummies Questions & Answers

Unexpected Behaviour from grepping Text File

Hi! I recently downloaded a wordlist file called 2of12.txt, which is a wordlist of common words, part of the 12dicts package. I've been getting unexpected results from grepping it, such as getting no matches when clearly there ought to be, or returns that are simply wrong. Par exemple: egrep... (4 Replies)
Discussion started by: sudon't
4 Replies

7. Shell Programming and Scripting

Filter a .kml file (xml) with data set from text file

I have a .kml file. So I want filter the .kml to get only the tags that have this numeric codes that they are in a text file 11951 11952 74014 11964 11965 11969 11970 11971 11972 60149 74018 74023 86378 11976 11980 11983 11984 11987 (5 Replies)
Discussion started by: pcoj33
5 Replies

8. Shell Programming and Scripting

Find and replace data in text file with data in same file

OK I will do my best to explain what I need help with. I am trying to format an ldif file so I can import it into Oracle oid. I need the file to look like this example. Keep in mind there are 3000 of these in the file. changetype: modify replace: userpassword dn:... (0 Replies)
Discussion started by: timothyha22
0 Replies

9. Shell Programming and Scripting

Grepping text by providing line numbers.

Dear Friends, I have a flat file from which I want to grep line no. 7,10, 19 to 35, 37. How can it be done? Thank you in advance Anushree (6 Replies)
Discussion started by: anushree.a
6 Replies

10. Shell Programming and Scripting

Extracting data from text file based on configuration set in config file

Hi , a:) i have configuration file with pattren <Range start no>,<Range end no>,<type of records to be extracted from the data file>,<name of the file to store output> eg: myfile.confg 9899000000,9899999999,DATA,b.dat 9899000000,9899999999,SMS,a.dat b:) Stucture of my data file is... (3 Replies)
Discussion started by: suparnbector
3 Replies
Login or Register to Ask a Question