gpt4 book ai didi

shell - 使用 shell 拆分数据

转载 作者:可可西里 更新时间:2023-11-01 16:29:57 26 4
gpt4 key购买 nike

我是 shell 脚本新手。我需要使用 shell 脚本获取运行和自动匹配计数之间的数据。这样它就可以作为半结构化数据进行处理。请多多指教

最佳答案

使用 sed -n '/run/,/Automatic/p' filename.txt|sed '1d;$d'|sed '$d;s///g' - 应该清理向上数据(第一行,最后两行,开头有空格)

shell 脚本 - split.sh:

#!/bin/bash
sed -n '/run/,/Automatic/p' $1|sed '1d;$d'|sed '$d;s/ //g'

如下所示为任何文件运行以在控制台和文件中获取输出:

shell> ./split.sh test.txt |tee splitted.dat
United Kingdom: 21/09/2012
Started: 08/02/2013 16:04:44
Finished: 08/02/2013 16:21:23
Time to process: 0 days 0 hours 16 mins 39 secs
Records processed: 37497
Throughput: 135124 records/hour
Time per record: 0.0266 secs

输出将存储在 splitted.dat 文件中:

shell> cat splitted.dat 
United Kingdom: 21/09/2012
Started: 08/02/2013 16:04:44
Finished: 08/02/2013 16:21:23
Time to process: 0 days 0 hours 16 mins 39 secs
Records processed: 37497
Throughput: 135124 records/hour
Time per record: 0.0266 secs
shell>

更新:

#!/bin/bash
# p - print lines with specified conditions
# !p - print lines except specified in conditions (opposite of p)
# |(pipe) - passes output of first command to the next
# $d - delete last line
# 1d - delete first line ( nd - delete nth line)
# '/run/,/Automatic/!p' - print lines except lines between 'run' to 'Automatic'
# sed '1d;s/ //g'- use output from first sed command and delete the 1st line and replace spaces with nothing

sed -n '/run/,/Automatic/!p' $1 |sed '1d;s/ //g'

输出:

Verified Correct:     32426 (86.5%)
Good Match: 2102 ( 5.6%)
Good Premise Partial: 862 ( 2.3%)
Tentative Match: 1039 ( 2.8%)
Poor Match: 4 ( 0.0%)
Multiple Matches: 7 ( 0.0%)
Partial Match: 872 ( 2.3%)
Foreign Address: 2 ( 0.0%)
Unmatched: 183 ( 0.5%)

关于shell - 使用 shell 拆分数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38623006/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com