LinuxQuestions.org
Visit Jeremy's Blog.
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
 
LinkBack Search this Thread
Old 12-15-2012, 08:18 AM   #1
kikinovak
Senior Member
 
Registered: Jun 2011
Location: Montpezat (South France)
Distribution: Slackware, Slackware64
Posts: 1,410

Rep: Reputation: 630Reputation: 630Reputation: 630Reputation: 630Reputation: 630Reputation: 630
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, 08:24 AM   #2
kikinovak
Senior Member
 
Registered: Jun 2011
Location: Montpezat (South France)
Distribution: Slackware, Slackware64
Posts: 1,410

Original Poster
Rep: Reputation: 630Reputation: 630Reputation: 630Reputation: 630Reputation: 630Reputation: 630
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, 09:20 AM   #3
Didier Spaier
Senior Member
 
Registered: Nov 2008
Location: Paris, France
Distribution: Slackware-14.0 on a Lenovo T61 6457-4XG
Posts: 3,644

Rep: Reputation: 828Reputation: 828Reputation: 828Reputation: 828Reputation: 828Reputation: 828Reputation: 828
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, 11:40 AM   #4
kikinovak
Senior Member
 
Registered: Jun 2011
Location: Montpezat (South France)
Distribution: Slackware, Slackware64
Posts: 1,410

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

Rep: Reputation: 1910Reputation: 1910Reputation: 1910Reputation: 1910Reputation: 1910Reputation: 1910Reputation: 1910Reputation: 1910Reputation: 1910Reputation: 1910Reputation: 1910
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, 01:22 PM   #6
kikinovak
Senior Member
 
Registered: Jun 2011
Location: Montpezat (South France)
Distribution: Slackware, Slackware64
Posts: 1,410

Original Poster
Rep: Reputation: 630Reputation: 630Reputation: 630Reputation: 630Reputation: 630Reputation: 630
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, 08:34 PM   #7
allend
Senior Member
 
Registered: Oct 2003
Location: Melbourne
Distribution: Slackware-current
Posts: 3,281

Rep: Reputation: 802Reputation: 802Reputation: 802Reputation: 802Reputation: 802Reputation: 802Reputation: 802
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, 09:04 PM   #8
perbh
Member
 
Registered: May 2008
Location: Republic of Texas
Posts: 204

Rep: Reputation: 32
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, 09:22 PM   #9
wildwizard
Member
 
Registered: Apr 2009
Location: Oz
Distribution: slackware64-14.0
Posts: 749

Rep: Reputation: 225Reputation: 225Reputation: 225
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 09:27 PM.
 
Old 12-16-2012, 02:18 AM   #10
Didier Spaier
Senior Member
 
Registered: Nov 2008
Location: Paris, France
Distribution: Slackware-14.0 on a Lenovo T61 6457-4XG
Posts: 3,644

Rep: Reputation: 828Reputation: 828Reputation: 828Reputation: 828Reputation: 828Reputation: 828Reputation: 828
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 03:09 AM. Reason: made some path absolutes
 
Old 12-16-2012, 08:29 AM   #11
Martinus2u
Member
 
Registered: Apr 2010
Distribution: Slackware
Posts: 338

Rep: Reputation: 41
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, 12: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: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946
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
Trackbacks are Off
Pingbacks are On
Refbacks are 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 10:54 PM
Perl: how to save an e-mail attachment on disk keeping the "&" character (no "%26"!!) d1s4st3r Programming 5 09-29-2010 09:30 PM
[SOLVED] Script to delete aligned single-character columns with no field separator? kmkocot Programming 15 04-22-2010 10:45 AM
Beagle: Searching for text string containing "-" character pgb205 Linux - Software 2 06-26-2008 07:28 PM
"Permission denied" and "recursive directory loop" when searching for string in files mack1e Linux - Newbie 5 06-12-2008 07:38 AM


All times are GMT -5. The time now is 10:19 AM.

Main Menu
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