LinuxQuestions.org
Review your favorite Linux distribution.
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 09-16-2005, 02: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, 03:28 PM   #2
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
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, 03:57 PM   #3
setianusa
LQ Newbie
 
Registered: Oct 2003
Posts: 9

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


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

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

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