-1

Basically, I have this script that updates wifi firmware on jailbroken devices (iOS). I need to check the output of this:

/usr/libexec/wifiFirmwareLoader -f

This outputs me the file path to the correct files needed for the rest of the script, but I don't want the whole path, I only want the name of the file at the end of the path. How could I go about parsing this?

If you need an example, this is what the output looks like.

RESOLVED /usr/share/firmware/wifi/C748383-A3/hans.trx

I would only want to store hans.trx in a variable.

Inian
  • 80,270
  • 14
  • 142
  • 161
  • 1
    But what did you try to solve this problem of yours? Post your research efforts into the question even if they are trivial – Inian Dec 06 '17 at 12:12
  • 1
    StackOverflow is about helping people fix their existing programming code. Edit your Q to show your best attempt at code. Requests for tutorials, research, tools, recommendations, libraries, and code are off-topic. ***Please*** read https://stackoverflow.com/help/on-topic , https://stackoverflow.com/help/how-to-ask , https://stackoverflow.com/help/dont-ask , https://stackoverflow.com/help/mcve and take the [tour](https://stackoverflow.com/tour) before posting more Qs here. Good luck – shellter Dec 06 '17 at 14:16

1 Answers1

0

You can use regexes in grep:

echo "RESOLVED /usr/share/firmware/wifi/C748383-A3/hans.trx" | grep -Eo "[^/]+$"
  • -E uses extended regexes. You could also use -P for perl regexes
  • -o only shows the matched output
  • [^/] matches everything that is not a /, the + makes it at least 1 character and the $ requires it to be at the end of the line
  • If this matches more lines that you would need, you could use postive lookahead (?=something) or positive lookbehind something\K to make the regex more strict (will need -P). Or in some cases I like to double grep (first grep for lines containing something, then get the part from the line you want)

Your final script could look something like this:

OUTPUT=$(/usr/libexec/wifiFirmwareLoader -f)
FILENAMES=$(echo "${OUTPUT}" | grep -Eo "[^/]+$")
echo "${FILENAMES}"
Veda
  • 2,025
  • 1
  • 18
  • 34