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

3 Answers3

3

Assuming you don't have access to a parser for this particular format 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
    },
Kusalananda
  • 333,661
Ed Morton
  • 31,617
  • 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 following line could help you.

while read n; do sed -n -e "/^\[$n\]/,/^},$/p" b.txt; done < a.txt
AdminBee
  • 22,803
bey0nd
  • 937
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
    },