LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Slackware (http://www.linuxquestions.org/questions/slackware-14/)
-   -   Reading two variables in a character string with "/" as field separator (http://www.linuxquestions.org/questions/slackware-14/reading-two-variables-in-a-character-string-with-as-field-separator-4175441553/)

kikinovak 12-15-2012 08:18 AM

Reading two variables in a character string with "/" as field separator
 
Hi,

I have a series of character strings that look like this:

Code:

ap/recode
d/jdk
l/a52dec
xfce/xfwm4-theme-axe

And so on.

Let's say I have this:

Code:

PACKAGE="ap/recode"
How do I trim and read it with a field separator "/", so I can get the two resulting variables:

Code:

CATEGORY=ap
PACKAGENAME=recode

I vaguely know it can be done with tools like sed and awk, but "my latin ends here", as we say in my home country.

Any suggestions?

kikinovak 12-15-2012 08:24 AM

OK, I'll answer that myself. No need to invoke awk and sed here, since basename and dirname are exactly meant for that.

Didier Spaier 12-15-2012 09:20 AM

Reinventing the wheel is not mandatory...
 
But it is not forbidden either, so here you are ;)

Code:

#!/bin/bash
cat << EOF > kiki.txt
ap/recode
d/jdk
l/a52dec
xfce/xfwm4-theme-axe
EOF
awk -v FS="/" '{category=$1;packagename=$2;print category "/" packagename}' kiki.txt


kikinovak 12-15-2012 11:40 AM

Thanks very much !

colucix 12-15-2012 11:58 AM

Another possibility is by means of the read built-in, by setting the Input Field Separator and feeding it by a here string:
Code:

$ PACKAGE="ap/recode"
$ IFS=/ read CATEGORY PACKAGENAME <<< "$PACKAGE"
$ echo $CATEGORY
ap
$ echo $PACKAGENAME
recode


kikinovak 12-15-2012 01:22 PM

If I ever forget how I appreciate the Slackware LQ forum, threads like this one are an excellent reminder. Thanks, again.

allend 12-15-2012 08:34 PM

Just to add to the list of suggestions :)

This can also be done using bash parameter expansions
Code:

bash-4.2$ PACKAGE="ap/recode"; echo ${PACKAGE%%/*}; echo ${PACKAGE##*/}
ap
recode

or by using cut
Code:

bash-4.2$ echo "ap/recode" | cut -d/ -f1 
ap
bash-4.2$ echo "ap/recode" | cut -d/ -f2
recode


perbh 12-15-2012 09:04 PM

and ...
most of these will fail if there are more than one '/' (eg /usr/share/xfce/xfwm4-theme-axe)
they all work admirably if you can guarantee only one.

Off the cuff, I'm not quite sure how to handle multiple ones - unless you just want the 'dirname' and 'basename' and are not worried about 'dirname' being a full pathname containing slashes ... or you could progressively use dirname until it return just a '.' ...

Just my 2c ...

wildwizard 12-15-2012 09:22 PM

Quote:

Originally Posted by perbh (Post 4850149)
Off the cuff, I'm not quite sure how to handle multiple ones

Given the usage shown by kiki if there was more to it then it would be a path with the package last and the category second last. This is a simple one that can be dealt with by using reverse then cutting it after the second delimiter and reversing it again.

EDIT In the interests of insane bash scripts here is one of mine :

http://wildwizard.abnormalpenguin.com/linux/checkx11

Didier Spaier 12-16-2012 02:18 AM

Quote:

Originally Posted by perbh (Post 4850149)
and ...
most of these will fail if there are more than one '/' (eg /usr/share/xfce/xfwm4-theme-axe)

Then this one, for instance:
Code:

#!/bin/bash
cat << EOF > kiki.txt
ap/recode
/d/jdk
//l/a52dec
this////one//includes/consecutive/slashes
this/one/has/a/very/very/very/very/very/long/path
xfce/xfwm4-theme-axe
EOF
awk -v FS="/" '{dirname=substr($0,1,length-length($NF));basename=$NF;print "dirname: " dirname " basename: " basename}' kiki.txt

As the saying goes, there's more than one way to skin a cat ;)

EDIT

If the category is the second last:

Code:

awk -v FS="/" '{if (NF > 1) {print "category: " $(NF-1) " packagename: " $NF} else {print "no category, packagename: " $NF}}' kiki.txt

Martinus2u 12-16-2012 08:29 AM

Quote:

Originally Posted by perbh (Post 4850149)
most of these will fail if there are more than one '/' (eg /usr/share/xfce/xfwm4-theme-axe)

allend's solution needs only slight modification:

Code:

PACKAGE="ap/my/test/recode"; echo ${PACKAGE%%/*}; echo ${PACKAGE#*/}
NB: for non-trivial work with patterns and strings i wouldn't bother with bash and sed/awk but go straight into perl scripting. less clumsy and much faster.

David the H. 12-16-2012 12:45 PM

For a full overview of built-in string manipulation techniques like these, see here:

http://mywiki.wooledge.org/BashFAQ/100


All times are GMT -5. The time now is 12:10 PM.