[英]How can I look through a file and set an array for each time I found the same String? I am programming with bash code
I am a beginner in bash code. 我是bash代码的初学者。 I am trying to set an array that contains different values that can be found in a file.
我试图设置一个数组,其中包含可以在文件中找到的不同值。 The values I would like to add to the array are Strings that follow specific words.
我想添加到数组中的值是遵循特定单词的字符串。 For example;
例如; Lets say the file is
可以说文件是
Subcase 201
Node 101
"Much more text here"
Subcase 202
Node 501
"Again more text here"
Subcase 301 Node 804
.......
I would like to have the following output: 我想要以下输出:
Subcase="201" "202" "301"; Node="101" "501" "804".
Can someone help me please? 有人能帮助我吗? I am not sure if I need to use a "for loop".
我不确定是否需要使用“ for循环”。 I hope I were clear enough
我希望我足够清楚
What you are looking for is the read
builtin. 您正在寻找的是内置的
read
。 You can use read
in a while
loop. 您可以在
while
循环中使用read
。 The following example reads from stdin (pipe the file to the script) but you can use read -r -u <fd> var vals
to select from a different stream if you need. 以下示例从stdin读取(将文件传递到脚本),但如果需要,可以使用
read -r -u <fd> var vals
从其他流中进行选择。
#!/bin/bash
while read -r var vals
do
eval P_${var}'+=($vals)' 2>/dev/null
done
for var in ${!P_@}
do
eval echo '"${var#P_} = ${'$var'[@]}"'
done
The assignment line ignores stderr because var
may contain invalid chars for a bash variable name. 赋值行忽略stderr,因为
var
可能包含bash变量名称无效的字符。
Assuming that neither "Subcase" or "Node" appear twice on a given line of the original file, and that they are always spelled with the same case (first letter capitalized), the following code would achieve what you want: 假设“ Subcase”或“ Node”在原始文件的给定行中均未出现两次,并且它们始终使用相同的大小写(首字母大写),则以下代码将实现您想要的:
#!/bin/bash
#assuming that a specific targeted string only appears once on a given line, and it is always written with same case (first letter capitalized). Cleaning data and storing all values on temporary files
grep -e 'Subcase' /folder/file > /tmp/subcase-holder && sed -i -e 's/^.*Subcase//' /tmp/subcase-holder && sed -i -e "s/^[ \t]*/\"/" /tmp/subcase-holder && sed -i -e 's/[[:blank:]]*$//' /tmp/subcase-holder && sed -i -e 's/\s.*$//' /tmp/subcase-holder && sed -i -e 's/$/\"/' /tmp/subcase-holder
grep -e 'Node' /folder/file > /tmp/node-holder && sed -i -e 's/^.*Node//' /tmp/node-holder && sed -i -e "s/^[ \t]*/\"/" /tmp/node-holder && sed -i -e 's/[[:blank:]]*$//' /tmp/node-holder && sed -i -e 's/\s.*$//' /tmp/node-holder && sed -i -e 's/$/\"/' /tmp/node-holder
#creating output file
cat /tmp/subcase-holder | tr '\n' ' ' > /tmp/subcase-holder2 && sed -e 's/.$/; Node\=/' /tmp/subcase-holder2 > /tmp/partial-answer && sed -i -e 's/^/Subcase\=/' /tmp/partial-answer
cat /tmp/node-holder | tr '\n' ' ' > /tmp/node-holder2 && sed -e 's/.$/\./' /tmp/node-holder2 > /tmp/partial-answer2
cat /tmp/partial-answer /tmp/partial-answer2 > /tmp/answer && echo >> /tmp/answer
exit 0
The result is stored on the file /tmp/answer. 结果存储在文件/ tmp / answer中。 By the way, the code does not use arrays, but it delivers the output you want.
顺便说一句,该代码不使用数组,但可以提供所需的输出。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.