LinuxQuestions.org
Visit the LQ Articles and Editorials section
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Distributions > Slackware
User Name
Password
Slackware This Forum is for the discussion of Slackware Linux.

Notices



Reply
 
Search this Thread
Old 12-15-2012, 09:18 AM   #1
kikinovak
Senior Member
 
Registered: Jun 2011
Location: Montpezat (South France)
Distribution: Slackware, Slackware64
Posts: 1,960

Rep: Reputation: 978Reputation: 978Reputation: 978Reputation: 978Reputation: 978Reputation: 978Reputation: 978Reputation: 978
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?
 
Old 12-15-2012, 09:24 AM   #2
kikinovak
Senior Member
 
Registered: Jun 2011
Location: Montpezat (South France)
Distribution: Slackware, Slackware64
Posts: 1,960

Original Poster
Rep: Reputation: 978Reputation: 978Reputation: 978Reputation: 978Reputation: 978Reputation: 978Reputation: 978Reputation: 978
OK, I'll answer that myself. No need to invoke awk and sed here, since basename and dirname are exactly meant for that.
 
Old 12-15-2012, 10:20 AM   #3
Didier Spaier
Senior Member
 
Registered: Nov 2008
Location: Paris, France
Distribution: Slackware{,64}-{14.1,current} on a Lenovo Thinkpad W520
Posts: 4,683

Rep: Reputation: 1240Reputation: 1240Reputation: 1240Reputation: 1240Reputation: 1240Reputation: 1240Reputation: 1240Reputation: 1240Reputation: 1240
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
 
1 members found this post helpful.
Old 12-15-2012, 12:40 PM   #4
kikinovak
Senior Member
 
Registered: Jun 2011
Location: Montpezat (South France)
Distribution: Slackware, Slackware64
Posts: 1,960

Original Poster
Rep: Reputation: 978Reputation: 978Reputation: 978Reputation: 978Reputation: 978Reputation: 978Reputation: 978Reputation: 978
Thanks very much !
 
Old 12-15-2012, 12:58 PM   #5
colucix
Moderator
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957
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
 
1 members found this post helpful.
Old 12-15-2012, 02:22 PM   #6
kikinovak
Senior Member
 
Registered: Jun 2011
Location: Montpezat (South France)
Distribution: Slackware, Slackware64
Posts: 1,960

Original Poster
Rep: Reputation: 978Reputation: 978Reputation: 978Reputation: 978Reputation: 978Reputation: 978Reputation: 978Reputation: 978
If I ever forget how I appreciate the Slackware LQ forum, threads like this one are an excellent reminder. Thanks, again.
 
Old 12-15-2012, 09:34 PM   #7
allend
Senior Member
 
Registered: Oct 2003
Location: Melbourne
Distribution: Slackware-current
Posts: 3,525

Rep: Reputation: 877Reputation: 877Reputation: 877Reputation: 877Reputation: 877Reputation: 877Reputation: 877
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
 
1 members found this post helpful.
Old 12-15-2012, 10:04 PM   #8
perbh
Member
 
Registered: May 2008
Location: Republic of Texas
Posts: 261

Rep: Reputation: 40
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 ...
 
Old 12-15-2012, 10:22 PM   #9
wildwizard
Member
 
Registered: Apr 2009
Location: Oz
Distribution: slackware64-14.0
Posts: 755

Rep: Reputation: 227Reputation: 227Reputation: 227
Quote:
Originally Posted by perbh View Post
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

Last edited by wildwizard; 12-15-2012 at 10:27 PM.
 
Old 12-16-2012, 03:18 AM   #10
Didier Spaier
Senior Member
 
Registered: Nov 2008
Location: Paris, France
Distribution: Slackware{,64}-{14.1,current} on a Lenovo Thinkpad W520
Posts: 4,683

Rep: Reputation: 1240Reputation: 1240Reputation: 1240Reputation: 1240Reputation: 1240Reputation: 1240Reputation: 1240Reputation: 1240Reputation: 1240
Quote:
Originally Posted by perbh View Post
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

Last edited by Didier Spaier; 12-16-2012 at 04:09 AM. Reason: made some path absolutes
 
Old 12-16-2012, 09:29 AM   #11
Martinus2u
Member
 
Registered: Apr 2010
Distribution: Slackware
Posts: 353

Rep: Reputation: 56
Quote:
Originally Posted by perbh View Post
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.
 
Old 12-16-2012, 01:45 PM   #12
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

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

http://mywiki.wooledge.org/BashFAQ/100
 
1 members found this post helpful.
  


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
[SOLVED] Grabbing the last field of a delimited entry without using "awk" or "sed" drandre Linux - Newbie 17 04-18-2012 11:54 PM
Perl: how to save an e-mail attachment on disk keeping the "&" character (no "%26"!!) d1s4st3r Programming 5 09-29-2010 10:30 PM
[SOLVED] Script to delete aligned single-character columns with no field separator? kmkocot Programming 15 04-22-2010 11:45 AM
Beagle: Searching for text string containing "-" character pgb205 Linux - Software 2 06-26-2008 08:28 PM
"Permission denied" and "recursive directory loop" when searching for string in files mack1e Linux - Newbie 5 06-12-2008 08:38 AM


All times are GMT -5. The time now is 11:58 AM.

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