LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 11-09-2009, 11:24 PM   #1
casperdaghost
Member
 
Registered: Aug 2009
Posts: 349

Rep: Reputation: 16
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
 
Old 11-09-2009, 11:44 PM   #2
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian + kde 4 / 5
Posts: 6,849

Rep: Reputation: 2024Reputation: 2024Reputation: 2024Reputation: 2024Reputation: 2024Reputation: 2024Reputation: 2024Reputation: 2024Reputation: 2024Reputation: 2024Reputation: 2024
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))"

Last edited by David the H.; 11-09-2009 at 11:53 PM.
 
Old 11-09-2009, 11:59 PM   #3
casperdaghost
Member
 
Registered: Aug 2009
Posts: 349

Original Poster
Rep: Reputation: 16
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
 
Old 11-10-2009, 12:08 AM   #4
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
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
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
awk one liner help niknak Linux - Newbie 1 05-07-2009 04:52 AM
IP Forward one-liner? JoeBleaux Linux - Networking 1 04-27-2009 10:24 AM
Help with Simple sed one liner SHARPY Linux - Newbie 10 04-12-2009 12:49 PM
one liner needed for search and replace chr15t0 Linux - General 2 09-24-2002 10:00 AM
how do I create or get a search engine Bheki Linux - General 1 02-04-2002 11:16 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 11:47 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration