WelcomeWelcome | FAQFAQ | DownloadsDownloads | WikiWiki

Author Topic: split a string  (Read 3025 times)

Offline jnovacek

  • Newbie
  • *
  • Posts: 30
split a string
« on: May 20, 2016, 04:40:25 PM »
how to split a string in bash?
IFS=', ' read -r -a array <<< "$string"
does not work

thanks

Offline bmarkus

  • Administrator
  • Hero Member
  • *****
  • Posts: 7183
    • My Community Forum
Re: split a string
« Reply #1 on: May 20, 2016, 05:14:27 PM »
This question has nothing to do with RPi nor TC. Please use generic support forums and Google. First hit in Google search is

http://stackoverflow.com/questions/918886/how-do-i-split-a-string-on-a-delimiter-in-bash

Once again. Use this forum for TC specific questions only.

THANKS!
Béla
Ham Radio callsign: HA5DI

"Amateur Radio: The First Technology-Based Social Network."

Offline jnovacek

  • Newbie
  • *
  • Posts: 30
Re: split a string
« Reply #2 on: May 20, 2016, 05:25:13 PM »
in other distributions it works, it does not work on Raspberry PI (Tiny Core Linux 7.0),
I get the message "syntax error: unexpected redirection"

Offline Greg Erskine

  • Sr. Member
  • ****
  • Posts: 404
Re: split a string
« Reply #3 on: May 20, 2016, 07:50:10 PM »
Are you using bash?

Offline gavinmc42

  • Sr. Member
  • ****
  • Posts: 301
Re: split a string
« Reply #4 on: May 21, 2016, 12:43:52 AM »
Standard LInux stuff, grep, sed, awk, these can be used in shell scripts.
All available in busybox on piCore.
Extremely powerful and highly obscure languages and commands 8)
I use  them to split out variables in config files and for data sorting.
One line of shell script with these in it can do the same as 100s of lines of python.
If you want to get really fancy then Perl, PHP.

Offline bmarkus

  • Administrator
  • Hero Member
  • *****
  • Posts: 7183
    • My Community Forum
Re: split a string
« Reply #5 on: May 21, 2016, 01:05:21 AM »
in other distributions it works, it does not work on Raspberry PI (Tiny Core Linux 7.0),
I get the message "syntax error: unexpected redirection"

Your original question was different. Ask what you really want to know :(
Béla
Ham Radio callsign: HA5DI

"Amateur Radio: The First Technology-Based Social Network."

Offline bmarkus

  • Administrator
  • Hero Member
  • *****
  • Posts: 7183
    • My Community Forum
Re: split a string
« Reply #6 on: May 21, 2016, 01:07:21 AM »

One line of shell script with these in it can do the same as 100s of lines of python.


Only if you can't use Python. Back to the split, in Python it just a simple string method

Code: [Select]
split(';')
Béla
Ham Radio callsign: HA5DI

"Amateur Radio: The First Technology-Based Social Network."

Offline bmarkus

  • Administrator
  • Hero Member
  • *****
  • Posts: 7183
    • My Community Forum
Re: split a string
« Reply #7 on: May 21, 2016, 01:11:25 AM »
Are you using bash?

Good question. TC (piCore) is using BusyBox shell ash. If you want to work with Bash, install bash.tcz from repo and explicitely start it.
Béla
Ham Radio callsign: HA5DI

"Amateur Radio: The First Technology-Based Social Network."

Offline jnovacek

  • Newbie
  • *
  • Posts: 30
Re: split a string
« Reply #8 on: May 21, 2016, 02:57:45 PM »
I used the "awk"
for my case it's ideal

val1=$(echo "$NAME" | awk -F':' '{print $1}')
val2=$(echo "$NAME" | awk -F':' '{print $2}')

thank you all