Tag Archives: split

Splitting Strings in bash

Say you’ve got a pipe ‘|’ separated string; to get each element of it you can use awk. myString=”a|b|c|d” firstElem=$(echo “$myString” | awk ‘{split($0,temp,”|”); print temp[1]}’) firstElem would then contain “a”. Or by using tr: myString=”a|b|c|d” myArray=( $(echo $myString | tr “|” ” “) ) echo ${myArray[0]} # prints a

Posted in Bash Tips, Linux | Also tagged , , , | Leave a comment