Saturday, May 4, 2024
2
rated 0 times [  2] [ 0]  / answers: 1 / hits: 15577  / 3 Years ago, tue, november 2, 2021, 7:05:21

I have the following in one of my json file file1.json :-


{
"$quer": {
"args": [{
"args": [
"select
db1.table1 as tab1,
db1.table2 as tab2,
db1.table3 as tab3
from db1.table4 as tab4"
],
"fn": "from-sql",
"ns": "op"
}],
"fn": "operators",
"ns": "op"
}
}

I want to extract the string db1.table4 from this json file and store into a variable.


I don't know much about sed and awk. Can someone help here?


More From » command-line

 Answers
6

  1. Assume that the string you want to extract stands in the same position in every file you can use head, tail and cut commands using pipes.



  2. For example:


    $ head -6 file.json | tail -1 | cut -b 121-129
    db1.table


  3. And here is an example of a script setting the output into a variable:


    #!/bin/bash
    v1=$(head -6 file.json | tail -1 | cut -b 121-130)
    echo "$v1"



The output of the script will be db1.table4 which is the value of V1 varaible.


You can read more about those commands here:



Of course you can use those commands to extract any other string from a file.


[#1266] Thursday, November 4, 2021, 3 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
earxcept

Total Points: 310
Total Questions: 115
Total Answers: 111

Location: Japan
Member since Sat, Oct 2, 2021
3 Years ago
;