0

I have several files that contains IDs that I need to extract and store into an array, problem is that all the IDs are on the same line and all follow the same patterns, also each file has a random amount of IDs that I need to extract. For example, one file might have 1 id and another might have 4 ids. So I have no idea how to get the sed command working. I'm not even sure if what I'm attempting to do is possible considering the random amount of IDs

The line that the IDs are stored on follow this structure

somerandomstuf.wesbsite.ac.uk/people/js9g09'>somerandomstuf.wesbsite.ac.uk/people/dd13'>....somerandomstuf.wesbsite.ac.uk/people/pq919'>......

IDs being js9g09, dd13 and pq919

I have following already in place (I'm just trying to get my code working for one file atm)

for file in $FOLDER/CLA.html
do
  declare -a lec_id_array
  lec_id_array=$(grep "uk\/people" $file | sed )
  echo "$lec_id_array"
done

But I'm trying to end up with the following:

lec_id_array=$(js9g09 dd13 pq919)

Again I don't know if this is possible or if I should approach this in another way

0

I'm leery of suggesting that you persist in using grep (and/or sed) for parsing HTML, however if that's your choice then I'd suggest something like:

mapfile -t lec_id_array < <(grep -oP 'people\/\K\w+' file.html)

mapfile (aka readarray) is a bash shell builtin (although also available in zsh) - I'm assuming that's acceptable since you tagged the question bash.

2
  • This is the work I've been given and how it needs to be completed unfortunately. I don't understand how your line of code is working... At this point I'm just trying to figure out a way to get all the IDs extracted as they're on the same line. Not worrying about the Array part. – DamLk Oct 2 '20 at 19:39
  • @DamLk well that sounds like a classic XY problem - it's easy (with grep -o) to output multiple matches, one per line, and it's easy (with mapfile) to read those lines into an array. IMHO you shouldn't get hung up on extracting the matches to a single line as being a necessary intermediate step. – steeldriver Oct 3 '20 at 0:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.