205

I have a string:

one_two_three_four_five

I need to save in a variable A value two and in variable B value fourfrom the above string

I am using ksh.

9 Answers 9

299

Use cut with _ as the field delimiter and get desired fields:

A="$(cut -d'_' -f2 <<<'one_two_three_four_five')"
B="$(cut -d'_' -f4 <<<'one_two_three_four_five')"

You can also use echo and pipe instead of Here string:

A="$(echo 'one_two_three_four_five' | cut -d'_' -f2)"
B="$(echo 'one_two_three_four_five' | cut -d'_' -f4)"

Example:

$ s='one_two_three_four_five'

$ A="$(cut -d'_' -f2 <<<"$s")"
$ echo "$A"
two

$ B="$(cut -d'_' -f4 <<<"$s")"
$ echo "$B"
four

Beware that if $s contains newline characters, that will return a multiline string that contains the 2nd/4th field in each line of $s, not the 2nd/4th field in $s.

5
  • Is there any alternative? I am using ksh (not bsh) and it returns ksh: syntax error: `<' unexpected
    – Alex
    Sep 25, 2016 at 23:14
  • @Alex Check my edits.
    – heemayl
    Sep 25, 2016 at 23:24
  • Nice answer, I have a little question: what happen if your variable "$s" is a path folder. When I try to cut a path folder I do like following: ` $FILE=my_user/my_folder/[file]*` $ echo $FILE my_user/my_folder/file.csv $ A="$(cut -d'/' -f2 <<<"$FILE")" $ echo $A [file]* Do you know what's happening here? Dec 5, 2018 at 11:55
  • 2
    And if you just want the last field, using only shell builtins - without needing to specify its position, or when you don't know the number of fields: echo "${s##*_}"
    – Amit Naidu
    May 28, 2019 at 21:45
  • Note that since it involves forking at least one process, executing a separate command, passing the data between processes several times via pipes and/or temporary files, it's a lot less efficient than solutions that use the built-in splitting operators of your shell. Jun 10, 2020 at 6:53
56

Wanted to see an awk answer, so here's one:

A=$(awk -F_ '{print $2}' <<< 'one_two_three_four_five')
B=$(awk -F_ '{print $4}' <<< 'one_two_three_four_five')  

Try it online!

1
  • 7
    And if you want the last piece - without needing to specify its position or when you don't know the number of fields: awk -F_ '{print $NF}' <<< 'one_two_3_4_five'
    – Amit Naidu
    May 28, 2019 at 21:38
43

Using only POSIX sh constructs, you can use parameter substitution constructs to parse one delimiter at a time. Note that this code assumes that there is the requisite number of fields, otherwise the last field is repeated.

string='one_two_three_four_five'
remainder="$string"
first="${remainder%%_*}"; remainder="${remainder#*_}"
second="${remainder%%_*}"; remainder="${remainder#*_}"
third="${remainder%%_*}"; remainder="${remainder#*_}"
fourth="${remainder%%_*}"; remainder="${remainder#*_}"

Alternatively, you can use an unquoted parameter substitution with wildcard expansion disabled and IFS set to the delimiter character (this only works if the delimiter is a single non-whitespace character or if any whitespace sequence is a delimiter).

string='one_two_three_four_five'
set -f; IFS='_'
set -- $string
second=$2; fourth=$4
set +f; unset IFS

This clobbers the positional parameters. If you do this in a function, only the function's positional parameters are affected.

Yet another approach for strings that don't contain newline characters is to use the read builtin.

IFS=_ read -r first second third fourth trail <<'EOF'
one_two_three_four_five
EOF
5
  • The use of unset IFS doesn't return IFS to default. If after that someone does OldIFS="$IFS" will have a null value inside OldIFS. Also, it is assuming that the previous value of IFS is the default, which is very possible (and useful) to not be. The only correct solution is to store in old="$IFS" and later restore with IFS="$old". Or ... use a sub-shell (...). Or, better yet, read my answer.
    – user232326
    Sep 26, 2016 at 16:09
  • 1
    @sorontar unset IFS doesn't restore IFS to the default value, but it returns field splitting to the default effect. Yes, it's a limitation, but usually an acceptable one in practice. The problem with a subshell is that we need to get data out of it. I do show a solution that doesn't change the state at the end, with read. (It works in POSIX shells, but IIRC not in the Bourne shell because it would run the read in a subshell due to the here-document.) Using <<< as in you answer is a variant that works only in ksh/bash/zsh. Sep 26, 2016 at 16:23
  • I don't see a problem even with att or heirloom shell about a subshell. All shells tested (including the old bourne) provide the correct value in the main shell.
    – user232326
    Sep 26, 2016 at 19:16
  • What happen if my path is something like user/my_folder/[this_is_my_file]*? What I obtain when I follow these steps is [this_is_my_file]* Dec 5, 2018 at 12:20
  • @HenryNavarro This output doesn't correspond to any of the code snippets in my answer. None of them do anything special on /. Dec 5, 2018 at 17:11
15

Here string

The simplest way (for shells with <<<) is:

IFS='_' read -r a second a fourth a <<<"$string"

Using a temporary variable $a instead of $_ because one shell complains.

In a full script:

string='one_two_three_four_five'
IFS='_' read -r a second a fourth a <<<"$string"
echo "$second $fourth"
  • No IFS changing
  • No issues with set -f (Pathname expansion)
  • No changes to the positional parameters ($@).

Heredoc
For a solution portable to all shells (yes, all POSIX included) without changing IFS or set -f, use the (a bit more complex) heredoc equivalent:

string='one_two_three_four_five'

IFS='_' read -r a second a fourth a <<_EOF_
$string
_EOF_

echo "$second $fourth"

Understand that this solutions (both the here-doc and the use of <<<) will remove all trailing newlines.

And that this is designed to a "one liner" variable content.

Solutions for multi-liners are possible but need more complex constructs.


Bash 4.4+
A very simple solution is possible in bash version 4.4

readarray -d _ -t arr <<<"$string"

echo "array ${arr[1]} ${arr[3]}"   # array numbers are zero based.

beware a newline character is added at the end of the last element and an empty $string is split into one element containing a newline character.

readarray -t -d _ arr < <(printf %s "$string")

Would create an empty array for an empty $string, but beware that a trailing empty element like in string=foo_ would not result in an empty trailing element.

readarray -t -d _ arr < <(printf %s_ "$string")

Would preserve all elements and split an empty string into one empty element.

readarray -t -d _ arr < <(printf %s "${string+${string}_}")

Would split an empty string into one empty element, but would give an empty list if $string was unset.

There is no equivalent for POSIX shells, as many POSIX shells do not have arrays.

Arrays
For shells that have arrays may be as simple as (tested working in attsh, lksh, mksh, ksh, and bash, but not zsh):

set -f; IFS=_; arr=($string)

But with a lot of additional plumbing to keep and reset variables and options:

string='one_* *_three_four_five'
    
case $- in
    *f*) noglobset=true; ;;
    *) noglobset=false;;
esac
    
oldIFS="$IFS"

set -f; IFS=_; arr=($string)
    
if $noglobset; then set -f; else set +f; fi

IFS=$oldIFS
    
echo "two=${arr[1]} four=${arr[3]}"

In zsh, arrays start in 1, and no split+glob is performed by default upon parameter expansions. So some changes need to be done to get this working in zsh:

IFS=_; arr=( $=string )
echo "two=${arr[2]} four=${arr[4]}"

Where $=string requests word splitting explicitly (glogging is still not done so doesn't need to be disabled globally). Also note that while foo_ would be split into foo only in ksh/bash/yash, it's split into foo and the empty string in zsh.

0
8

With zsh you could split the string (on _) into an array:

non_empty_elements=(${(s:_:)string})
all_elements=("${(@s:_:)string}")

and then access each/any element via array index:

print -r -- ${all_elements[4]}

Keep in mind that in zsh (like most other shells, but unlike ksh/bash) array indices start at 1.

Or directly in one expansion:

print -r -- "${${(@s:_:)string}[4]}"

Or using an anonymous function for the elements to be available in its $1, $2...:

(){print -r -- $4} "${(@s:_:)string}"
0
5

Is a python solution allowed?

# python3 -c "import sys; print(sys.argv[1].split('_')[1])" one_two_three_four_five
two

# python3 -c "import sys; print(sys.argv[1].split('_')[3])" one_two_three_four_five
four
0
2

With due respect to everyone who have posted excellent answers, I wonder if we are over-engineering this problem. Three simple lines to just answer the question asked without generalizing:

str="one_two_three_four_five" <-- create a string

A=$(echo $str | awk -F_ '{print $2}') <-- tell awk to use _ as the delimiter and assign the second field to A

B=$(echo $str | awk -F_ '{print $4}') <-- tell awk to use _ as the delimiter and assign the fourth field to B

You can then use the variables as usual. Here is an example:

$ echo "The value of A is: $A; the value of B is: $B"
The value of A is: two; the value of B is: four
$ 
1
  • That answer has already been given (several times). Try with str='*' or str='-n _-o _-p ' or str=$'a_b\n_c_d_e'. Jun 10, 2020 at 6:35
2

Another awk example; simpler to understand.

A=$(echo one_two_three_four_five | awk -F_ '{print $1}')
B=$(echo one_two_three_four_five | awk -F_ '{print $2}')  
C=$(echo one_two_three_four_five | awk -F_ '{print $3}')  
... and so on...  

Can be used with variables also.

Suppose:

this_str="one_two_three_four_five"  

Then the following works:

A=$(printf '%s\n' "${this_str}" | awk -F_ '{print $1}')
B=$(printf '%s\n' "${this_str}" | awk -F_ '{print $2}')
C=$(printf '%s\n' "${this_str}" | awk -F_ '{print $3}')
... and so on...  

That assumes ${this_str} doesn't contain newline characters, or it would return the first _ in each line of the contents of the variable instead of the first field in the contents of the variable.

1
  • Yea, this is the most readable solution, although keep in mind this will run pretty slow given there are many subshell invocations used.
    – Yuri
    May 14, 2021 at 8:19
0

Using Raku (formerly known as Perl_6)

A=$(raku -e  'print $*IN.split("_")[1];' <<< 'one_two_three_four_five')
B=$(raku -e  'print $*IN.split("_")[3];' <<< 'one_two_three_four_five')

This answer complements the awk answer by @Paul_Evans. You can place print at the right end of the method chain, if you find that more readable. Also, if you have an issue with quoting, then the .split("_") call can be replaced by .split(q[_]).

Putting these two options together:

A=$(raku -e  '$*IN.split(q[_])[1].print;' <<< 'one_two_three_four_five')
B=$(raku -e  '$*IN.split(q[_])[3].print;' <<< 'one_two_three_four_five')

Finally, a word about indexing. You can take the first element after splitting with head, or the first 2 elements with head(2). If you want to take elements from the right end, use tail in a similar manner. The way to numerically index from the right end in Raku is to use the * "whatever-star" idiom. So the last (zero-indexed) element is [*-1], the second-to-last is [*-2], etc.

~$ raku -e  'print $*IN.split(q[_])[*-4];' <<< 'one_two_three_four_five'
two
~$ raku -e  'print $*IN.split(q[_])[*-2];' <<< 'one_two_three_four_five'
four

https://raku.org

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .