0

i'm trying to extract a specific string from a grep output

uci show minidlna

produces a large list

.
.
.
minidlna.config.enabled='1'
minidlna.config.db_dir='/mnt/sda1/usb/db'
minidlna.config.enable_tivo='1'
minidlna.config.wide_links='1'
.
.
.

so i tried to narrow down what i wanted by running

uci show minidlna | grep -oE '\bdb_dir=\S+'

this narrows the output to

db_dir='/mnt/sda1/usb/db'

what i want is to output only

/mnt/sda1/usb/db

without the quotes and without the starting "db_dir" so i can run rm /mnt/sda1/usb/db/file.db

i've used the answers found here How to extract string following a pattern with grep, regex or perl and that's as close as i got.

EDIT: after using Ed Morton's awk command i needed to pass the output to rm command.

i used:

| ( read DB; (rm $DB/files.db) .

read DB passes the output into the vairable DB.

(...) combines commands.

rm $DB/files.db deletes the the file files.db.

asamahy
  • 15
  • 4
  • `path/to/my/db` isn't present in your sample input. Make sure the posted expected output is output you can get from your posted sample input, not from some other set of data. – Ed Morton Jun 27 '19 at 18:21

4 Answers4

2

Is this what you're trying to do?

$ awk -F"'" '/db_dir/{print $2}' file
/mnt/sda1/usb/db

That will work in any awk in any shell on every UNIX box.

If that's not what you want then edit your question to clarify your requirements and post more truly representative sample input/output.

Ed Morton
  • 188,023
  • 17
  • 78
  • 185
  • yes that's exactly what i need. i next would like to execute ```rm``` on that output as in ``` rm -R /mnt/sda1/usb/db``` or with a file as in ```rm /mnt/sda1/usb/db/file.db``` – asamahy Jun 27 '19 at 18:31
  • and if it's no trouble would you dissect your answer as well so i can gain better understanding. – asamahy Jun 27 '19 at 18:48
  • Just google something like "how to save the output of a shell command in a variable". The answer looks for db_dir on a line and when found prints the 2nd `'`-separated field. – Ed Morton Jun 27 '19 at 21:27
0

Using sed with some effort to avoid single quotes:

sed -n 's/^minidlna.config.db_dir=\s*\S\(\S*\)\S\s*$/\1/p' input
perreal
  • 94,503
  • 21
  • 155
  • 181
0

Well, so you end up having a string like db_dir='/mnt/sda1/usb/db'. I would first remove the quotes by piping this to

.... |  tr -d "'"

Now you end up with a string like db_dir=/mnt/sda1/usb/db. Say you have this string stored in a variable named confstr, then

${confstr##*=}

gives you just /mnt/sda1/usb/db, since *= denotes everything from the start to the equal sign, and ## denotes removal.

user1934428
  • 19,864
  • 7
  • 42
  • 87
0

I would do this:

Once you either extracted your line about into file.txt (or pipe it into this command), split the fields using the quote character. Use printf to generate the rm command and pass this into bash to execute.

$ awk -F"'" '{printf "rm %s.db/file.db\n", $2}' file.txt  | bash
rm: /mnt/sda1/usb/db.db/file.db: No such file or directory

With your original command:

$ uci show minidlna | grep -oE '\bdb_dir=\S+' | \
       awk -F"'" '{printf "rm %s.db/file.db\n", $2}' | bash
dr-who
  • 189
  • 6