Perl Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesPerl Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old November 5th, 2012, 02:05 PM
Elizabeth H Elizabeth H is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2012
Posts: 8 Elizabeth H User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 3 h 15 m 47 sec
Reputation Power: 0
How to grep from a list

Hi

Below is what I am trying to do. I have a variable which contains a list of commands seperated by ":". While trying to use a for loop to get each command, it doesnt retrun the correct results

Example:

var="command -1:command -2*: command something*"
for c in `echo $var | tr ':' ' '`
do
echo $c
done

Instead of getting the below
command -1
command -2*
command something

I get
command
-1
command
-2*
command
something

Please help

Reply With Quote
  #2  
Old November 5th, 2012, 03:17 PM
Laurent_R Laurent_R is offline
Contributing User
Dev Shed Novice (500 - 999 posts)
 
Join Date: Jun 2012
Posts: 502 Laurent_R User rank is Major (30000 - 40000 Reputation Level)Laurent_R User rank is Major (30000 - 40000 Reputation Level)Laurent_R User rank is Major (30000 - 40000 Reputation Level)Laurent_R User rank is Major (30000 - 40000 Reputation Level)Laurent_R User rank is Major (30000 - 40000 Reputation Level)Laurent_R User rank is Major (30000 - 40000 Reputation Level)Laurent_R User rank is Major (30000 - 40000 Reputation Level)Laurent_R User rank is Major (30000 - 40000 Reputation Level)Laurent_R User rank is Major (30000 - 40000 Reputation Level)Laurent_R User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 4 Days 18 h 50 m 40 sec
Reputation Power: 385
Is this supposed to be Perl?

It looks like shell to me. But some parts are looking a bit like Perl. Don't do such things. Do it in Perl or in shell script, not a mixture of both (even though you may call some shell command from Perl if you need).

Reply With Quote
  #3  
Old November 5th, 2012, 03:55 PM
Elizabeth H Elizabeth H is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2012
Posts: 8 Elizabeth H User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 3 h 15 m 47 sec
Reputation Power: 0
This is shell and I keep posting this in Perl coz I dont see a seperate section for shell scripting. I am not sure if I missed it but I did look for it

Reply With Quote
  #4  
Old November 5th, 2012, 08:55 PM
spacebar208's Avatar
spacebar208 spacebar208 is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Apr 2012
Location: spaceBAR Central
Posts: 188 spacebar208 User rank is Sergeant Major (2000 - 5000 Reputation Level)spacebar208 User rank is Sergeant Major (2000 - 5000 Reputation Level)spacebar208 User rank is Sergeant Major (2000 - 5000 Reputation Level)spacebar208 User rank is Sergeant Major (2000 - 5000 Reputation Level)spacebar208 User rank is Sergeant Major (2000 - 5000 Reputation Level)spacebar208 User rank is Sergeant Major (2000 - 5000 Reputation Level) 
Time spent in forums: 2 Days 9 h 5 m
Reputation Power: 41
Try it using an array:
Code:
$ cat test.sh
var=("command -1:" "command -2*:" "command something*")
for c in "${var[@]}"
do
  echo $c | sed 's/\://g'
done

$ test.sh
command -1
command -2*
command something*

Reply With Quote
  #5  
Old November 5th, 2012, 09:04 PM
Elizabeth H Elizabeth H is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2012
Posts: 8 Elizabeth H User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 3 h 15 m 47 sec
Reputation Power: 0
Thanks a bunch!!!!

Reply With Quote
  #6  
Old November 6th, 2012, 01:08 AM
Laurent_R Laurent_R is offline
Contributing User
Dev Shed Novice (500 - 999 posts)
 
Join Date: Jun 2012
Posts: 502 Laurent_R User rank is Major (30000 - 40000 Reputation Level)Laurent_R User rank is Major (30000 - 40000 Reputation Level)Laurent_R User rank is Major (30000 - 40000 Reputation Level)Laurent_R User rank is Major (30000 - 40000 Reputation Level)Laurent_R User rank is Major (30000 - 40000 Reputation Level)Laurent_R User rank is Major (30000 - 40000 Reputation Level)Laurent_R User rank is Major (30000 - 40000 Reputation Level)Laurent_R User rank is Major (30000 - 40000 Reputation Level)Laurent_R User rank is Major (30000 - 40000 Reputation Level)Laurent_R User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 4 Days 18 h 50 m 40 sec
Reputation Power: 385
Quote:
Originally Posted by Elizabeth H
This is shell and I keep posting this in Perl coz I dont see a seperate section for shell scripting. I am not sure if I missed it but I did look for it


