LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 09-27-2013, 12:15 PM   #1
ezekieldas
Member
 
Registered: Mar 2010
Posts: 122

Rep: Reputation: 16
bash: removing whitespace filled element from array


First off, I have to do this in bash. I realize there are other langs better suited but doing it in shell is a hard requirement.

I have a section of this script where I get a blob of data and start working with it as an array. There's this one section that is problematic, I want to get rid of the entire key/value "comments." I can get rid of the key, "comments=" (see below) but I need something that keeps going until it gets to "enabled." Any ideas as to how I might do this? I found a few things that come close but not quite here: http://tldp.org/LDP/abs/html/arrays.html

Code:
BLOB="name=foo1 subnets=120.111.32.0/25 site=XYZ comments=this is a comment enabled=true specialCase=false"

stuff=( $BLOB )

echo ${stuff[@]/comments=/}
 
Old 09-27-2013, 12:33 PM   #2
Keith Hedger
Senior Member
 
Registered: Jun 2010
Location: Wiltshire, UK
Distribution: Linux From Scratch, Slackware64, Partedmagic
Posts: 2,253

Rep: Reputation: 559Reputation: 559Reputation: 559Reputation: 559Reputation: 559Reputation: 559
Code:
BLOB="name=foo1 subnets=120.111.32.0/25 site=XYZ comments=this is a comment enabled=true specialCase=false"
p1="${BLOB%%comments=?*}"
p2="enabled=${BLOB##?*enabled=}"
echo "$p1 $p2"
Grab a copy of the advanced bash scripting guide.
 
1 members found this post helpful.
Old 09-27-2013, 12:46 PM   #3
ezekieldas
Member
 
Registered: Mar 2010
Posts: 122

Original Poster
Rep: Reputation: 16
Thanks Keith! Can you tell me what the notation BLOB%% is called? I'd like to read more about that.

I found that just this is working fine, how does ?* know to read up to "enabled"

Code:
BLOB="name=foo1 subnets=120.111.32.0/25 site=XYZ comments=this is a comment so deal with it. enabled=true specialCase=false"
stuff="${BLOB%%comments=?*}"
echo ${stuff[@]}
 
Old 09-27-2013, 02:12 PM   #4
Keith Hedger
Senior Member
 
Registered: Jun 2010
Location: Wiltshire, UK
Distribution: Linux From Scratch, Slackware64, Partedmagic
Posts: 2,253

Rep: Reputation: 559Reputation: 559Reputation: 559Reputation: 559Reputation: 559Reputation: 559
p1="${BLOB%%comments=?*}" strips the longest match of comments=?* from then end of the string giving:
name=foo1 subnets=120.111.32.0/25 site=XYZ
p2="enabled=${BLOB##?*enabled=}" strips the longest match of ?*enabled= from the front of the string giving:
enabled=true specialCase=false
"$p1 $p2" puts the back together.

"?*" matches any number of single characters.

so ?*enabled= matches "name=foo1 subnets=120.111.32.0/25 site=XYZ comments=this is a comment enabled=" and strips it.

comments=?* matches "comments=this is a comment enabled=true specialCase=false" and strips it.

All this is in the indispensable abs guide.
 
Old 09-30-2013, 09:43 PM   #5
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.8, Centos 5.10
Posts: 17,241

Rep: Reputation: 2325Reputation: 2325Reputation: 2325Reputation: 2325Reputation: 2325Reputation: 2325Reputation: 2325Reputation: 2325Reputation: 2325Reputation: 2325Reputation: 2325
The technique is known as 'parameter expansion'; here's a really good guide http://wiki.bash-hackers.org/syntax/pe
 
  


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
Check bash array element for whitespace or empty darkangel29 Programming 8 08-07-2013 03:12 AM
BASH-Adding array element: Naming issue using array[${#array[*]}]=5 calvarado777 Programming 8 07-26-2013 10:48 PM
bash array substitute the value of nth element casperdaghost Linux - Newbie 11 03-28-2013 05:15 AM
get index of an element in array in bash mangatmodi Programming 4 11-20-2009 08:45 AM
BASH: Removing ALL whitespace from variable eur0dad Programming 1 09-07-2006 11:25 AM


All times are GMT -5. The time now is 08:25 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration