如何在 bash 中获取一个字符串并将其拆分为 2 个变量?

发布于 2024-12-21 19:08:52 字数 111 浏览 0 评论 0原文

我有一个从格式为“名字姓氏”的文件中读取的字符串。我想拆分该字符串并将其放入两个单独的变量 $first$last 中。做到这一点最简单的方法是什么?

I have a string that is read in from a file in the format "firstname lastname". I want to split that string and put it into two separate variables $first and $last. What is the easiest way to do this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

永不分离 2024-12-28 19:08:52

read 可以自行进行分割,例如

read  first last < name.txt

echo "$first"
echo "$last"

read can do the splitting itself, e.g.

read  first last < name.txt

echo "$first"
echo "$last"
请你别敷衍 2024-12-28 19:08:52

扩展 fgm 的答案,只要您有一个包含标记的字符串,由不属于任何标记的单个字符分隔,并且以换行符结尾,您可以使用内部字段分隔符 (IFS) 和 read 来拆分它。一些例子:

echo 'John Doe' > name.txt
IFS=' ' read first last < name.txt
echo "$first"
John
echo "$last"
Doe

echo '+12 (0)12-345-678' > number.txt
IFS=' -()' read -a numbers < number.txt
for num in "${numbers[@]}"
do
    echo $num
done
+12
0
12
345
678

一个典型的错误是认为 read << file 相当于 cat file |读取回显内容| read,但情况并非如此:管道中的读取命令在单独的子 shell 中运行,因此一旦 read 完成,值就会丢失。要解决此问题,您可以使用同一子 shell 中的变量执行所有操作:

echo 'John Doe' | { read first last; echo $first; echo $last; }
John
Doe

或者如果文本存储在变量中,则可以重定向它:

name='John Doe'
read first last <<< "$name"
echo $first
John
echo $last
Doe

Expanding on fgm's answer, whenever you have a string containing tokens separated by single characters which are not part of any of the tokens, and terminated by a newline character, you can use the internal field separator (IFS) and read to split it. Some examples:

echo 'John Doe' > name.txt
IFS=' ' read first last < name.txt
echo "$first"
John
echo "$last"
Doe

echo '+12 (0)12-345-678' > number.txt
IFS=' -()' read -a numbers < number.txt
for num in "${numbers[@]}"
do
    echo $num
done
+12
0
12
345
678

A typical mistake is to think that read < file is equivalent to cat file | read or echo contents | read, but this is not the case: The read command in a pipe is run in a separate subshell, so the values are lost once read completes. To fix this, you can either do all the operations with the variables in the same subshell:

echo 'John Doe' | { read first last; echo $first; echo $last; }
John
Doe

or if the text is stored in a variable, you can redirect it:

name='John Doe'
read first last <<< "$name"
echo $first
John
echo $last
Doe
偷得浮生 2024-12-28 19:08:52

cut 可以将字符串分割成由所选字符分隔的部分:

first=$(echo $str | cut -d " " -f 1)
last=$(echo $str | cut -d " " -f 2)

这可能不是最优雅的方式,但它确实很简单。

cut can split strings into parts separated by a chosen character :

first=$(echo $str | cut -d " " -f 1)
last=$(echo $str | cut -d " " -f 2)

It is probably not the most elegant way but it is certainly simple.

柏拉图鍀咏恒 2024-12-28 19:08:52
$first=`echo $line | sed -e 's/([^ ])+([^ ])+/\1/'`
$last=`echo $line | sed -e 's/([^ ])+([^ ])+/\2/'`
$first=`echo $line | sed -e 's/([^ ])+([^ ])+/\1/'`
$last=`echo $line | sed -e 's/([^ ])+([^ ])+/\2/'`
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文