LinuxQuestions.org
Support LQ: Use code LQ3 and save $3 on Domain Registration
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 09-16-2005, 03:05 PM   #1
setianusa
LQ Newbie
 
Registered: Oct 2003
Posts: 9

Rep: Reputation: 0
Simple question about sed or awk


Hi,

I have a variable which may have a value like this :
i=$(echo "-pabc.par -sXYZ:123 -cZZ")
or like this :
i=$(echo "-sXYZ:123")
or like this :
i=$(echo "") # empty, or null

From that $i variable, I would like to generate 3 variables, $P, $S, and $C
In the first case :
$P=abc.par
$S=XYZ:123
$C=ZZ

In the second one
$S=XYZ:123
$P and $C are null or empty

And in the third one all 3 variables are empty or null.

How can I do this using sed or awk ?

Thanks for any help.
 
Old 09-16-2005, 04:28 PM   #2
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374
Hi,

Does this have to be done with sed or awk?

This does what you want, but no sed or awk is used:

Code:
#!/bin/bash

# test string used:
i="-pabc.par -sXYZ:123 -cZZ"

# start with nullified variables
P=""
S=""
S=""

# parse contents of i one at the time (if any):
for THIS in ${i}
do
  # check to see if first 2 characters match.
  case ${THIS} in
    -p*) P=${THIS:2} ;; # a match on -p , remove first 2 characters
    -s*) S=${THIS:2} ;;
    -c*) C=${THIS:2} ;;
  esac
done

# print variables:
echo "P: ${P}"
echo "S: ${S}"
echo "C: ${C}"

exit 0
A sed or awk solution is probably possible, but this seems to be one of the simpler ways of doing it (my 2c, that is ).

Anyway, hope this helps.
 
Old 09-16-2005, 04:57 PM   #3
setianusa
LQ Newbie
 
Registered: Oct 2003
Posts: 9

Original Poster
Rep: Reputation: 0
Thanks a lot druuna !
 
  


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
Simple bash/awk/sed scripting question R00ts Programming 4 04-16-2005 03:55 AM
awk/sed help pantera Programming 1 05-14-2004 12:59 AM
sed or awk question - replace caps with small letters computera Linux - General 1 12-30-2003 05:39 AM
sed/awk problem player_2 Programming 9 08-26-2003 07:09 PM
using sed, simple question bobthebat Programming 3 04-30-2001 11:01 PM


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