LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
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 12-20-2007, 09:27 AM   #1
tostay2003
Member
 
Registered: Jun 2006
Posts: 126

Rep: Reputation: 15
simple parsing (ksh scripting)


I want to parse a string as below

Code:
-c abcdefg -d hijklmnop -e abracadabra     1234     -f gmail
I need to obtain all the data after '-e' and before '-' with '.' filled up between two words i.e. I would need the output

Code:
abracadabra.1234
I don't want to use getopts. I want to obtain the result with sed/awk/grep/tr or anything else. I am new to these things, so partially successful in obtaining the code. But looks lot messy. I am sure there might be a good way to implement.

Can someone help me out.
 
Old 12-20-2007, 11:41 AM   #2
jim mcnamara
Member
 
Registered: May 2002
Posts: 964

Rep: Reputation: 36
One way -
Code:
echo $@ | awk '{ split($0,arr, " ")
          while(arr[i]!="-e") { i++; continue}
          printf("%s.%s", arr[++i], arr[++i])
          } '
In all honesty, this seem to be an odd thing to do. For example what if there is no "-e"?
 
Old 12-20-2007, 11:53 PM   #3
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
GNUawk
Code:
# s="-c abcdefg -d hijklmnop -e abracadabra     1234     -f gmail"
# echo $s | awk '{b=gensub(".*-e(.*)-.*","\\1","g");print b}'
 abracadabra 1234
 
Old 12-24-2007, 08:59 AM   #4
tostay2003
Member
 
Registered: Jun 2006
Posts: 126

Original Poster
Rep: Reputation: 15
I didn't wanted to use a while loop for this tasks.

When I run the other code, I get an error that gensub is not defined.

I started off just to extract the matched word. But when I run this, I get an error
Code:
echo "$S" | sed 's/-e.*-/\1/'

Last edited by tostay2003; 12-24-2007 at 09:14 AM.
 
Old 12-24-2007, 10:43 AM   #5
tostay2003
Member
 
Registered: Jun 2006
Posts: 126

Original Poster
Rep: Reputation: 15
Got it - I am pasting the code below. Sed is used to remove anything before -e (including -e and spaces after it) + removing anything after -(including spaces before it). Then I use tr to squeeze spaces between and replaces them with '.'

Quote:
echo "$S" | sed 's/.*-e[ \t]*//;s/[ \t]*-.*//'| tr -s -A ' ' '.'
 
  


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
Shell Scripting for KSH sagarbsa Programming 1 10-12-2007 05:37 AM
What is the best way to parsing, handle files ksh, perl, c++ richikiki Programming 7 07-06-2006 08:18 AM
Three ksh scripting questions liguorir Linux - Software 2 10-22-2005 12:40 PM
KSH Scripting case statement..... ']['HeBroken Programming 1 12-10-2004 10:38 AM
some ksh scripting nabil Programming 1 03-03-2002 10:34 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

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