I'm writing a bash script for which in a file containing several entries where each entry has this structure:
Id: 33239
Folder: /Contacts/Holder/Center
Date: 04/17/20 13:17
Revision: 34011
Attrs:
firstName: Name
lastName: First Second
mobilePhone: +345555555
fileAs: 2
jobTitle: Médico
company: some company
email: test_1@somedomain.com
I need to find the "Id" of the Item associated to a specific "email". For doing that, I'm trying to use "sed" with a hold. But I fail to achieve my objective. This is what I have so far, but I'm not getting the results I need.
id=$(grep $usuario -B20 /tmp/contactos \
| grep "Folder: /Contacts/Holder" -B2 -A20 \
| sed -n "/^Id: /h;/^ email: $usuario/{g;p;}" \
| awk '{print $2}')
With this I'm trying to:
id=
- assign the value to a variable I will use later on the script
$(grep $usuario -B20 /tmp/contactos
- Get all the lines in the file, where the email appears, and also getting 20 lines before it. This is because the email appears associated with more than one Id
an undetermined number of lines below the `Id itself.
grep 'Folder: /Contacts/Holder' -B2 -A20
- I filter again, trying to get now only the results for the Ids for that email in a specific "folder path".
sed -n '/^Id: /h;/^ email: $usuario/{g;p;}
- This is the part that is not working and I don't know how to fix it. Here, I try to return the line containing the Id:
associated with the email. Something like: Id: 33239
in this example.
awk '{print $2}')
- Just me trying to print only the number from that line (33239
).
Can anyone please help to understand how I can do it with sed` or if any other option is given, it will be also more than welcome :)
Thank you very much!