LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   simple parsing (ksh scripting) (https://www.linuxquestions.org/questions/programming-9/simple-parsing-ksh-scripting-608123/)

tostay2003 12-20-2007 09:27 AM

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.

jim mcnamara 12-20-2007 11:41 AM

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"?

ghostdog74 12-20-2007 11:53 PM

GNUawk
Code:

# s="-c abcdefg -d hijklmnop -e abracadabra    1234    -f gmail"
# echo $s | awk '{b=gensub(".*-e(.*)-.*","\\1","g");print b}'
 abracadabra 1234


tostay2003 12-24-2007 08:59 AM

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/'

tostay2003 12-24-2007 10:43 AM

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 ' ' '.'


All times are GMT -5. The time now is 03:13 AM.