且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何从“sed =”中检索单个输出命令/数组

更新时间:2023-02-14 09:07:24

(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