Allright, sorry, but then, may be you should tell it upfront, because I was initially thinking: "oh, man, what a mess, she really understands nothing to Perl and thinks it is like shell." ;-)

I guess you have a solution by now.

Reply With Quote
  #7  
Old November 6th, 2012, 05:35 AM
Axweildr's Avatar
Axweildr Axweildr is offline
'fie' on me, allege-dly
Click here for more information.
 
Join Date: Mar 2003
Location: in da kitchen ...
Posts: 12,874 Axweildr User rank is General 81st Grade (Above 100000 Reputation Level)Axweildr User rank is General 81st Grade (Above 100000 Reputation Level)Axweildr User rank is General 81st Grade (Above 100000 Reputation Level)Axweildr User rank is General 81st Grade (Above 100000 Reputation Level)Axweildr User rank is General 81st Grade (Above 100000 Reputation Level)Axweildr User rank is General 81st Grade (Above 100000 Reputation Level)Axweildr User rank is General 81st Grade (Above 100000 Reputation Level)Axweildr User rank is General 81st Grade (Above 100000 Reputation Level)Axweildr User rank is General 81st Grade (Above 100000 Reputation Level)Axweildr User rank is General 81st Grade (Above 100000 Reputation Level)Axweildr User rank is General 81st Grade (Above 100000 Reputation Level)Axweildr User rank is General 81st Grade (Above 100000 Reputation Level)Axweildr User rank is General 81st Grade (Above 100000 Reputation Level)Axweildr User rank is General 81st Grade (Above 100000 Reputation Level)Axweildr User rank is General 81st Grade (Above 100000 Reputation Level)Axweildr User rank is General 81st Grade (Above 100000 Reputation Level)  Folding Points: 162285 Folding Title: Super Ultimate Folder - Level 1Folding Points: 162285 Folding Title: Super Ultimate Folder - Level 1Folding Points: 162285 Folding Title: Super Ultimate Folder - Level 1Folding Points: 162285 Folding Title: Super Ultimate Folder - Level 1Folding Points: 162285 Folding Title: Super Ultimate Folder - Level 1Folding Points: 162285 Folding Title: Super Ultimate Folder - Level 1
Time spent in forums: 4 Months 2 Weeks 1 Day 20 h 28 m 56 sec
Reputation Power: 6421
Send a message via Google Talk to Axweildr
Orkut
Unix Help or Linux Help is probably where it should be posted, but the description doesn't mention shell scripting

Windows Help used to have a good few WSH questions at one time, but I don't spend too much time there
__________________
--Ax
without exception, there is no rule ...
Handmade Irish Jewellery
Targeted Advertising Cookie Optout (TACO) extension for Firefox
The great thing about Object Oriented code is that it can make small, simple problems look like large, complex ones


09 F9 11 02
9D 74 E3 5B
D8 41 56 C5
63 56 88 C0
Some people, when confronted with a problem, think "I know, I'll use regular expressions." Now they have two problems.
-- Jamie Zawinski
Detavil - the devil is in the detail, allegedly, and I use the term advisedly, allegedly ... oh, no, wait I did ...
BIT COINS ANYONE

Reply With Quote
  #8  
Old November 7th, 2012, 06:50 AM
steveeshair steveeshair is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2012
Posts: 3 steveeshair Negative: is most likely a SPAMMER and a traitor to the cause. 
Time spent in forums: 34 m
Reputation Power: 0
I'm on Solaris and it is saying there is no -d option for grep.

I see what you were doing there. Nice and simple if I were on something a bit more full featured.

so close, so far away...

Thanks

Reply With Quote
  #9  
Old November 7th, 2012, 12:20 PM
Laurent_R Laurent_R is offline
Contributing User
Dev Shed Novice (500 - 999 posts)
 
Join Date: Jun 2012
Posts: 502 Laurent_R User rank is Major (30000 - 40000 Reputation Level)Laurent_R User rank is Major (30000 - 40000 Reputation Level)Laurent_R User rank is Major (30000 - 40000 Reputation Level)Laurent_R User rank is Major (30000 - 40000 Reputation Level)Laurent_R User rank is Major (30000 - 40000 Reputation Level)Laurent_R User rank is Major (30000 - 40000 Reputation Level)Laurent_R User rank is Major (30000 - 40000 Reputation Level)Laurent_R User rank is Major (30000 - 40000 Reputation Level)Laurent_R User rank is Major (30000 - 40000 Reputation Level)Laurent_R User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 4 Days 18 h 50 m 40 sec
Reputation Power: 385
Maybe you have egrep installed.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesPerl Programming > How to grep from a list

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap