How can I use the result of file parsing as an argument to a bash command?

I use Linux and want to use a program named Trelby to open directly the latest saved file.

The latest saved file is stored in the seventh line of the file /home/me/.trelby/state, which looks like this:

PositionX:0
PositionY:0
Width:1366
Height:736
ViewMode:1
Files:5
Files/1:/home/me/writing/text-1.trelby
Files/2:/home/me/writing/text-2.trelby
Files/3:/home/me/writing/text-3.trelby
Files/4:/home/me/writing/text-4.trelby
Files/5:/home/me/writing/text-5.trelby

Hence the command I want to execute should be:

trelby /home/me/writing/text-1.trelby

What command could I use to read the 'state' file and have the portion /home/me/writing/text-1.trelbyappended to the Trelby command?


Solution 1:

In general, avoid relying too much on something as incidental as the line number in which you find the information. In its next version, maybe trelby will add a line with some new saved setting and your command will be broken.

The syntax of the file you give is rather transparent that the path of the latest file is prepended by "Files/1": use this rather than the line number.

In bash, you can use:

trelby $(awk -F: '{if ($1=="Files/1") {print $2}}' ${HOME}/.trelby/state)

Solution 2:

There might be a more elegant or effective solution, but this worked:

trelby $(head -7 /home/me/.trelby/state | tail -1 | cut -c9-100)