Showing posts with label grep. Show all posts

Searching multiple strings in multiple files in a directory

I have a list of strings in a file separated by a new line.
for example:
input.txt
temp1
temp2
temp3
Now I have a directory with multiple dat files like:
>ls -1 *.dat
one.dat
two.dat
three.dat
And many more dat like like above with random names. Now I want to search for all the strings in input.txt in all the dat files present in  directory(let's say current working directory).This is what I came up with:
create a perl script given below and name it as anything you wish(I named here as temp.pl).place the file input.txt in the current working directory.
#!/usr/bin/perl -w

open (INP,"input.txt") or die $!;
while(<INP>)
{
my $cmd="find . -name \"*.dat\"|xargs grep -w -i $_";
my $output=`$cmd`;
 if($output!~/^\s*$/)
 {
 print $_."\n";
 print "------------------\n";
 print $output."\n";
 print "-------------------\n";
 }
}
exit;
Run this script as :
>./temp.pl
This solved my need.I hope it solves yours too :)

Delete empty lines in a file

Some times there are some empty lines which we feel are redundant in the file and want to remove them.Below is the command in unix to do that.
sed -i '/^$/d' your_file
But there is also another way to do this:
grep . your_file > dest_file
In perl also we can acheive this as below:
perl -pi -e 's/^$//g' your_file
the above mentioned perl and sed solutions will do an inplace replacement in the file
If in case the lines have some spaces then:
perl -pi -e 's/^\s*$//g' your_file

Search a string in multiple files recursively

Almost every unix programmer will need this at least once in a day.
For searching a string abc in all the files in the current direcory we use:
grep 'abc' *

If you want to serach in files which are under any sub directories also including the files in the current directory then we have to combine both find and grep:

find . -type f|xargs grep 'abc'

Another possible way is using exec of find command:
find . -type f -exec grep 'abc' {} \;

Perl's equivalent of "grep -f"

Lets say there are two files:

> cat file1
693661
11002
10995
48981
79600
> cat file2
10993   item    0
11002   item    6
10995   item    7
79600   item    7
439481  item    5
272557  item    7
224325  item    7
84156   item    6
572546  item    7
693661  item    7

using
grep -f file1 file2
we can filter the data in file2 but comparing the matching rows only with teh first column.

I have written a perl equivalent to this but yet not in a complete way.
the below command will simply compare the first field of the file 1 and present the output if it matches with the first field of file2.

> perl -F -lane 'BEGIN{$c=1}if($c==1){$x{$_}++};if(eof && $c==1){$c++;next};if($c==2){if($x{$F[0]}){print }}' file1 file2
11002   item    6
10995   item    7
79600   item    7
693661  item    7

Print limited characters per a line

Printing first 80 characters in a line.Below are the different ways to do it.
Cut
cut -c1-80 your_file
Awk
awk '{print substr($0,0,80)}' your_file
Sed
sed -e 's/^\(.\{80\}\).*/\1/' your_file
Perl
perl -lne 'print substr($_,0,80)' your_file
perl -lpe 's/.{80}\K.*//s' your_file

Grep
grep -o "^.\{80\}" your_file