xxxxxxxxxx
#!/bin/bash
# Define the string to be splitted
string="Hello,World,How,Are,You"
# Split the string using a delimiter (comma in this case)
IFS=',' read -ra parts <<< "$string"
# Print each part of the splitted string
for part in "${parts[@]}"; do
echo "$part"
done
xxxxxxxxxx
#!/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 ";" "\n")
# 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.
xxxxxxxxxx
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
xxxxxxxxxx
IN="bla@some.com;john@home.com"
arrIN=(${IN//;/ })
echo ${arrIN[1]} # Output: john@home.com
xxxxxxxxxx
# separator is space
VAR="inforge srl bergamo"
read -r ONE TWO THREE <<< "${VAR}"
echo ${ONE}
echo ${TWO}
echo ${THREE}
# separator is comma
VAR="inforge,srl,bergamo"
IFS="," read -r ONE TWO THREE <<< "${VAR}"
echo "${ONE} ${TWO} ${THREE}"
xxxxxxxxxx
text="Hello,World,Devs"
# Set comma as delimiter
IFS=','
#Read the split words into an array based on comma delimiter
read -a strarr <<< "$text"
#Print the split words
echo "index 0 : ${strarr[0]}" # Hello
echo "index 1 : ${strarr[1]}" # World
echo "index 2: ${strarr[2]}" # Devs
xxxxxxxxxx
#!/bin/bash
input_file="large_file.txt"
split -b 1M "$input_file" "split_file"