1

Thank you in advance and sorry for the bad english!

I want

'Odd rows' 'CRLF' 'Even rows' CRLF' → 'Odd rows' ',' 'Even rows' 'CRLF'

Example Input:

  0
SECTION
  2
HEADER

Desired Output:

  0,SECTION
  2,HEADER

What I have tried:

Find: (.*)\n(.*)\n
Replace: $1,$2\n

I want ー Easy to see dxf

4 Answers 4

2

. matches a newline the same as it matches any other characer, so the first .* is going to gobble up the whole string and leave nothing left.

Instead, use a character group that excludes \n. Also, it's not clear whether your final line terminates with a \n or not, so the Regex should handle for that:

Find

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

Replace

$1,$2$3

Breakdown:

  • ([^\n]*) - 0 or more characters that are not \n
  • \n
  • ([^\n]*)
  • (\n|$) - \n or end of string
1

For you example data you could capture one or more digits in capturing group 1 followed by matching a newline.

In the replacement use group 1 followed by a comma.

Match

(\d+)(?:r?\n|\r)

Regex demo

Replace

$1,

1

you should match enter and space also, because there may be multiple spaces and new line available in string try this regex-

"0\nSECTION\n 2\nHEADER".replace(/([\d]+)([\s\n]+)([^\d\s\n]*)/g,"$1,$3")

var myStr = `  0
SECTION
  2
HEADER`;

var output = myStr.replace(/([\d]+)([\s\n]+)([^\d\s\n]*)/g,"$1,$3");
console.log(output);

-1

DXF file ok

ODD line abc...

(AWK)

NR%2!=0{L1=$0}
NR%2==0{print L1 "," $0;L1=""}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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