2

This one is tricky... input has multiple occurrences per line over several lines. many "id=" lines exist, but I only want ones starting with ai-c " id="

Example input (two long lines):

d-fl ai-c" id="3af3a73f26a63a-list-" lima zulu bravo hotel d-fl ai-c" id="7ab8273f273e7273f82c2-list-" alpha id="8372364fa2361ab1-list-" hotel foxtrox mike d-fl ai-c" id="973a72b72c72df12a2-list-" uniform hotel

start of second line golf november d-fl ai-c" id="83hd826ab712cc71-list-" bravo mike mike d-fl ai-c" id="7612364bab72a-list-" golf mike

Output desired is:

id="3af3a73f26a63a"

id="7ab8273f273e7273f82c2"

id="973a72b72c72df12a2"

id="83hd826ab712cc71"

id="7612364bab72a"

All of the script examples I've seen only seem to work for the first occurrence in a line.

1

One way to do this is:

grep -oP '\hai-c"\h\Kid="[^-]*'  input.file | sed -e 's/$/"/'

Where we use GNU grep with the Perl feature turned on and the -o option to output just the matche(s), rather than the default whole line when a match is found.

Output:

id="3af3a73f26a63a"
id="7ab8273f273e7273f82c2"
id="973a72b72c72df12a2"
id="83hd826ab712cc71"
id="7612364bab72a"
0

Another way:

sed ' s/d-fl ai-c" /\n/g; s/-/"\n/g; :L; /^id=/ P; D; bL; ' file
id="3af3a73f26a63a"
id="7ab8273f273e7273f82c2"
id="973a72b72c72df12a2"
id="83hd826ab712cc71"
id="7612364bab72a"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.