LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 08-17-2015, 02:45 AM   #1
larotule
LQ Newbie
 
Registered: Aug 2015
Posts: 4

Rep: Reputation: Disabled
Scripting with Grep : how to include a variable inside a search ?


hi i made a script who works well like that

i split a log file in line

and grep the results like that :

for ligne in $(cat scangalaxies.txt); do echo -e "$ligne"; done | grep -B8 "recycle" | grep -A4 '"position":"5:\(.\)\(.\)\(.\):\(.\)"'

i want to replace the number 5 by a var (i) in my search is it possible with grep ?

i tried :

grep -A4 '"position":"\($i\):"1:\(.\)\(.\)\(.\):\(.\)"'

but it doesnt work

any help ? or maybe with sed instead ?

thx

Last edited by larotule; 08-17-2015 at 02:46 AM.
 
Old 08-17-2015, 04:16 AM   #2
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 21,126

Rep: Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120
The variable won't be resolved inside single quotes. Either get rid of the single quotes completely, or terminate them before the variable and resume them after. Very messy.
 
1 members found this post helpful.
Old 08-17-2015, 05:04 AM   #3
larotule
LQ Newbie
 
Registered: Aug 2015
Posts: 4

Original Poster
Rep: Reputation: Disabled
thx

yes its very messy lol

thx you tip works if i didnt add a second part after the variable

grep -A4 '"position":"'$i

works great, exactly what i m looking for ! and its less messy
 
Old 08-17-2015, 07:03 AM   #4
larotule
LQ Newbie
 
Registered: Aug 2015
Posts: 4

Original Poster
Rep: Reputation: Disabled
Almost working ! ... but i have a strange problem with read command !?

if i do that in shell it works
read player
for ligne in $(cat scangalaxies.txt); do echo -e "$ligne"; done | grep -e '{"username' -e 'position' -e 'class' -e 'longinactive' -e '"ally_name"' -e 'has_moon' | grep $player -A10

but NOT when i put read in my script

#!/bin/sh
echo "1 - Positions : Search a player :"
echo "2 - Recycle : Look for debris field size"
echo "3 : Longinactive : Search longinactive players"
read choice
### Player Mapping
if [ "$choice" -eq 1 ]
then
read -p "Enter player name (case sensitive) :" player
for ligne in $(cat scangalaxies.txt); do echo -e "$ligne"; done | grep -e '{"username' -e 'position' -e 'class' -e 'longinactive' -e '"ally_name"' -e 'has_moon' | grep $player -A10
else
...

///

script show everyone... seems read doesnt record my input inside the script

any idea ?

thx
 
Old 08-17-2015, 07:11 AM   #5
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,842

Rep: Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308
for ligne in $(cat scangalaxies.txt); do echo -e "$ligne"; done
has no any meaning
using:
grep -e 'whatever' scangalaxies.txt
will do exactly the same thing
I do not really understand what do you want to achieve would be nice to show us an example (before and after)
 
Old 08-17-2015, 07:16 AM   #6
larotule
LQ Newbie
 
Registered: Aug 2015
Posts: 4

Original Poster
Rep: Reputation: Disabled
i found myself why it's nt working !

forgot to declare IFS=, !!!

pan64, my file is a big mess (just a sniff log) so i split in line after each "," after that i could grep some text

Everything works well now !

thx to both of you
 
  


Reply



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 On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Can grep search for multiple patterns from a variable pradeepdee6 Linux - Newbie 10 11-08-2012 04:52 AM
[SOLVED] grep --include glob with shell variable hashbang#! Programming 1 11-10-2010 04:02 PM
I need grep options to search inside a .tar.gz file. ZAMO Linux - General 2 06-24-2008 11:55 PM
How to use grep to search for a specific variable.. memo007 Linux - Software 4 02-24-2007 05:52 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 09:24 PM.

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