LinuxQuestions.org
Visit Jeremy's Blog.
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 02-20-2009, 04:51 PM   #1
xikspan
LQ Newbie
 
Registered: Feb 2009
Posts: 2

Rep: Reputation: 0
Korn Shell Parse Something out of a Line


I have a line such as:

fr=domain reply=0 comment="Success"

I need to either parse out the value for reply or the value for comment to use as a condition.

This is in an existing Korn shell.

I was trying to use awk, but could not just get the value of reply. Also, sometimes there are more fields, so I can't just print the second column.

Thanks.
 
Old 02-20-2009, 05:29 PM   #2
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976
Maybe using sed?
Code:
echo fr=domain reply=0 comment="Success" | sed 's/.*reply=\(.*\) .*/\1/'
 
Old 02-23-2009, 12:07 PM   #3
xikspan
LQ Newbie
 
Registered: Feb 2009
Posts: 2

Original Poster
Rep: Reputation: 0
Thanks. This gives me:

1 command=scrap_wafer

I can awk the first column, probably.
 
Old 02-23-2009, 12:15 PM   #4
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976
Quote:
Originally Posted by xikspan View Post
1 command=scrap_wafer
Where does this string comes from? Can you post the exact command you issued and/or a real example of input data?
 
Old 02-23-2009, 12:51 PM   #5
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976
This should work better:
Code:
echo fr=domain reply=1 command=scrap_wafer comment="Success" | sed 's/.*reply=\([[:alnum:]]*\) .*/\1/'
the problem was that the dot inside the \(.*\) expression matches any character including a blank space, so that if you have more field after reply=something, separated by spaces, all of them (but the last one) are matched. The expression [:alnum:] matches alphanumeric character only, that is spaces, tabs and punctuations are excluded.
 
Old 02-23-2009, 02:01 PM   #6
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,576
Blog Entries: 31

Rep: Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195
Hello xikspan

An alternative, which will run quicker, is
Code:
#! /bin/bash

x='fr=domain reply=0 comment="Success"'
buf="${x#fr=}"
fr="${buf%% *}" 
echo $fr

buf="${x%\"}"
comment="${buf##*\"}"
echo $comment
Best

Charles
 
  


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
(Korn Shell/POSIX) want to make sure last character in line is a space quadmore Programming 2 09-08-2008 10:57 AM
How to parse in korn shell eddieboo Linux - Newbie 1 04-19-2008 08:06 AM
Globbing using Korn Shell metallica1973 Programming 1 12-05-2007 05:56 PM
Using a Korn shell in rc scripts desbyleo Solaris / OpenSolaris 3 11-12-2004 06:27 PM
Korn shell script Muzica Solaris / OpenSolaris 4 09-06-2004 01:47 PM


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