Another sed question

I'm trying to process some files using sed but I'm running into a problem. Let me first show an example- this isn't the actual data I use but it will simplify the problem..

For my example, this is the contents of input.txt.

Name: Bob Smith
Address: 123 Main Street
City: Anytown, USA 010101

Item: Coffee Mug
Item: Baseball Hat
Item: Keychain

Here is the sed command I run to parse the information I need to capture.

cat input.txt | sed -e 's:Name\: \(.*\).*:\1:p' -e 's:Address\: \(.*\).*:\1:p' -e 's:City\: \(.*\), \(.*\) \(.*\).*:\1, \2 \3:p' -n -e 's:Item\: \(.*\).*:\1:p'

And finally, here is the output...

Bob Smith
123 Main Street
Anytown, USA 010101
Coffee Mug
Baseball Hat
Keychain

The sed command prints all of the 'Items'. What I want is a way to print only the first occurrence of the Item field.

So the output would look like this...

Bob Smith
123 Main Street
Anytown, USA 010101
Coffee Mug

I've simplified the data and the output for my question. I would really like to do it all in a shell script with no sed command files. I don't want to use tail or head either.

Any ideas? I've looked and looked but can't really find the answer. Thanks in advance for any help you may provide! :slight_smile:

nawk -f stivo.awk input.txt

stivo.awk:

BEGIN {
  FS=RS=""
}
FNR % 2 { for(i=1; i <=NF; i++) print substr($i, index($i, ":")+2); next }
/^Item:/ { print substr($1, index($1, ":")+2);next }

or the inline version:

nawk -v FS='' -v RS='' '
     FNR % 2 { for(i=1; i <=NF; i++) print substr($i, index($i, ":")+2); next }
     /^Item:/ { print substr($1, index($1, ":")+2);next }' input.txt