从 UNIX 中的文本文件中提取特定文本

发布于 2024-12-08 23:24:40 字数 536 浏览 0 评论 0原文

我在 UNIX shell 脚本编写方面遇到一些问题,特别是文件读取方面。我希望最终产品是让脚本将文本文件作为命令行参数,然后提取某些部分以在各种操作中使用。文本文件将如下所示:

ABC12345:John Smith:78
DEF12345:Jane Doe:80
GHI12345:Bob Johnson:91

并且它将像其他几行一样继续。现在,我到目前为止所做的提取最后一个冒号后面的数字的代码片段如下:

case $1 in

   m)cat $2 | while read -r file; do
   #gets the numbers from 0 to 100
   current=grep [0-100]

case 语句只是因为最终用户将能够以不同的方式运行程序。然而,代码段的主要思想是获取文本文件中行末尾的 2 位数字并将其存储在当前变量中。

其余的操作确实围绕这个想法,但是,我不完全确定如何提取中间的名称。

无论如何,任何帮助都会很棒!请记住,我对此很陌生。

I'm having some problems with UNIX shell scripting, specifically file reading. What I would like the end product to be is for the script to take a text file as a command line argument and then extract certain parts to use in various operations. The text file would look like this:

ABC12345:John Smith:78
DEF12345:Jane Doe:80
GHI12345:Bob Johnson:91

and it would continue like that with several other lines. Now what I have done so far to extract the number after the last colon is here in this code snippet:

case $1 in

   m)cat $2 | while read -r file; do
   #gets the numbers from 0 to 100
   current=grep [0-100]

The case statement is just because in the end the user will be able to run the program different ways. The main idea in the code segment however is to take the 2-digit number at the end of the line in the text file and store it in the current variable.

The rest of the operations really revolve around this idea, however, I'm not entirely sure how to extract the name in the middle.

Anyway, any help would be great! Just please keep in mind I am very new to this.

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

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

发布评论

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

评论(4

她如夕阳 2024-12-15 23:24:40

正如 Frankc 所建议的,awk 或 cut 效果很好。您还可以摆弄 IFS 和(假设是 Bash)数组:

_old_ifs="$IFS"
IFS=":"
ID_NAME_GRADE=( $LINE )
IFS="$_old_ifs"

echo "Hello ${ID_NAME_GRADE[1]}, your grade is ${ID_NAME_GRADE[2]}"

As frankc suggests, awk or cut would work well. You could also fiddle with IFS and (assuming Bash) arrays:

_old_ifs="$IFS"
IFS=":"
ID_NAME_GRADE=( $LINE )
IFS="$_old_ifs"

echo "Hello ${ID_NAME_GRADE[1]}, your grade is ${ID_NAME_GRADE[2]}"
匿名。 2024-12-15 23:24:40

试试这个:

$ while IFS=: read a b c; do echo $c; done < input.txt

这将回显每行的第三个字段。修改以满足您的需要。

Try this:

$ while IFS=: read a b c; do echo $c; done < input.txt

That will echo the third field of each line. Modify to suit your needs.

从来不烧饼 2024-12-15 23:24:40

有很多方法可以提取您的案例中的姓名和分数。参见示例:

kent$  cat t
ABC12345:John Smith:78
DEF12345:Jane Doe:80
GHI12345:Bob Johnson:91

#using awk 
kent$  awk -F: '{print "name="$2,", score="$3}' t                                         
name=John Smith , score=78
name=Jane Doe , score=80
name=Bob Johnson , score=91

#using cat
kent$  sed -r 's/[^:]*?:([^:]*):([0-9]*)$/name=\1, score=\2/g' t
name=John Smith, score=78
name=Jane Doe, score=80
name=Bob Johnson, score=91

#or just catch it directly with grep
kent$  grep -Po  "(?<=:)[^:]*(?=:)|(?<=:)\d+$" t
John Smith
78
Jane Doe
80
Bob Johnson
91

cut 也可以做到。

there are many ways to extract name and score in your case. see example:

kent$  cat t
ABC12345:John Smith:78
DEF12345:Jane Doe:80
GHI12345:Bob Johnson:91

#using awk 
kent$  awk -F: '{print "name="$2,", score="$3}' t                                         
name=John Smith , score=78
name=Jane Doe , score=80
name=Bob Johnson , score=91

#using cat
kent$  sed -r 's/[^:]*?:([^:]*):([0-9]*)$/name=\1, score=\2/g' t
name=John Smith, score=78
name=Jane Doe, score=80
name=Bob Johnson, score=91

#or just catch it directly with grep
kent$  grep -Po  "(?<=:)[^:]*(?=:)|(?<=:)\d+$" t
John Smith
78
Jane Doe
80
Bob Johnson
91

cut can do it as well.

╰つ倒转 2024-12-15 23:24:40

AWk -F: '{print $NF}' 文件名

AWk -F: '{print $NF}' file_name

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文