I have one file (approx 1000 words).
1.txt:
XYZ
ABC
DEF
GHI
...
And I have another file 2.txt which contains some data, now I want to grep these words in 1.txt in the file 2.txt.
I have used the below logic, but an error is given.
name=$(cat 1.txt |tr '\n' '|')
grep -E -w ${name} 2.txt
Grep can read patterns from a file using the -f option:
egrep -w -f 1.txt 2.txt
you need quotes and use egrep
egrep -w "$(cat 1.txt |tr '\n' '|')" 2.txt
but the answer with -f is better
Related
I need a script that will go into a directory, execute the command on each file, i tried some commands in a batch file, but i can't figure it out :)
john-wick-parse serialize file_route/filename_with_no_extention
Bash shell for loops
for f in file1 file2 file3 file5
do
echo "We are now processing... $f"
# do something on $f
done
And if you want, you can declare a variable containing the path to the files, one on each line, something like this:
FILES="file1
/path/to/file2
/etc/resolv.conf"
for f in $FILES
do
echo "We are now processing... $f"
# do something on $f
done
There's all sort of options for that, like selecting all files with a certain extension.
So if you were to use the first option:
Our files
-- my_folder
-- file1
-- file2
-- file3
-- etc
Our code
cd /path/to/my_folder
for f in file1 file2 file3 file5
do
echo "We are now processing... $f"
# do something on $f
done
#!/bin/bash
n=0
for f in *; do
[[ -f "$f" ]] && { echo "$f"; ((n++)); }
done
echo :Number of empty files: $n"
currently it checks the current directory for empty files, I would like it to search for empty files in any directory. Any ideas?
Recursively searches for empty files in current directory and below:
find . -empty -type f
Recursively lists empty files in specified directory and below and reports total
findempty
#!/bin/bash
echo :Number of empty files: `find $1 -empty -type f | tee /dev/tty | wc -l`
Example Usage
findempty /tmp
Example Output
/tmp/source/fb/b
/tmp/source/fb/a
/tmp/source/fb/c
/tmp/source/fa/b
/tmp/source/fa/a
/tmp/source/fa/c
/tmp/source/fc/b
/tmp/source/fc/a
/tmp/source/fc/c
/tmp/dest/source/fb/b
/tmp/dest/source/fa/b
/tmp/dest/source/fc/b
:Number of empty files: 12
I want to search for each line of a text file in another text file using Unix shell script. Consider these two text files:
file1:
HELEN
JOE
FRED
HARRY
TERRY
file2:
hello FRED
bert
JOE
hi there
HARRY
JOHN
PETE was here
hello STEVE
The required output should look like this:
HELEN not found in file2
JOE found in file2
FRED found in file2
HARRY found in file2
TERRY not found in file2
The script I have used is as follows:
while read name
do
found="no"
while read line
do
echo $line | grep $name >/dev/null
if [ $? -eq 0 ]
then
echo $name found in file2
found="yes"
break
fi
done < file2
if [ $found = "no" ]
then
echo $name not found in file2
fi
done < file1
This is the output I'm getting, which is not correct
not found in file2
found in file2
found in file2
found in file2
not found in file2
Is there something wrong with the script or the logic used here?
This does the job:
while read f; do
if grep -q -o $f file2
then
echo $f found in file2
else
echo $f not fount in file2
fi
done<file1
I have the following code for finding a string in a file then delete the line which contains that string.
echo `sed /$string/d file.txt` > file.txt
the problem is that if initially file.txt contains:
a
b
c
after deleting "a" (string=a) file.txt will become
b c
instead of
b
c
can any one help me?
This is because of the backticks. Do this instead:
sed -i /$string/d file.txt
Note, if you want to do this in-place, you need to use -i to sed as > will destroy the file before sed can read it.
You do not need the echo wrap, simply try:
sed -i '/a/d' file.txt
You need to quote the command's output:
echo -n "sed /$string/d file.txt" > file.txt
sed has an in-place editing option. It's more proper to use that in your senario.
e.g.
sed -i /$string/d file.txt
For the problem of your case, as the output of `` is not enclosed in double quotes, word splitting is done, by bash. And the newlines are removed.
To use echo in this case, do it like this:
echo "`sed /$string/d file.txt`" > file.txt
I'm trying to remove all lines of text that contain a double quote, and I have tried this:
sed -ne '/\"/!p' theinput > theproduct
It left the lines untouched. What do I do? Here is my script:
`touch tmp.txt
open tmp.txt
read -sn 1 -p "Paste in data and press any key to convert"
echo
touch tmp.txt
open tmp.txt
read -sn 1 -p "Paste in data and press any key to convert"
echo
sed -e 's/-/ /g' tmp.txt > tmp2.txt
grep -v '"' tmp2.txt > final.txt
open final.txt
echo Study Conversion Successful
The first sed command works. It replaces a hyphen with a bunch of spaces (don't ask why I need that). The grep command, which I added from a response, does not work. It leaves the lines with quotes untouched.
Its not necessary to escape the double quote:
sed -ne '/"/!p' theinput > theproduct
Very strange. It "works for me"
$ cat data.txt
dsklfljs
sdjflk"Sdgsd"
sdfj sldkfj "Sdfsd"
sdfj
sdf
sdjflks
$ sed -ne '/\"/!p' data.txt
dsklfljs
sdfj
sdf
sdjflks
Perhaps it is a version issue with sed?
However, you can also consider using grep -v for this.
$ grep -v '"' data.txt
dsklfljs
sdfj
sdf
sdjflks