I've been using this a lot:
find <directory to start search at> -name "<files to search in>" -type f | xargs grep "<string to search for>"
e.g.
find /usr/include -name "*.h" -type f | xargs grep "#define UINT"
now what if I wanted to output the results to a file?
I want to And search grep shell
but It's hard to grep variable
---------------------------------------------------------------
#!/bin/bash
if [ $# -eq 0 ]
then
echo "Ussage: phone searchfor [...searchfor]"
echo "(You didn't tell me what you want to search for )"
else
pass=0
find=""
for idx in $*
do
if [ -n "$idx" ]
then
if [ $pass -eq 0 ]
then
find=$(egrep "$idx" mydata)
pass=1
else
find=$("$find" | grep "$idx")
echo $find
fi
fi
done
if [ -z "$find" ]
then
echo "There is no such thing"
else
echo $find | awk -f display.awk
fi
fi
-----------------------------------------------------
there is one error : command not found
in find=$("$find" | grep "$idx")
how can I grep variable and store it into variable?
I copied the following from my linux console.
grep -lr "SMTP" *.ini
grep: *.ini: No such file or directory
I wanted to search recursively under current location in files with extenstion .ini
Actually there are files that contain "SMTP" under this directory. But I got the above error message.
What is wrong? I am using centos 6.
Thanks,
3rock
If there is one Windows XP feature that I greatly miss in Mint, it is the Search Companion.
I have been struggling with 'grep' in order to create something suitable but with limited success. Take the following problem:-
I wish to interrogate the folder home/dell/Documents/Domestic/Recipes, searching for all files containing the word "mushroom" or "mushrooms", ignoring case. (I can manage the latter. )
Each individual file search should terminate at the first instance of a match and move to the next file. (Recursive, yeh?) Only the file names need to be listed and the output should be paged to allow for easier reading of long lists.
Several different types of file may be involved, including .doc, .odt, .txt .pdf, .htm and .rtf. It would be nice to include all of them in one command. (Wild card behaviour in grep is not entirely predictable - at least not for me.) Running a separate grep command for each different file type would be tedious.
A significant difficulty is that, if grep fails with a syntax, or run-time error, it generally reports the fact but it also has a habit of producing no output, perhaps not returning to the command prompt, whilst sitting inviting the user to decide what to do next. What makes this particularly frustrating is that some file types might not be amenable to a grep search. Text in .txt files and, it would appear, .doc files appears to be searchable but I suspect that .odt files might be more problematic. The snag in such circumstances is trying to interpret grep's response. Does a null return mean that no match was found or that the file format cannot be successfully interrogated? Such failure might not be apparent if the associated file names are simply excluded from the output list.
Apart from grep, is there any other software that would do the job? Sadly LibreOffice Writer seems to be lacking in this area.
Hello all,
As title says.... whats the best way of getting linux to do another command after the first one?
e.g. Say I type "find . -iname "*.dll" this displays a list of all the dlls in a folder/subdirectory. ... then i wanted to copy these files, or du them, or other commands.
The three things i am aware of is:
Pipe |
awk
xargs
Which one is the most useful/standard/best practice to use?
Thanks
Hi all,
I'm trying to identify files that do not have matches for certain strings. FYI, these are files of DNA sequences and I'm trying to find those that are NOT sampled for any species by my group of interest (e.g., genes that are specific to that group of organisms).
I tried this code but it's actually yielding a list of files that DO match for my regexp.
Code:
for FILENAME in *.fas
do
grep -q -L ">PBAH" $FILENAME && grep -q -L ">SKOW" $FILENAME && grep -q -L ">CGRA" $FILENAME && echo $FILENAME
done
Basically I want to somehow go through and file files that do not contain ">PBAH" ">SKOW" or ">CGRA". Any assistance would be greatly appreciated!
Best,
Kevin
i need a bash script to switch to zero second on ac the waiting time for turning off the monitor (highlighten in red). same as for on battery (highlighten in purple)
Code:
<?xml version="1.0" encoding="UTF-8"?>
<channel name="xfce4-power-manager" version="1.0">
<property name="xfce4-power-manager" type="empty">
<property name="power-button-action" type="empty"/>
<property name="show-tray-icon" type="empty"/>
<property name="logind-handle-lid-switch" type="bool" value="true"/>
<property name="dpms-on-ac-sleep" type="uint" value="1"/>
<property name="dpms-on-ac-off" type="uint" value="2"/>
<property name="brightness-on-battery" type="uint" value="9"/>
<property name="dpms-on-battery-sleep" type="uint" value="1"/>
<property name="dpms-on-battery-off" type="uint" value="2"/>
<property name="lock-screen-suspend-hibernate" type="bool" value="false"/>
<property name="dpms-enabled" type="bool" value="true"/>
</property>
</channel>
in my RHEL 4 server , I want to know what ftp server is running but can't find it.
I tried "ps -ef |grep ftp" but no output , chkconfig --list |grep ftp also no output related to ftp , /etc/rc.d/init.d can't find ftp service , ftp localhost is not allow .
when use FileZilla to connect it , it is ok , the ftp should be running , I tried to use "ps -ef |grep ftp" , it pops the following output , would advise what ftp server is running in server ? thanks
Code:
edp 11027 11026 0 12:39 ? 00:00:00 tcsh -c /usr/libexec/openssh/sftp-server
edp 11037 11027 0 12:39 ? 00:00:00 /usr/libexec/openssh/sftp-server
user 11050 7747 0 12:48 pts/2 00:00:00 grep ftp
Hi all ,
I have a requirement where I have a file. Contents of the file are :
#comments
VAR="abg"
RES=123
#comments
IC6790ABG="https://www.abc.com"
IC5678-vg="https://www.bhy.com"
IC-gy_567:78="https://www.gyt.com"
#comments
The variable names can not have characters like - , : so
in this file I have to find words starting with IC and replace characters like - ,:
I want to change only the variable name , not the whole line.
I have used SED command
sed -i '/^IC/s/[^0-9 a-z A-Z _]*//g' file
when I am using this command , it is replacing the whole line
output becomes :
#comments
VAR="abg"
RES=123
#comments
IC6790ABGhttpswwwabccom
IC5678vghttpswwwbhycom
ICgy56778httpswwwgytcom
#comments
But I want the output like this :
#comments
VAR="abg"
RES=123
#comments
IC6790ABG="https://www.abc.com"
IC5678vg="https://www.bhy.com"
ICgy_56778="https://www.gyt.com"
#comments
How can I get the desired output , thanks for your help in advance .
hello,
i am trying to make a table from some files. i used this to record how much "RD_" field i have in my file.
Quote:
grep -o 'RD_' $f|grep -c 'RD_'
forexample i got 5 "RD_" fields now i want to print 5 number of fields from another file starting from 2nd field. i did it mannully like
Quote:
awk 'NR==1{print"{"$2","$3","$4","$5","$6","0.0000",""0.0000""}"","}' $file
i want to make it work together and a bit auto matic like
PHP Code:
awk 'NR==1{print"{"$2"to "$5"," apend zeros to make it total 7 fields"}"","}' $file
your coments would be apreciated
thanks alot
Hi
When i run this command manually on Centos 6.6 it works:
Code:
/usr/bin/find /backup/ -type d -mtime +1 -print0 | xargs -0 rm -rf
but as a cron job it doesn't as i can see a folder with files there from Mar 28:
Code:
55 5 * * * /usr/bin/find /backup/ -type d -mtime +1 -print0 | xargs -0 rm -rf
And here are the logs from cron that it is executing this at the correct time :
Code:
Mar 30 05:55:01 server CROND[9526]: (root) CMD (/usr/bin/find /backup/ -type d -mtime +1 -print0 | xargs -0 rm -rf)
Any ideas why?
Thanks