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 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

PREVIOUS NEXT
Code Example
Shell :: change dns resolver linux 
Shell :: how to combine audio tracks ffmpeg 
Shell :: how to start mongodb in linux 
Shell :: how to change permissions for only the root 
Shell :: git lfs add file 
Shell :: angular bootstrap 
Shell :: bash terminal function display all arguments 
Shell :: linux change user in terminal 
Shell :: bash remove first character from string 
Shell :: powershell set variable 
Shell :: sudo rm rf hack 
Shell :: how to add directory path to path variable ubuntu 
Shell :: git files change from commit 
Shell :: install mysql in debian 
Shell :: Error: Cannot tap homebrew/cask: invalid syntax in tap! 
Shell :: git revert to commit 
Shell :: pod install react native failed 
Shell :: are github and git same thing 
Shell :: how to go back in terminal 
Shell :: gitignore for eclipse 
Shell :: how to git pull origin master 
Shell :: valet allow phpmyadmin route 
Shell :: linux find where program is installed 
Shell :: Mount EBS volume on Linux 
Shell :: list dir by date linux 
Shell :: git remove ignored files 
Shell :: npm install xlsx 
Shell :: zip linux exclude directory 
Shell :: replace char for new line 
Shell :: vim count ocurrences 
ADD CONTENT
Topic
Content
Source link
Name
5+4 =