Click here to Skip to main content
15,887,331 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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]++))"
Posted
Updated 25-Sep-18 14:43pm
v2

1 solution

#retrieve first matching line number
index=$(sed -n "/$choice/{=;q}" "$PWD$DEBUG_DIR$DEBUG_MENU")
#delete matching line plus next line from file 

 the solution is to run the following code here 

  #remove command line
  sed -i "${index}d" "$PWD$DEBUG_DIR$DEBUG_MENU"
  # remove description line 
  sed -i "${index}d" "$PWD$DEBUG_DIR$DEBUG_MENU"
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900