LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Software (https://www.linuxquestions.org/questions/linux-software-2/)
-   -   one liner to create a value and then search (https://www.linuxquestions.org/questions/linux-software-2/one-liner-to-create-a-value-and-then-search-768075/)

casperdaghost 11-09-2009 11:24 PM

one liner to create a value and then search
 
we add a static number to the pid of the process to name the log file - lets just say 27000.
i spend alot of time adding the two numbers with expr, then cut an paste the number into a grep statements and then search for the number in the log directory.

Is there a way i could put it in a one-liner.

% i=$(( expr 23416 + 27000 )) ; grep $i ~casper/home/users/logs

or is there a way that i could load awk into a variable.

% echo 23416 27000 | i=`awk '{ print $1 + $2 }'`| grep $i ~casper/home/users/log

David the H. 11-09-2009 11:44 PM

It took me a minute to figure out exactly what you want; you want to search a log file for "pid+fixed value", right?

Your first line is close to what you want. "$(( ))" is the bash arithmetic operator, and as long as you're adding integers you don't need to use expr or anything like it. I would also turn it around a little, like so.
Code:

value="23416"  # The pid you want to search
const="27000"  # The constant you want to add

grep "$((value+const))" ~casper/home/users/logs

Notes:
You don't need to use $ in front of the variable inside the arithmetic operator. The values will be expanded automatically.
Be sure to use double quotes around your grep expression. Single quotes will not allow the operator to work.

-----

Edit: waitaminutehere. Reading again more closely, are you perhaps saying you want to search a directory for filenames? For filenames you need to use find instead. Or perhaps grep through the output of ls.
Code:

find /logdrectory -name "*$((value+const))*"

ls /logdirectory | grep "$((value+const))"


casperdaghost 11-09-2009 11:59 PM

this is great

grep "$((value+const))" ~casper/home/users/logs

i am looking for a file in directory - pid + fixed value.

i do it maybe fifty times a day, i can't always add it in my head right, and getting sick of using the mouse to cut and paste the value

ghostdog74 11-10-2009 12:08 AM

you can use awk
Code:

awk 'BEGIN{
 cmd="pidof firefox"  #just example of getting pid
 cmd|getline value
 close(cmd)
 num=23416+value
}
$0 ~ num' ~casper/home/users/logs



All times are GMT -5. The time now is 10:21 AM.