Search
 
SCRIPT & CODE EXAMPLE
 

SHELL

split string and create array bash

my_array=($(echo $string | tr "," "
"))
Comment

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

bash split

# Basic syntax:
cut -d "delimeter" -f split_number your_file
# Where:
#	- -d specifies the delimeter to split by
#	- -f specifies which element to return after splitting
# Note, if running this in a bash script, use syntax like:
"$(cut -d ',' -f split_number <<<$variable_to_split)"

# Note, there are lots of ways of doing this, e.g. with awk:
awk -F delimiter '{print $split_number}' your_file
# Where:
#	- -F specifies the field delimiter

# Note, awk also has a split function which has this syntax:
awk '{split($column, a, "delimiter"); action_after_split }' your_file
# Where:
#	- the column is the column to be split (awk uses whitespace to determine
#		columns by default)
#	- a is an array variable which will store the contents of the split
#	- delimiter is the delimiter by which to split the column

# Example usage:
# Say you have a file with this line:
my	file	with	fields_to_split
# You can print "to" with:
awk '{split($4, a, "_"); print a[2] }' your_file
--> to
Comment

split string and create array bash

IFS=', ' read -r -a array <<< "$string"
Comment

PREVIOUS NEXT
Code Example
Shell :: push to git with token 
Shell :: push a new branch git 
Shell :: install composer by cmd 
Shell :: delete previous word vim 
Shell :: docker unused 
Shell :: ubuntu server set static ip 
Shell :: execute command remotely by ssh 
Shell :: git merge develop to feature branch 
Shell :: combine strings bash 
Shell :: github push 
Shell :: install kind kubernetes 
Shell :: command get full history terminal mac 
Shell :: linux redirect everything (stdout and stderr) to file 
Shell :: how to get homebrew 
Shell :: ubuntu all installed services 
Shell :: brave browser install on ubuntu 
Shell :: compress directory with tar and bzip2 
Shell :: linux remove lines from one file in another 
Shell :: install windows app powershell 
Shell :: how to figure out ubuntu partition 
Shell :: install bootstrap in angular 
Shell :: git clone in 
Shell :: install kivy 
Shell :: docker-compose build 
Shell :: exec into docker container 
Shell :: list apt installed programs 
Shell :: how to hide hostname mac terminal 
Shell :: check ip address linux 
Shell :: windows ssh-copy-id 
Shell :: how to login github in terminal 
ADD CONTENT
Topic
Content
Source link
Name
6+6 =