Operating System - HP-UX
1855680 Members
3771 Online
104103 Solutions
New Discussion

parameter substitution - newbie question

 
SOLVED
Go to solution
Jon Hill_3
Advisor

parameter substitution - newbie question

I'm new to UX scripting and am having trouble finding a good way to split a given variable into two.

Let's say I've assigned "abc123,def456" to $i.
1. How can I use the comma as a delimiter to split $i into $a and $b ($a="abc123"; $b="def456")?
2. How can I retrieve only the first character of $i?


Maybe I should describe what I'm really after, because there's a good chance I'm going about it all wrong.

I've got a text file that looks like this:
first, 1
second, 2
#third, 3
fourth, 4

I'm writing a script that will ignore all lines preceded by # and, for each of the remaining lines, assign everything before the comma to $a, assign everything after the comma to $b, and run a command using both variables.

I've tried it two ways.

The first way works but feels kinda ugly to me:

while read -r a b
do
case $a in
\#*) ;;
*) echo a is $a and b is $b;;
esac
done < $1


I think the second approach is better but I just can't figure out how to split the variable:

export IFS=""
for i in `grep -v "^#" $1`
do
echo full line is $i
done


Any help appreciated!
7 REPLIES 7
Jon Hill_3
Advisor

Re: parameter substitution - newbie question

I should have mentioned that I'm using posix sh.
John Poff
Honored Contributor

Re: parameter substitution - newbie question

Hi,

Here is one way to do it using a variation of your first method:

grep -v "^#" $1 | while read a b
do
echo "a is $a and b is $b"
done


That way, you filter out the lines you don't want before you operate with them.

JP
John Poff
Honored Contributor

Re: parameter substitution - newbie question

My formatting was lost in the translation. The 'while read' line is supposed to be on the next line and there is supposed to be a continuation character at the end of the line before that. Plus, I forgot to put in a line with the IFS set to a comma, otherwise the comma comes out at the end of $a.

Let me try this as an attachment.

JP
curt larson_1
Honored Contributor

Re: parameter substitution - newbie question

here is another way:
awk -F, '/^[^#]/ {printf("%s %s\n",$1,$2);}' yourFile |
while read a b
do
command $a $b
done

or

if your not interested in the command's return code or processing the output from the command:

awk -F, '/^[^#]/ {system("command "$1 $2"");}' yourFile
curt larson_1
Honored Contributor
Solution

Re: parameter substitution - newbie question

How can I use the comma as a delimiter to split $i into $a and $b ($a="abc123"; $b="def456")?

using IFS(assumes you want first two fields):
oldIFS=$IFS
IFS=","
echo $i | read a b
IFS=$oldIFS

using tr to change ,to space:
cat yourFile |tr -s "," " " | read a b

using sed to change , to space
cat yourFile | sed 's/\(.*\),\(.*\)/\1 \2/ | read a b

my previous example used awk to change , to space.

using cut(assumes you want first two fields):
a=$(echo $i | cut -d, -f1)
b=$(echo $i | cut -d, -f2)

using variable substrings(assumes their are only two fields):

$a=${$i#*,}
$b=${$i%,*}

2. How can I retrieve only the first character of $i?

x=$(echo $i| sed 's/\(.\).*/\1/')
x=$(echo $i| awk '{printf("%s",substr($0,1,1));}')
x=$(echo $i| cut -c 1)

take your pick
curt larson_1
Honored Contributor

Re: parameter substitution - newbie question

another quick one

2. How can I retrieve only the first character of $i?

typeset -L1 x
x=$i

if you can't do it ten different ways, you're not using uxix or you haven't thought long enough.
Jon Hill_3
Advisor

Re: parameter substitution - newbie question

This is exactly what I needed, guys. Thanks.

And Curt, thanks for the variable substring example; I've been puzzling over substrings all day!