LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 09-03-2020, 10:19 PM   #1
wackysiy1024
LQ Newbie
 
Registered: Sep 2020
Posts: 1

Rep: Reputation: Disabled
Grep not working for string with both spaces and numbers


Hi

I am trying to print all the following entries from my input file.

Quote:
pptstatistics#rtpRequestCount#path 0#rate
pptstatistics#rtpRequestCount#path 1#rate
pptstatistics#rtpRequestCount#path 3#rate
pptstatistics#rtpRequestCount#path 2#rate
Since my string had a space,I wrote the grep in following format
Quote:
grep "pptstatistics#rtpRequestCount#path *#rate " inputfile > output file
I wanted to get all the paths printed(in this case path 0-path 3)It didnt work. I also tried for another example string where i am trying to print the required strings with all paths and different cores. (essentially I am trying to print all with all different datapath and all values of icore

Expected output:
Quote:
datapath icore all parameter ocr-frames#datapath:0#icore:2
datapath icore all parameter ocr-frames#datapath:3#icore:5
datapath icore all parameter ocr-frames#datapath:5#icore:2

grep "datapath icore all parameter ocr-frames#datapath:*#icore:*" inputfile > output file
Any awk or sed commands to do this?

Last edited by wackysiy1024; 09-03-2020 at 11:15 PM. Reason: added quotes
 
Old 09-03-2020, 10:35 PM   #2
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 21,128

Rep: Reputation: 4121Reputation: 4121Reputation: 4121Reputation: 4121Reputation: 4121Reputation: 4121Reputation: 4121Reputation: 4121Reputation: 4121Reputation: 4121Reputation: 4121
You are likely confusing globbing and regex - you can't use the asterisk like that as a wildcard in regex. What happens if it isn't a number - do you care ?.

Replace the aster with a period (full-stop).

Last edited by syg00; 09-03-2020 at 10:40 PM. Reason: forgot last bit
 
1 members found this post helpful.
Old 09-03-2020, 11:59 PM   #3
berndbausch
LQ Addict
 
Registered: Nov 2013
Location: Tokyo
Distribution: Mostly Ubuntu and Centos
Posts: 6,316

Rep: Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002
Quote:
Originally Posted by wackysiy1024 View Post
I wanted to get all the paths printed(in this case path 0-path 3)It didnt work.?
The asterisk in a regular expression matches a string of zero or more copies of the preceding character. Thus, "path *#" means the string "path", followed by zero or more spaces, followed by a hash sign. Therefore, none of the paths match this regular expression.

awk and sed are no different. For your purpose, grep is the right tool, but use syg00's advice.

Last edited by berndbausch; 09-04-2020 at 12:01 AM.
 
1 members found this post helpful.
Old 09-04-2020, 03:14 AM   #4
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,850

Rep: Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309
you might want to use an online regex tester like this: https://regex101.com/
it will not only allow you to test your expression, but also explain it
 
2 members found this post helpful.
Old 09-04-2020, 03:18 AM   #5
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,309
Blog Entries: 3

Rep: Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721
Quote:
Originally Posted by syg00 View Post
You are likely confusing globbing and regex - you can't use the asterisk like that as a wildcard in regex.
See also "man 7 glob" about globbing versus "man 7 regex" and "man perlre" in addition to "man grep" for regular expressions.
 
2 members found this post helpful.
  


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



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] grep for a string with both letters and digits... masavini Programming 8 12-17-2018 10:14 PM
i am not able to concatenate both the string in shell script as below string spatil20 Linux - Newbie 16 04-24-2016 02:59 AM
Creating an alias in ksh that uses grep and includes 'grep -v grep' doug248 Linux - Newbie 2 08-05-2012 02:07 PM
[SOLVED] copy string a to string b and change string b with toupper() and count the chars beep3r Programming 3 10-22-2010 07:22 PM
Spaces and escaped spaces pslacerda Linux - Newbie 13 12-20-2008 09:03 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 03:14 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