简体   繁体   English

每当我找到相同的String时,如何浏览文件并设置数组? 我正在使用bash代码进行编程

[英]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.

相关问题 我如何打印数组并将字符串添加到Bash中的每个项目 - How can I print an array and add a string to each item in Bash 如何知道 c 编程二维字符串数组中的 ascii 码? - How can I know the ascii code in c programming two dimensional string array? 如何返回整个mysql结果集,并且一次将每一行设置为数组? - How can I return a whole mysql result set, and one at a time set each row as an array? 在C编程中,如何从文本文件中读取数据数组? - How can I read an array of data from a text file in my code in C programming? C 中的结构数组,我要遍历程序中的每个索引吗? 我可以让它动态吗? - Array of structs in C, am I going through each index in my program? Can I make it Dynamic? 如何遍历一个jQuery数组,然后一次输出一项(从索引0开始)? - How can I loop through a jquery array and then output each item (starting at index 0) one at a time? 有没有办法将字符串数组转换为集合? 我正在尝试使用在集合 class 中找到的 symmetricDifference function 但它不起作用 - Is there a way to convert a string array to a set? I am trying to use the symmetricDifference function found in the set class but it will not work 如何将字符串中的每个单词转换为数组? - How can I convert each of the words in a string to each be an array? 我可以使用bash将数组追加到字符串中吗? - Can i append array into string using bash? 知道如何在批处理文件中循环遍历数组,根据每个值获取信息吗? - Any idea how I can loop through an array, in a batch file, getting info based on each value?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM