0

I have log files which stores the output in json format (looks to be). but when i try to use jq method of reading the file its not working

command i used echo $log | jq '.username'

log file contains :

{u'username': u'tempname', u'command_log': u'logfilelocation', u'reporting': True}

I am trying to use

echo $log | jq '.command_log'

I want to read the command_log value and pass this to some other function.

Getting Error parse error: Invalid numeric literal

archana k
  • 27
  • 6
  • Why do you pass the file name (`$log`) to `jq`, instead of the content? Try `jq ... "$log"`. – Renaud Pacalet Jul 08 '23 at 05:10
  • 3
    That's not json; looks like maybe python syntax to me (maybe?). Also, never use `echo $var` -- you should (almost) always double-quote variable references, like `echo "$var"`. See ["When should I double-quote a parameter expansion?"](https://stackoverflow.com/questions/55023461/when-should-i-double-quote-a-parameter-expansion) – Gordon Davisson Jul 08 '23 at 05:31
  • Getting the error jq: error: log/0 is not defined at – archana k Jul 08 '23 at 05:33
  • With bash, herestrings are a useful way of avoiding `echo`ing a variable's contents: `command arg ... <<<"$foo"` – Shawn Jul 08 '23 at 06:09

2 Answers2

1

What you have is not JSON, but Python's native data. You can use Python to dump it:

$ python -c "log=$log; print(log['command_log'])"
Hai Vu
  • 37,849
  • 11
  • 66
  • 93
0

Perhaps a simple combination of grep and cut might do the job?

Field='username'
grep -o -e "\\<u'${Field}': u'[^']*'" INPUTFILE | cut -d "'" -f 4

Based on the following input (contents of INPUTFILE):

{u'username': u'tempname', u'command_log': u'logfilelocation', u'reporting': True}

it produces:

tempname

Tested under Debian 11 with GNU grep in POSIXLY_CORRECT mode.

Hope that helps.

Grobu
  • 599
  • 1
  • 11