Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

Here is a great challenge for someone to attack!

Is there a way to achieve the following using sed and/or awk. Please see myfile.txt below:

$cat myfile.txt:

entry

"entry-header"

entry-top

name

!udwd870kdjll   --  random string

Derek

code=nsmssdllskpdp

code=nassmdllskpdp

name

!ueo0000kdjll   --  random string

name

Moss

!ueo3870kdjll   --  random string

choice=Pear comments="Great"

choice=Pineapple comments="Not quite the best"

choice=Apple comments="OK"

choice=Grapes comments="OK"

choice=Grapefruit comments="OK"

/entry-top

/entry



entry

"entry-header"

entry-top

!udwd870kdjll   --  random string

name

!udwd870kdjll   --  random string

Sarah

code=nsmsstyyukpdp

code=nalmdllskpdpx

!ueo0000kdjll   --  random string

name

Zimmerman

!ueo3870kdjll   --  random string

choice=Orange comments="These are awesome"

choice=Pineapple comments="Nice"

choice=Mango comments="Pretty good"

/entry-top

/entry

etc...

This is a huge list of peoples favorite fruits, and their entries are in order, with the one at the top being the favorite. I need to be able to assign a number to each entry so I can collate the results. I also need to differentiate between the two name lines given, the surname always appeasr second. As long as I can update the labels (to be name1 and name2 rather than name and name), I will be able to manipulate the text how I want to.

The required output is below, and should show you exactly what I am after:


entry

"entry-header"

entry-top

name1

!udwd870kdjll   --  random string

Derek

code=nsmssdllskpdp

name2

code=nassmdllskpdp

!ueo0000kdjll   --  random string

Moss

!ueo3870kdjll   --  random string

choice1=Pear comments="Great"

choice2=Pineapple comments="Not quite the best"

choice3=Apple comments="OK"

choice4=Grapes comments="OK"

choice5=Grapefruit comments="OK"

/entry-top

/entry



entry

"entry-header"

entry-top

name1

!udwd870kdjll   --  random string

Sarah

code=nsmsstyyukpdp

name2

code=nalmdllskpdpx

!ueo0000kdjll   --  random string

Telford

!ueo3870kdjll   --  random string

choice1=Orange comments="These are awesome"

choice2=Pineapple comments="Nice"

choice3=Mango comments="Pretty good"

/entry-top

/entry
share|improve this question

closed as too localized by Mikel, Kevin, manatwork, Michael Mrozek Jun 11 '12 at 19:22

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.If this question can be reworded to fit the rules in the help center, please edit the question.

3  
I'm guessing this should probably be tagged as homework? –  Chris Down Jun 10 '12 at 21:18
3  
"Is there a way to achieve the following using sed and/or awk?" Yes, they are both Turing-complete, so it can be done in either. –  Kevin Jun 10 '12 at 21:38

1 Answer 1

Since this seems to be homework, I'm tempted not to give you anything, but in the interest of learning I'll give you this sed script to chew on:

/^entry$/{:a;n;s/^name$/name1/;tb;ba;:b;n;s/^name$/name2/;tc;bb;:c}

Note that on non-gnu systems you may need to put each command on a separate line.

share|improve this answer
    
That's great thanks. Just what I was looking for - but it isn't homework!!! that is somebody elses tag. I have changed the data to be fruit based to protect the real content. I am 48 years old, I wish I still got homework!!! –  mark Jun 11 '12 at 8:19
2  
Some friendly advice: The reason people Think it's homework is primarily because you presented it as a challenge and didn't show any evidence of trying to solve it yourself. All questions here should be some level of a challenge, and when you post in the future I suggest you take a serious stab at the problem and show us your work. –  Kevin Jun 11 '12 at 11:57

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