Find EXACT word in files, just the word: no prefix, no suffix, no 'similar', just the word
I have a file that has the words I want to find in other files (but lets say I just want to find my words in a single file). Those words are IDs, so if my word is ZZZ4, outputs like aaZZZ4, ZZZ4bb, aaZZZ4bb, ZZ4, ZZZ, ZyZ4, ZZZ4.8 (or anything like that) WON'T BE USEFUL.
I need the whole word (and the line where it is), but the exact word, nothing more and nothing less. The only thing that could be useful would be (ZZZ).
Of course, I need THE ENTIRE output line, so using grep -o, would not be useful either.
I've tried so many things. Nothing had worked fine.
-----
ok, lets say that my file that has the IDs is called 'Id', i've tried:
If my first Id was 'Sun', the previous line gives me outputs like 'Sunny' (it also gives me 'Sun' as an output, that's ok). NOT GOOD!
I've tried several grep options. I've tried fgrep. Not good. I've tried agrep, and it's not good because it is approximate, so if I were looking for 'Sunny', agrep could throw 'Suny' as an output.
The things I've not tried much are things that involve regular expressions (too scared). Anyway, if I was trying to use regular expressions, how could I do that if the words I am searching are in a file called Id?
I am totally brand new to this so don't get mad if I'm wrong... But when I'm looking through the maillog for my name in a line, I do this:
# more maillog | grep "freddy"
which is what I think you're doing now. Have you tried putting the white space in the serach like " freddy "? I know that if you want to match spaces on either side you can use escaped s "\s" but have no example.
This User Gave Thanks to Freddythunder For This Post:
Can you confirm the content of your control file please. i suspect that you have something like the following (using ^ to mark start of line and $ to mark the end):-
... so what you are matching is the string listed on each line (e.g. Sun) so this will match anywhere it finds Sun, e.g. Sunny, Sunday, WhitSun etc. What you might need is a tweak in your control file. What are the delimiters in the file you are searching? If they are spaces, then you need to generate you control file with spaces at the beginning and end of each line, so (using same markers):-
That way, it will match the whole string including the spaces before and after. Of course you then have the problem that some records you want may only have a space before or after, so you might have to get a little more inventive.
Does this help?
If you already have a large control file to search with, then using vi you can enter the following commands to insert a space at the beggining and end of each line:-
I hope that this (and the explanation) helps, but feel free to write back with more examples where it doesn't quite work.
What happens when you use
It should work:
So perhaps there is an extra character between Sun and ny in the file you are searching in? Have you checked with od -c?
Otherwise what is your OS and version?
This User Gave Thanks to Scrutinizer For This Post:
I was doing other tests right know, following several of your advises. And turned out that grep is doing it fine. I took some precautions like deleting hidden files (that were making some noise). The last command line that I used and work fine was:
I don't know if the -f was necessary but it works fine with it.
My control file does not have the ^ and $. Maybe I should add those to each line.
You don't need them in the file itself.
^ and $ have special meanings to grep -- ^ means "Beginning of the line", and "$" means "end of the line."
So "^word$" would search for a line that starts, and ends, with word. It wouldn't match "wword" or "wordd".
So this doesn't sound relevant to what you want, since you're not looking for a regex at the beginning or end of a line. You're looking for an exact string in a column.
awk is very good at dealing with columns... How about:
Last edited by Corona688; 03-30-2012 at 05:34 PM..
Hi All,
I am looking for a perl/awk/sed command to auto-increment the numbers line in file, P1.tcl:
run_build_model sparc_ifu_dec
run_drc
set_faults -model path_delay -atpg_effectiveness -fault_coverage
add_delay_paths P1
set_atpg -abort_limit 1000
run_atpg -ndetects 1000
I would like... (6 Replies)
I have a file: file.txt, which contains the following data in it.
This is a file, my name is Karl, what is this process, karl is karl junior, file is a test file, file's name is file.txt
My name is not Karl, my name is Karl Joey
What is your name?
Do you know your name and... (3 Replies)
Hi All,
I have a XML file which is looks like as below. <<please see the attachment >>
<?xml version="1.0" encoding="UTF-8"?>
<esites>
<esite>
<name>XXX.com</name>
<storeId>10001</storeId>
<module>
... (4 Replies)
hi all, I'm new there, I'm just playing with perl and lwp and I just successfully created a script for log in to a web site with post. I have a response but I would like to have something like this:
I have in my response lines like:
<div class="sender">mimi020</div>
<some html code.....>... (3 Replies)
I need to compare two files word by word using unix shell scripting.
Could someone help me? I need the code which will compare the 1st word from file1 with 1st word from file2, 2nd word from file1 with 2nd word from file2 etc..., for all the lines.
Example:
File1:
aaa bbb ccc ... (7 Replies)
Hi Everyone,
I am looking for a simple way for replacing all the files under a directory that use the server "xsgd1234dap" with "xsdr3423pap".
For Example:
In the Directory,
$pwd
/home/nick
$ grep -l "xsgd1234dap" *.sh | wc -l
119
I have "119" files that are still using... (5 Replies)
Hi, i am new to unix shell scripting and i need a script which would search for a particular word in all the files present in a directory. The output should have the word and file path name. For example: "word" "path name".
Thanks for the reply in adv,:) (3 Replies)
Hi ALL,
I want to search one string “20 “ i.e 20 with space.
But my file where I am searching this “20 “ contain some data like
120 before image file truncated
220 Reports section succeeded
20 Transaction database .prd stopped
220 Reports section completed.
When I search for the... (5 Replies)
Hi all,
I have a file with lines written somewhat like this.
aaaa
ccc
aa
linux
browse = no
xssxw
cdcedc
dcsdcd
csdw
police
dwed
dwd
browse = no
cdecec (2 Replies)
Greetings.
I am struggling with a shell script to make my life simpler, with a number of practical ways in which it could be used. I want to take a standard text file, and pull the 'n'th word from each line such as the first word from a text file.
I'm struggling to see how each line can be... (5 Replies)