LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 03-03-2011, 08:15 AM   #1
voda87
LQ Newbie
 
Registered: Mar 2011
Posts: 6

Rep: Reputation: 0
Smile sed/awk/cut/grep.. looking for the best solution


Hi,
Im looking for a way to get variables out of a string..
There are a few forms that the string can be inputed by the user- thats the problem.
i need to seperate the string into 3 vars.. ($first $sign $second)
a few examples:

"name=jack"
"year>1980"
"age<25"
"name>abcd"
"price=59.99"

ok, you get the idea.. the first(left) part of the string will always be a word. The middle(sign) can only be: < , > , = .
and the third(right) part can be a number or a word, as described at the example. if the third part is a word it will do a string compare.

I need to execute an IF statement out of the string i get.
so im trying to first store each part of the string so i can know whats going on before writing the statement.

Thanks a lot for the helpers- any idea will B welcome!
 
Old 03-03-2011, 08:38 AM   #2
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950
Using only variables and parameter substitution:
Code:
string="name=jack"

first="${string%[=><]*}"

sign=${string//[^=><]}

second="${string#*[=><]}"

echo "first: $first"
echo "sign: $sign"
echo "second: $second"
Note that this all assumes that there will always be exactly one instance of =,>,< in the string.

Last edited by David the H.; 03-03-2011 at 08:39 AM. Reason: Changed variable names to reflect OP
 
Old 03-03-2011, 08:43 AM   #3
voda87
LQ Newbie
 
Registered: Mar 2011
Posts: 6

Original Poster
Rep: Reputation: 0
Smile thanks!

wow what a quick reply.. thx it looks great.. ill give it a try
 
Old 03-03-2011, 08:54 AM   #4
voda87
LQ Newbie
 
Registered: Mar 2011
Posts: 6

Original Poster
Rep: Reputation: 0
Great

works 100% , problem solved!
Thanks 10x
 
Old 03-03-2011, 08:59 AM   #5
EricTRA
Guru
 
Registered: May 2009
Location: Gibraltar, Gibraltar
Distribution: Fedora 20 with Awesome WM
Posts: 6,805
Blog Entries: 1

Rep: Reputation: 1291Reputation: 1291Reputation: 1291Reputation: 1291Reputation: 1291Reputation: 1291Reputation: 1291Reputation: 1291Reputation: 1291
Hi,

Been playing around with this one and looks like it works.
File contains
Code:
test=exit1
test2<exit2
test3>exit3
Using a command like this:
Code:
cat test | awk -F'=|<|>' '{ print $2 , $1 }'
give this result:
Code:
exit1 test
exit2 test2
exit3 test3
as expected. Hope it's useful.

Kind regards,

Eric
 
1 members found this post helpful.
  


Reply

Tags
shell


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
using SED or AWK to cut data from a file, between certain characters hugh86 Linux - Newbie 18 10-28-2010 05:17 AM
Get data from multi lined text file using awk, sed or perl - grep & cut not upto par cam34 Programming 4 07-02-2010 04:10 AM
How to use grep, cut, or awk to get an IP from a file chudster Linux - General 4 02-03-2010 08:06 PM
How to use command grep,cut,awk to cut a data from a file? hocheetiong Linux - Newbie 7 09-11-2008 08:16 PM
How do I cut out a specific piece of a html page (using sed/awk or similar)? bomix Linux - General 2 10-08-2005 05:30 PM


All times are GMT -5. The time now is 04:48 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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration