user3490755
user3490755

Reputation: 995

Parse info from bash

I'm trying to parse out certain information from a bash script on Ubuntu

I'm having a bash script execute every x seconds which does write:

forever list

this response from that command looks like this:

info:    Forever processes running
data:        uid  command         script                            forever pid   logfile                 uptime        
data:    [0] _1b2 /usr/bin/nodejs /home/ubuntu/node/server.js 28968   28970 /root/.forever/_1b2.log 0:0:17:17.233

I want to parse out the location of the logfile /root/.forever/_1b2.log

Any ideas how to accomplish this with bash?

Upvotes: 1

Views: 111

Answers (4)

Sylvain Leroux
Sylvain Leroux

Reputation: 51990

Two of the many awk variations to solve this issue:

# most basic
command | awk 'NR==3{ print $8 }' data


# a little bit more robust:
command | awk '$1=="data:" && $2=="[0]" { print $8 }' data
#                             ^^^^^^^^^
#             here I filter on the "[0]" text, but depending your needs
#             you might want to use $3=="_1b2" or $4=="/usr/bin/nodejs"

Upvotes: 1

Stefano Sanfilippo
Stefano Sanfilippo

Reputation: 33046

Assuming the logfile is in /root/.forever directory, you can use grep this regex:

forever list |  grep -o "/root/\.forever/.*\.log"

Upvotes: 0

Aaron
Aaron

Reputation: 57748

You could grep the line(s) you're looking for and pipe the output to awk:

forever list | grep "\.log" | awk '{print $4}'

This will find all processes with a .log file, and print the log file's location (4th column).

Upvotes: 0

Avinash Raj
Avinash Raj

Reputation: 174696

You could try the below GNU and basic sed commands,

$ command | sed -nr 's/^.* (\S+) [0-9]+:[0-9]+\S+$/\1/p' 
/root/.forever/_1b2.log
$ command | sed -n 's/^.* \(\S\+\) [0-9]\+:[0-9]\+\S\+$/\1/p' 
/root/.forever/_1b2.log

It prints only the non-space characters which is followed by a space + one or more digits + : symbol + one or more digits.

Upvotes: 0

Related Questions