Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove every other newline

I have a list of data in text format that looks as follows

Name1 Name2

location job date amount

Name1 Name2

location job date amount

Name1 Name2

location job date amount

I need to somehow make this into

Name1 Name2 location job date amount

...

I figure there has got to be a way to grab every other newline with either regex or excel. I could write a python script but this file has been a pain and was hoping there would be an easier way to do it.

EDIT: adding what I've tried below:

Sorry, I should have included some code... I tried

([^\n]*\n)[^\n]*\n 

in regex, but that groups the two lines together, I basically would like to grab only the newline between the groups. I have also tried doing this in excel systematically but it doesn't continue to grab every other cell when I drag the box down. It changes from every other cell in the column to every cell once I try to extrude it.

like image 580
slantzjr Avatar asked Dec 13 '13 17:12

slantzjr


People also ask

How do I get rid of the last new line?

The -c 1 flag gets the last character of the file. We pipe this character into wc to check whether it's a newline. If it is, we use head -c -1 to print the whole file except the last character, thus removing the newline. This method uses a temporary file and overwrites the original file with it.

How do I get rid of the new line in awk?

Use printf() when you want awk without printing newline.


1 Answers

Using .Net regex flavoring (should be the same for VBA, but I didn't check), I came up with the following:

Pattern = "([^\n]*)(\n)([^\n]*(\n|\z))"
Replacement = "$1$3"

Basically, catching the first NewLine character in the second capture group and then not returning it in our replacement.

Hope this helps.

like image 200
John Bustos Avatar answered Oct 18 '22 02:10

John Bustos