Skip to content
Advertisement

move single string up into previous row

I have been trying to solve a problem for hours and haven’t found a solution. I am trying to reformat a text file that has been incorrectly parsed. The file format contains a list of names, starting with the last name first, and has a troublesome sole first name on one of the rows as below:

 Mussen, Steven
 Fletcher, Bob
 Monday, Mario
 Glitch, Christopher
 Fletcher, Bob
 Ram, Dale
 Ram,
 Dale
 Mussen, Steven
 Von Hemie, Donnie
 Gallape, Patrick
 Desormore, J.
 Sun, Dallas
 Sun, Dallas

I have tried dumping the file into a bash array by doing:

declare -a GAMERS=($(cat Gamers.txt))"

then running a for-loop:

for i in "${GAMERS[@]}";do echo $i >>NewGamers.txt; done; 

which gives me one word per a line. I then tried using awk to combine every two rows into one with:

awk 'NR%2{printf $0" ";next;}1' NewGamers.txt

The problem is Mr. Donnie Von Hemie has three strings in his name and makes my awk solution useless. The end goal is to simply solve bad formatting, in this case I am trying to move the sole Dale up a row. The end output should be:

Mussen, Steven
Fletcher, Bob
Monday, Mario
Glitch, Christopher
Fletcher, Bob
Ram, Dale
Ram, Dale
Mussen, Steven
Von Hemie, Donnie
Gallape, Patrick
Desormore, J.
Sun, Dallas
Sun, Dallas

Any ideas would be hella appreciated…

Advertisement

Answer

You can keep it simple and use awk:

awk -F ' *, *' '!$2{if (!p) p=$0; else { print p, $0; p=""} next} 1' file

Mussen, Steven
Fletcher, Bob
Monday, Mario
Glitch, Christopher
Fletcher, Bob
Ram, Dale
Ram, Dale
Mussen, Steven
Von Hemie, Donnie
Gallape, Patrick
Desormore, J.
Sun, Dallas
Sun, Dallas
User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement