sed - Replace new line characters not followed by 5-digit number

AlSher

I have a csv file with some (dirty) DB schema.

example:

10391,0,3,4,12,44 --ok
10391,0,3,4,      --not ok
12,44             --not ok
10391,0,3,4,12,44 --ok

I want to write sed script to replace new line characters (not followed by 5-digit number) with spaces.

Wrote this one, but not works correctly for me:

sed 's/\n\([0-9]{1,4}\)/ \1/g' 

running on this sample

11111 sss
22222 aaa
3333 aaa
333 sss
22 sss
1 sss

should produce

11111 sss
22222 aaa 3333 aaa 333 sss 22 sss 1 sss

thanks to anyone who will be able to help

zx81

Or use a Perl One-Liner

perl -0777 -pe 's/\n(?!\d{5}\b)/ /g' yourfile

Explanation

  • \n matches the newline
  • (?!\d{5}\b) asserts that what follows is not five digits and a word boundary
  • we insert a space

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Can sed replace new line characters?

From Dev

sed replace special characters with new line on Linux

From Dev

Sed replace with first characters in line

From Dev

sed to replace comma and new line

From Dev

Regex for 5 digit number with optional characters

From Dev

Print each digit of a number on a new line in Java

From Dev

Combining awk and sed to match line and replace characters

From Dev

Combining awk and sed to match line and replace characters

From Dev

Replace % followed by a number in a string

From Dev

Searching a two digit number in a line of a file through Sed

From Dev

Replace text of a specific line number using sed

From Dev

Using sed or VIM to replace space with new line

From Dev

sed replace text containing new line

From Dev

using sed replace the new line with character

From Dev

Replace a certain number of characters with * in the middle of the line

From Dev

RegEx to remove new line characters and replace with comma

From Dev

sed replace entire line at line number with a string that contains nested quotes

From Dev

Use sed to replace line of text with new line including variable

From Dev

Replace special characters or special characters followed by space

From Dev

How to replace a hashtag followed by a number?

From Dev

looking for 1-3 words preceded by 4 digit number followed by non alpha characters or 1-4 specific words

From Dev

How to use sed to replace a whole line with another one with characters in it

From Dev

(Sed) Search/Replace Involving Negative Number Yields Extra Characters

From Dev

Sed Match Number followed by string and return Number

From Dev

Optimal way to add new line characters for every known number of characters

From Dev

Replace specials characters with sed

From Dev

Replace a double backslash followed by quote (\\') using sed?

From Dev

sed: Replace a whole line by number overwriting the original file

From Dev

Replace the pattern a certain number of times per line, using sed

Related Related

  1. 1

    Can sed replace new line characters?

  2. 2

    sed replace special characters with new line on Linux

  3. 3

    Sed replace with first characters in line

  4. 4

    sed to replace comma and new line

  5. 5

    Regex for 5 digit number with optional characters

  6. 6

    Print each digit of a number on a new line in Java

  7. 7

    Combining awk and sed to match line and replace characters

  8. 8

    Combining awk and sed to match line and replace characters

  9. 9

    Replace % followed by a number in a string

  10. 10

    Searching a two digit number in a line of a file through Sed

  11. 11

    Replace text of a specific line number using sed

  12. 12

    Using sed or VIM to replace space with new line

  13. 13

    sed replace text containing new line

  14. 14

    using sed replace the new line with character

  15. 15

    Replace a certain number of characters with * in the middle of the line

  16. 16

    RegEx to remove new line characters and replace with comma

  17. 17

    sed replace entire line at line number with a string that contains nested quotes

  18. 18

    Use sed to replace line of text with new line including variable

  19. 19

    Replace special characters or special characters followed by space

  20. 20

    How to replace a hashtag followed by a number?

  21. 21

    looking for 1-3 words preceded by 4 digit number followed by non alpha characters or 1-4 specific words

  22. 22

    How to use sed to replace a whole line with another one with characters in it

  23. 23

    (Sed) Search/Replace Involving Negative Number Yields Extra Characters

  24. 24

    Sed Match Number followed by string and return Number

  25. 25

    Optimal way to add new line characters for every known number of characters

  26. 26

    Replace specials characters with sed

  27. 27

    Replace a double backslash followed by quote (\\') using sed?

  28. 28

    sed: Replace a whole line by number overwriting the original file

  29. 29

    Replace the pattern a certain number of times per line, using sed

HotTag

Archive