0

I have read other questions regarding similar subjects, such as how to parse a text file using shell and Code for parsing a key/value in in file from shell script

These show how to parse a line or field in a line, specifically using grep:

grep MARX_BUILD builds.txt | cut -d= -f2

my problem lies in the fact I need to have each line between two lines as its own item so I can make an array.

I have a file named users.txt with a list of users that is formatted like this:

Authorized Admins:
admin1
admin2

Authorized Users:
user1
user2
user3

So far I have this:

readarray -t users < <(grep "Authorized Admins" users.txt | cut -d '/n' -f2<=

Then I can use an array like "${users[@]}" to use later on to change passwords and add/delete users depending on the list.

What do I put at the end to stop the parsing at the string "Authorized Users"?

Also, how would I change this to parse from "Authorized Users" to end of file?

  • 2
    Please add sample input and your desired output for that sample input to your question. – Cyrus Apr 18 '18 at 17:40
  • 1
    Combinations of `grep` and `cut` are usually a sign that you should be using `awk` instead: `awk -F= '/MARX_BUILD/ {print $2}' builds.txt`. – chepner Apr 18 '18 at 17:44
  • BTW, do you really want to stop at `Authorized Users:`, or do you want to stop at the next blank line? If a blank line indicates end-of-section, it's more reliable to use that as your sigil rather than to assume that sections will always be in the same order. – Charles Duffy Apr 18 '18 at 18:25
  • BTW, if you want to read output of a command into an array, you're better off with `readarray -t users < <(awk ...)`; `users=( $(...) )` is generally buggy, for reasons given in [BashPitfalls #1](http://mywiki.wooledge.org/BashPitfalls#for_i_in_.24.28ls_.2A.mp3.29). – Charles Duffy Apr 18 '18 at 18:28
  • The sections are always in the same order, but I do not quite remember if there is a blank line at the end of each section, so if I could ignore blank lines and stop at the next section, that would be the best, otherwise I can delete blank lines. – Gavin Morton Apr 18 '18 at 18:30

0 Answers0