Eu tenho dois arquivos, o que eu quero é usararquivar ume procure o número emarquivo be imprima todos os parênteses, incluindo a linha pesquisada. Não tenho certeza se é assim que se chama.
Arquivo um:
300
302
303
Arquivo 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
},
Resultado esperado:
[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
},
Tentei usar o awk, mas não consigo fazê-lo funcionar.
awk 'NR==FNR{a[$1]=1;next} $1 in a && /\[$1\]/,/^\t\},/ {print}' a b > c
Obrigado pela ajuda..
Responder1
Supondo que você não tenha acesso a um analisador para esse formato específico e que seus registros sejam realmente tão simples e regulares quanto você mostra, isso funcionará usando qualquer awk em qualquer shell em cada caixa UNIX:
$ 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
},
Responder2
Testado com o comando abaixo e funcionou bem
comando
for i in `cat filea`; do sed -n '/'$i'/,/^[[:space:]]\{4\}\},$/p' fileb; done
saída
[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
},
Responder3
Se você usar sed
a linha a seguir, poderá ajudá-lo.
while read n; do sed -n -e "/^\[$n\]/,/^},$/p" b.txt; done < a.txt