Search
 
SCRIPT & CODE EXAMPLE
 

SHELL

How to split a string in bash

string="you got a friend in me"
IFS=' ' read -ra split <<< "$string"
echo "${split[*]}"
# Output: you got a friend in me
echo "${split[3]}"
# Output: friend
Comment

split string in shell script

IN="bla@some.com;john@home.com"
arrIN=(${IN//;/ })
echo ${arrIN[1]}                  # Output: john@home.com
Comment

split string in shell

#!/usr/bin/env bash

# There are different method of splitting a string.
# Two of those methods are shown below

# a sample string delimeted by ";"
IN="FirstName=John; LastName=Doe; Email=jd@someone.com"

# First method:
# splits the string on ';' and puts them into an array
person=$(echo $IN | tr ";" "
")

# you can loop through the array
for info in $person
do
    echo "> [$info]"
done



# Second method:
echo "$IN" | cut -d ";" -f 1  # returns the first part
echo "$IN" | cut -d ";" -f 2  # returns the second part

#and so on.

Comment

split sh string

#YOURSTRING="this_is_an_example"
#output:
#this
#is
#an
#example
for i in $(echo $YOURSTRING | tr "_" "
")
do
	echo $i
done
Comment

split bash string

IN="bla@some.com;john@home.com"
arrIN=(${IN//;/ })
Comment

split string using linux cmd

$ s='one_two_three_four_five'

$ A="$(cut -d'_' -f2 <<<"$s")"
$ echo "$A"
two

$ B="$(cut -d'_' -f4 <<<"$s")"
$ echo "$B"
four
Comment

PREVIOUS NEXT
Code Example
Shell :: how to mount a hard drive in ubuntu 
Shell :: microsoft todo for linux 
Shell :: imagemagick add background to transparent png 
Shell :: ffmpeg not installed 
Shell :: install brew on linux 
Shell :: conda install notebook 
Shell :: git update fork from original repo 
Shell :: create repository and push to git using command in vs code 
Shell :: install snap change in progress ubuntu 
Shell :: linux bash script to clean up log files 
Shell :: git syntax 
Shell :: ffmpeg cut video without re encoding 
Shell :: openssl universal requires cocoapods openssl-universal 
Shell :: env npm 
Shell :: ubuntu 20.04 how to check dns server 
Shell :: find out a branch a parent branch git 
Shell :: scp folder recursive 
Shell :: uninstall multipass 
Shell :: git add and remove 
Shell :: conda install parse 
Shell :: install spicetify on windows 
Shell :: install grafana 
Shell :: kill process ubuntu 
Shell :: git change commit 
Shell :: how to install terraform macos 
Shell :: bash if number equals 
Shell :: leap year program in shell 
Shell :: download google drive file ubuntu 
Shell :: creating copy of a branch 
Shell :: push new branch to remote 
ADD CONTENT
Topic
Content
Source link
Name
6+7 =