473,382 Members | 1,165 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

advanced split in bash

Ciary
247 Expert 100+
Hi,

i'm not an expert in bash and i have a question. is there a way to split a string on a sequence of characters rather than just one?

the idea is the following
Expand|Select|Wrap|Line Numbers
  1. workdir=`pwd`
  2. echo $workdir
  3. # /path/to/my/split/directory/i/want/
  4. arr=$(echo $workdir | tr "/split/" "\n")
  5. echo $arr[0]
  6. # /path/to/my
  7. echo $arr[1]
  8. # directory/i/want/
  9.  
this is not what this code actually does, but that's what i tried and wanted it to do. when i execute this, the string in workdir is actually split on all characters that match a character in "/split/"

tnx
Oct 29 '12 #1
2 5081
Ciary
247 Expert 100+
there is a workaround
Expand|Select|Wrap|Line Numbers
  1. workdir=`pwd`
  2. echo $workdir
  3. # /path/to/my/split/directory/i/want/
  4. temp=$(echo $workdir | sed "s/\/split\// /g")
  5. echo $temp
  6. # /path/to/my directory/i/want/
  7. arr=( $temp )
  8. echo ${arr[0]}
  9. # /path/to/my
  10. echo ${arr[1]}
  11. # directory/i/want/
  12.  
but there has to be a way to do this without the temp-variable, right?
Oct 29 '12 #2
Luuk
1,047 Expert 1GB
Expand|Select|Wrap|Line Numbers
  1. #!/bin/bash
  2.  
  3. workdir=/path/to/my/split/directory/i/want/
  4. echo "workdir: $workdir"
  5. p1=/split/*
  6. echo "1: " ${workdir/$p1}
  7. echo "workdir: $workdir"
  8. p2=*/split/
  9. echo "2: " ${workdir/$p2}
  10.  
output:
Expand|Select|Wrap|Line Numbers
  1. workdir: /path/to/my/split/directory/i/want/
  2. 1:  /path/to/my
  3. workdir: /path/to/my/split/directory/i/want/
  4. 2:  directory/i/want/
  5.  
read more on this in the man-page:
${parameter/pattern/string}
Pattern substitution. The pattern is expanded to produce a pat-
tern just as in pathname expansion. Parameter is expanded and
the longest match of pattern against its value is replaced with
string. If pattern begins with /, all matches of pattern are
replaced with string. Normally only the first match is
replaced. If pattern begins with #, it must match at the begin-
ning of the expanded value of parameter. If pattern begins with
%, it must match at the end of the expanded value of parameter.
If string is null, matches of pattern are deleted and the / fol-
lowing pattern may be omitted. If parameter is @ or *, the sub-
stitution operation is applied to each positional parameter in
turn, and the expansion is the resultant list. If parameter is
an array variable subscripted with @ or *, the substitution
operation is applied to each member of the array in turn, and
the expansion is the resultant list.
Nov 3 '12 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: bill | last post by:
Consider the following: import os, commands os.environ="string with foo" a = '$QWE ${QWE/foo/baz}' b = commands.getoutput('echo ' + a) This does what I want, which is to expand a...
31
by: Rod Pemberton | last post by:
It seems there is a strong division between those who want to take the group to a higher level and those who are just learning. Perhaps, it's time to split the newsgroup. I'd suggest the...
11
by: vdrab | last post by:
Hi all, Is there some sort of coherent source (dead tree format, maybe?) on some of the more advanced features of python (decorators, metaclasses, etc)? I'm sort of looking for a python book...
2
by: Bryan_Cockrell | last post by:
Hi World, I am extracting text from an ebcdic header using dd in the cygwin environment (bash/ksh) as below in order to rename the file to something intelligent. I'm using a specific string...
16
by: John Salerno | last post by:
Hi all. I just installed Ubuntu and I'm learning how to use the bash shell. Aside from the normal commands you can use, I was wondering if it's possible to use Python from the terminal instead of...
3
by: Tempo | last post by:
Hey. I am trying to grab the prices from the string below but I get a few errors when I try to do it: Take a look at the code and error messages below for me and thanks you in advanced to all that...
4
by: melmack3 | last post by:
Hello My PHP script executes many bash/cmd commands. Functions like "exec()" or "system()" cause that new bash/cmd session is started, the command is executed and the session is closed....
8
by: Peter Otten | last post by:
hofer wrote: for line in open("file"): # read from file try: a, b = map(int, line.split(None, 2)) # remove extra columns, # convert to integer except ValueError: pass # remove comments,...
2
by: Lars | last post by:
Hi I got some programming experience and I recently started looking into Python. I've read much of the tutorial from 2.6 documentation. But it was more interesting to get started on something I...
0
by: Marek Dohojda | last post by:
I need to split an output from a bash script and populate an array with it. So I can manipulate and print that later. Unfortunately I can not find a way to do that. so what I have thus far is...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.