I have a text file like this:

ge-0/0/45.0,First-Floor-Security,13
,Voice_First_Floor,18
ge-0/0/46.0,First-Floor-Security,13
,Voice_First_Floor,18
ge-0/0/47.0,AV-Media,155
,Building-Automation,75
,CobraNet,156
,First-Floor-Management,19
,First-Floor-Pavilion,11
,First-Floor-Security,13
,First-Floor-Users,10
,Guest-Wired,3
,Video,150
,Voice_First_Floor,18
,Wireless-Corp,410
,Wireless-Guest,420
,Wireless-Mtg,400
,Wireless-Voice,430
ge-1/0/0.0,First-Floor-Users,10
,Voice_First_Floor,18

I looking for a command to grab the first field of the line above and insert it into the line below. So for example, my resulting file would look like this:

ge-0/0/45.0,First-Floor-Security,13
ge-0/0/45.0,Voice_First_Floor,18
ge-0/0/46.0,First-Floor-Security,13
ge-0/0/46.0,Voice_First_Floor,18
ge-0/0/47.0,AV-Media,155
ge-0/0/47.0,Building-Automation,75
ge-0/0/47.0,CobraNet,156
ge-0/0/47.0,First-Floor-Management,19
ge-0/0/47.0,First-Floor-Pavilion,11
ge-0/0/47.0,First-Floor-Security,13
ge-0/0/47.0,First-Floor-Users,10
ge-0/0/47.0,Guest-Wired,3
ge-0/0/47.0,Video,150
ge-0/0/47.0,Voice_First_Floor,18
ge-0/0/47.0,Wireless-Corp,410
ge-0/0/47.0,Wireless-Guest,420
ge-0/0/47.0,Wireless-Mtg,400
ge-0/0/47.0,Wireless-Voice,430
ge-1/0/0.0,First-Floor-Users,10
ge-1/0/0.0,Voice_First_Floor,18

I have some understanding about grouping and backreferences but cannot seem to find a way to search, create a backreference, and then use that backreference in a different line. Is this possible? If so, would you please point me in the right direction?

Thanks,

Drew

Here's a command you could use:

:g/^,/norm 0kyt,jP

This uses the "global" command. Here's what it basically does:

:g/<regex>/<command>

will run "command" on every single line that matches the "regex". Our regex is pretty simple: Any line that starts with (^) a comma (,).

The command:

norm 0kyt,jP

norm means "run these characters like I typed them in normal mode". So 0k goes to the beginning of the last line with a comma. yt, is yank until ,. jP goes back to the line we matched on and pastes from the clipboard before the comma.

Conveniently, this runs each line in order, so it works on multiple entryless lines in a row, such as

,Building-Automation,75
,CobraNet,156
,First-Floor-Management,19
,First-Floor-Pavilion,11
,First-Floor-Security,13
,First-Floor-Users,10
,Guest-Wired,3
,Video,150
,Voice_First_Floor,18
,Wireless-Corp,410
,Wireless-Guest,420
,Wireless-Mtg,400
,Wireless-Voice,430

This is because on the "building automation" line, it will grab the "ge-0/0/47.0" from the previous line, and then paste it on this line. Then, on the "cobraNet" line, it will grab the new "ge-0/0/47.0" from the "building automation" line, etc.

  • 1
    That's awesome! Thank you. I would up vote your post but I can't seem to get the confirmation email and confirm to complete my sign up. :/ – 610Drew Nov 16 at 21:02

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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