Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In shell, split a portion of a string with dot as delimiter [duplicate]

Tags:

linux

shell

I am new to shell scripting, can you please help with below requirement, thanks.

$AU_NAME=AU_MSM3-3.7-00.01.02.03
#separate the string after last "-", with "." as delimiter
#that is, separate "00.01.02.03" and print/save as below.
major=00
minor=01
micro=02
build=03
like image 344
rodee Avatar asked Nov 11 '13 20:11

rodee


People also ask

How do I split a string on a delimiter in Bash?

In bash, a string can also be divided without using $IFS variable. The 'readarray' command with -d option is used to split the string data. The -d option is applied to define the separator character in the command like $IFS. Moreover, the bash loop is used to print the string in split form.

What is ${} in shell script?

${} Parameter Substitution/Expansion A parameter, in Bash, is an entity that is used to store values. A parameter can be referenced by a number, a name, or by a special symbol. When a parameter is referenced by a number, it is called a positional parameter.

How to split strings with delimiter in Bash shell?

This tutorial helps to split strings with delimiter with examples in bash shell programming. awk is a Linux command which works in all bash and shell distributions. and returns the exit code with the result. Input to awk is given with the pipe (|) symbol.

What if the delimiters are different from each other?

If you have a string you want to split but the delimiters are different from each other. For example, you have two delimiters : “.” and “;”. The string looks like :

How to split string into array in shell script?

You can use Internal Field Separator (IFS) variable in shell script to split string into array. When we set IFS variable then the assignment to IFS only takes place to that single command’s environment to read. It then parses the input according to the IFS variable value into an array that we can iterate over all values.

What is delimiter in JavaScript?

The location or the pattern on which it is decided to split the string is known as delimiter. But before starting it becomes imperative for us to know something on IFS (Internal Field Separator) as it will constitute the majority of the method.


1 Answers

First, note that you don't use $ when assigning to a parameter in the shell. Your first line should be just this:

AU_NAME=AU_MSM3-3.7-00.01.02.03

The $ is used to get the value of the parameter once assigned. And the bit after the $ can be an expression in curly braces with extra stuff besides just the name, allowing you to perform various operations on the value. For example, you can do something like this:

IFS=. read major minor micro build <<EOF
${AU_NAME##*-}
EOF

where the ##*- strips off everything from the beginning of the string through the last '-', leaving just "00.01.02.03", and the IFS (Internal Field Separator) parameter tells the shell where to break the string into fields.

In bash, zsh, and ksh93+, you can get that onto one line by shortening the here-document to a here-string:

IFS=. read major minor micro build <<<"${AU_NAME##*-}"

More generally, in those same shells, you can split into an arbitrarily-sized array instead of distinct variables:

IFS=. components=(${AU_NAME##*-})

(Though that syntax won't work in especially-ancient versions of ksh; in them you have to do this instead:

IFS=. set -A components ${AU_NAME##*-}

)

That gets you this equivalence (except in zsh, which by default numbers the elements 1-4 instead of 0-3):

major=${components[0]}
minor=${components[1]}
micro=${components[2]}
build=${components[3]}
like image 165
Mark Reed Avatar answered Sep 21 '22 04:09

Mark Reed