V
V
Vladimir Titov2021-02-12 18:55:54
linux
Vladimir Titov, 2021-02-12 18:55:54

Linux has a locale.gen file. and strings like en_US.UTF-8 UTF-8. How can I split a string into 2 variables before a space and after?

The arch linux distribution has a locale.gen file. It contains lines of the form
ru_RU.UTF-8 UTF-8
en_US.UTF-8 UTF-8
zh_SG GB2312 The
question is to divide a string of this type into 2 variables, for example, zh_SG in one and GB2312 in another.
thanks for the help

Answer the question

In order to leave comments, you need to log in

2 answer(s)
X
xotkot, 2021-02-12
@uranus71

Option 1
read the file line by line in a loop (while) read (read), and already in the loop we convert each line into variables like this:

$ LINE="zh_SG GB2312"
$ eval $(echo "$LINE" | awk '{print "var1="$1";var2="$2}')
we get:
$ echo "$var1 $var2"
zh_SG GB2312

Option 2 The
other option is to simply cast it into an array.
for example there is a file lines.txt containing:
ru_RU.UTF-8 UTF-8
en_US.UTF-8 UTF-8
zh_SG GB2312

we convert it to an array:
$ lines=(`cat lines.txt`)
as a result, we get that the paired elements of the array will contain values ​​from the first column, and unpaired elements from the second, for example, for the first row this is:
$ echo ${lines[0]}
ru_RU.UTF-8
$ echo ${lines[1]}
UTF-8

S
Saboteur, 2021-02-12
@saboteur_kiev

via read

$ LINE="zh_SG GB2312"
$ IFS=' '; read var1 var2<<<$LINE
$ echo $var1
zh_SG
$ echo $var2
GB2312

via variable expansion
$ LINE="zh_SG GB2312"
$ echo ${LINE% *}
zh_SG
$ echo ${LINE#* }
GB2312

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question