0

I have two files, what I want is to do is to use file a and search number in file b and print its whole parentheses including the searched line. I'm not sure, if it's what it's called.

File a:

300
302
303

File b:

    [300] = {
        name = "John",
        age = "12",
        address = {""},
        job = "Marketing",
        job a = "some job",
        job b = {"some job"},
        car = 0,
        salary = 0
    },
    [301] = {
        name = "John",
        age = "12",
        address = {""},
        job = "Marketing",
        job a = "some job",
        job b = {"some job"},
        car = 0,
        salary = 0
    },
    [302] = {
        name = "John",
        age = "12",
        address = {""},
        job = "Marketing",
        job a = "some job",
        job b = {"some job"},
        car = 0,
        salary = 0
    },
    [303] = {
        name = "John",
        age = "12",
        address = {
        "Person street address"
        },
        job = "Marketing",
        job a = "some job",
        job b = {
        "His job description"
        },
        car = 0,
        salary = 0
    },

Expected output:

    [300] = {
        name = "John",
        age = "12",
        address = {""},
        job = "Marketing",
        job a = "some job",
        job b = {"some job"},
        car = 0,
        salary = 0
    },
    [302] = {
        name = "John",
        age = "12",
        address = {""},
        job = "Marketing",
        job a = "some job",
        job b = {"some job"},
        car = 0,
        salary = 0
    },
    [303] = {
        name = "John",
        age = "12",
        address = {
        "Person street address"
        },
        job = "Marketing",
        job a = "some job",
        job b = {
        "His job description"
        },
        car = 0,
        salary = 0
    },

Tried using awk, but can't get it working.

awk 'NR==FNR{a[$1]=1;next} $1 in a && /\[$1\]/,/^\t\},/ {print}' a b > c

Thank you for your help..

  • I don't think you can use `$1` directly in a pattern. `/\[$1\]/` should probably look like `$0 ~ "\[" $1 "\]"`. – muru Mar 17 '20 at 09:39
  • 1
    It would appear that you are working with JSON data. Hence, a dedicated parser like `jq` seems most suitable for the job (if you have one available or can install one). – AdminBee Mar 17 '20 at 09:52

3 Answers3

3

Assuming you don;'t have access to a JSON parser and your records really are as simple and regular as you show, this will work using any awk in any shell on every UNIX box:

$ cat tst.awk
NR == FNR {
    vals["["$1"]"]
    next
}
rec == "" { key = $1 }
{ rec = rec $0 ORS }
/^[[:blank:]]*},$/ {
    if ( key in vals ) {
        printf "%s", rec
    }
    key = rec = ""
}

.

$ awk -f tst.awk file_a file_b
    [300] = {
        name = "John",
        age = "12",
        address = {""},
        job = "Marketing",
        job a = "some job",
        job b = {"some job"},
        car = 0,
        salary = 0
    },
    [302] = {
        name = "John",
        age = "12",
        address = {""},
        job = "Marketing",
        job a = "some job",
        job b = {"some job"},
        car = 0,
        salary = 0
    },
Ed Morton
  • 28,789
  • 5
  • 20
  • 47
  • Thank you sir, your solution works perfectly on the example, however, i may have made it too simple, actually some of the record may looks like (i edited my question to include the new sample data format) Sorry for trouble.. when i apply to actual data it only print until address.. Thank you.. – NewbieCabbage Mar 17 '20 at 14:14
  • 1
    Nevermind, already got it to work sir, i change to { rec = rec $0 ORS } /^\t},$/ { – NewbieCabbage Mar 17 '20 at 14:25
0

If you use sed the the following line could help you:
while read n; do sed -n -e "/^\[$n\]/,/^},$/p" b.txt; done < a.txt

bey0nd
  • 917
  • 3
  • 11
  • seems like it print nothing, btw, i miss look the b file actually got . i change my question slightly. – NewbieCabbage Mar 17 '20 at 12:01
  • See [why-is-using-a-shell-loop-to-process-text-considered-bad-practice](https://unix.stackexchange.com/questions/169716/why-is-using-a-shell-loop-to-process-text-considered-bad-practice) – Ed Morton Mar 17 '20 at 13:09
0

Tested with Below command and worked fine

command

for i in `cat filea`; do sed -n '/'$i'/,/^[[:space:]]\{4\}\},$/p' fileb; done

output

 [300] = {
        name = "John",
        age = "12",
        address = {""},
        job = "Marketing",
        job a = "some job",
        job b = {"some job"},
        car = 0,
        salary = 0
    },
    [302] = {
        name = "John",
        age = "12",
        address = {""},
        job = "Marketing",
        job a = "some job",
        job b = {"some job"},
        car = 0,
        salary = 0
    },
    [303] = {
        name = "John",
        age = "12",
        address = {
        "Person street address"
        },
        job = "Marketing",
        job a = "some job",
        job b = {
        "His job description"
        },
        car = 0,
        salary = 0
    },
Praveen Kumar BS
  • 5,139
  • 2
  • 9
  • 14