如何从“sed =”中检索单个输出命令/数组 [英] How to retrieve individual output from "sed = " command / array

查看:126
本文介绍了如何从“sed =”中检索单个输出命令/数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这不是我上一篇文章的愚蠢行为,我现在正在寻求帮助来解决具体问题。





我使用带有选项=的sed来检索指定的第一次出现的模式的文件行号。

文档说明sed option =OUTPUTs找到模式匹配的行号。

我使用命令替换将结果收集到变量index。



问题是双重的 - 我得到的似乎是是一个在文件中找到的匹配数组 - 而不仅仅是第一次出现匹配。 (实际上不是问题,我可以稍后处理)



然而,真正的问题是我无法弄清楚如何从这个数组中检索FIRST结果。



这是我正在处理的基本bash脚本



 #retrieve第一个匹配的行号
index = $(sed -n/ $ choice / =$ PWD $ DEBUG_DIR $ DEBUG_MENU)
#delete匹配行加上文件
的下一行sed -i/ $ index [1],(($ index [1] ++))/$ PWD $ DEBUG_DIR $ DEBUG_MENU





我尝试了什么:



数组似乎是\ n分隔值,正如Linux中预期的那样。

我尝试的所有选项总是返回整个数组,而不仅仅是选择索引。



这是我用过的脚本的一部分。





78 #delete动态命令和descrition

79 echo $ {index [1]}

80 echo {$ index [0]}

81 echo$ index [1]

82 echo$((index [1] ++))

解决方案

(sed -n/


choice / =


PWD

This is not a dupe of my previous post, I am asking for help to resolve specific issue now.


I am using "sed" with an option "=" to retrieve file line number of first occurrence of pattern specified.
The documentation states that "sed option = " OUTPUTs line number(s) where the match of pattern is found.
I am "collecting" the results to variable "index" using command substitution.

The issue is twofold - I get what appears to be an array of matches found in file - not just first occurrence of match. (not actually a problem, I can deal with that later)

However, the real problem is I cannot figure out how to retrieve the FIRST result form this array.

Here is the basic bash script I am working on

#retrieve first matching line number
index=$(sed -n "/$choice/=" "$PWD$DEBUG_DIR$DEBUG_MENU")
#delete matching line plus next line from file 
sed -i "/$index[1], (( $index[1]++))/" "$PWD$DEBUG_DIR$DEBUG_MENU"



What I have tried:

The "array" appears to be \n separated values , as expected in Linux.
All of the options I tried always returned whole array, not just selected index.

Here is part of the script I used.


78 #delete dynamic command and descrition
79 echo ${index[1]}
80 echo {$index[0]}
81 echo "$index[1]"
82 echo "$((index[1]++))"

解决方案

(sed -n "/


choice/=" "


PWD


这篇关于如何从“sed =”中检索单个输出命令/数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