从多个记录的文本文件中提取特定文本的方法是什么?

huangapple go评论49阅读模式
英文:

How to extract specific text from a multiple records text file?

问题

我有一个文本文件,其结构如下:

记录 1
属性1: 一些数字1
属性2: 一些数字2
属性3: 一些数字3
...
属性20: 一些数字

记录 2
属性1: 一些其他数字1
属性2: 一些其他数字2
属性3: 一些其他数字3
...
属性20: 一些其他数字
...
...
...
记录 350
属性1: 更多数字1
属性2: 更多数字2
属性3: 更多数字3
...
属性20: 一些其他数字

(这里的 ... 表示更多属性/记录)

使用一个 Bash 脚本,我想要输入记录号,然后提取特定属性值到一个 .csv 文件。例如,使用 2(对于记录 #2)和 property2,结果为 some_other_number2

记录, 属性2
2, some_other_number2

我已经尝试逐行读取文件,然后不断检查是否找到给定的字符串(例如 Record 2),然后查找包含 property2 的行,但没有成功。

英文:

I have a text file with the following structure:

Record 1
property1: some_number1
property2: some_number2
property3: some_number3
...
property20: some_number

Record 2
property1: some_other_number1
property2: some_other_number2
property3: some_other_number3
...
property20: some_other_number
...
...
...
Record 350
property1: more_numbers1
property2: more_numbers2
property3: more_numbers3
...
property20: some_other_number

(here ... represents more properties/records)

Using a bash script, I want to input the Record number, and then extract some specific property values to a .csv file. For example, using 2 (for Record #2) and the propery2, results in some_other_number2:

Record, property2
2,some_other_number2 

I already tried read the file line by line, and keep checking if a given string (ex. Record 2) is found and then look for a line with property2, unsuccessfully.

答案1

得分: 1

如果您的txt文件始终以这种方式格式化,您甚至可能不需要awk。
您可以简单地使用grep查找您想要的选项编号,就在您想要的记录编号后面。

例如,此函数将在cvs文件中写入您想要的内容:

function extract_property {
nrec=$1
nprop=$2

prop=$(echo $(cat test) | grep -Po "Record $nrec .*?property$nprop:[\s\t]*\K[^\s\t]*")

cat > extracted.csv <<EOF
Record, property$nprop
$nprop, $prop
EOF
}

例如:

extract_property 2 2

写入文件:

Record, property2
2, some_other_number2
英文:

If your txt file is formatted always in that way, you might not even need awk.
You can simply grep for the option number you want, right after the record number you want.

For example this function will write what you want in a cvs file:

function extract_property {
nrec=$1
nprop=$2

prop=$(echo $(cat test) | grep -Po &quot;Record $nrec .*?property$nprop:[\s\t]*\K[^\s\t]*&quot;)

cat &gt; extracted.csv &lt;&lt;EOF
Record, property$nprop
$nprop, $prop
EOF
}

For example:

extract_property 2 2

writes the file

Record, property2
2, some_other_number2 

huangapple
  • 本文由 发表于 2023年2月14日 07:45:06
  • 转载请务必保留本文链接:https://go.coder-hub.com/75442207.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定