Skip to main content
added 114 characters in body
Source Link

Purely using GNU sed substitution:

sed 's/\(\([^;]*;\)\{5\}\)/\1\n/g' 

or without all the escaping backslashes using -E (thanks @JoL):

sed -E 's/(([^;]*;){5})/\1\n/g' 

Example:

$ cat test.txt a;a;a;a;a;a;a;a;a;a;a;a;a;a;a;a;a;a;a;a;a;a;a;a; etc...... $ cat test.txt | sed 's/\(\([^;]*;\)\{5\}\)/\1\n/g' a;a;a;a;a; a;a;a;a;a; a;a;a;a;a; a;a;a;a;a; a;a;a;a; etc...... 

Explanation:

  • \([^;]*;\): regex capture group matching all characters up to and including a semi-colon.
  • \(\([^;]*;\)\{5\}\): regex capture group matching five occurrences of the above. In the sed command, this will be matched into \1.
  • s/\(\([^;]*;\)\{5\}\)/\1\n/g: substitute (s/) every occurrence (/g) of the group of five occurrences of all characters up to and including a semicolon (\(\([^;]*;\)\{5\}\)) with itself (\1), but followed by a newline character (\n).

Purely using GNU sed substitution:

sed 's/\(\([^;]*;\)\{5\}\)/\1\n/g' 

Example:

$ cat test.txt a;a;a;a;a;a;a;a;a;a;a;a;a;a;a;a;a;a;a;a;a;a;a;a; etc...... $ cat test.txt | sed 's/\(\([^;]*;\)\{5\}\)/\1\n/g' a;a;a;a;a; a;a;a;a;a; a;a;a;a;a; a;a;a;a;a; a;a;a;a; etc...... 

Explanation:

  • \([^;]*;\): regex capture group matching all characters up to and including a semi-colon.
  • \(\([^;]*;\)\{5\}\): regex capture group matching five occurrences of the above. In the sed command, this will be matched into \1.
  • s/\(\([^;]*;\)\{5\}\)/\1\n/g: substitute (s/) every occurrence (/g) of the group of five occurrences of all characters up to and including a semicolon (\(\([^;]*;\)\{5\}\)) with itself (\1), but followed by a newline character (\n).

Purely using GNU sed substitution:

sed 's/\(\([^;]*;\)\{5\}\)/\1\n/g' 

or without all the escaping backslashes using -E (thanks @JoL):

sed -E 's/(([^;]*;){5})/\1\n/g' 

Example:

$ cat test.txt a;a;a;a;a;a;a;a;a;a;a;a;a;a;a;a;a;a;a;a;a;a;a;a; etc...... $ cat test.txt | sed 's/\(\([^;]*;\)\{5\}\)/\1\n/g' a;a;a;a;a; a;a;a;a;a; a;a;a;a;a; a;a;a;a;a; a;a;a;a; etc...... 

Explanation:

  • \([^;]*;\): regex capture group matching all characters up to and including a semi-colon.
  • \(\([^;]*;\)\{5\}\): regex capture group matching five occurrences of the above. In the sed command, this will be matched into \1.
  • s/\(\([^;]*;\)\{5\}\)/\1\n/g: substitute (s/) every occurrence (/g) of the group of five occurrences of all characters up to and including a semicolon (\(\([^;]*;\)\{5\}\)) with itself (\1), but followed by a newline character (\n).
Most sed implementations don't support \n
Source Link
terdon
  • 252.7k
  • 69
  • 481
  • 719

Purely using GNU sed substitution:

sed 's/\(\([^;]*;\)\{5\}\)/\1\n/g' 

Example:

$ cat test.txt a;a;a;a;a;a;a;a;a;a;a;a;a;a;a;a;a;a;a;a;a;a;a;a; etc...... $ cat test.txt | sed 's/\(\([^;]*;\)\{5\}\)/\1\n/g' a;a;a;a;a; a;a;a;a;a; a;a;a;a;a; a;a;a;a;a; a;a;a;a; etc...... 

Explanation:

  • \([^;]*;\): regex capture group matching all characters up to and including a semi-colon.
  • \(\([^;]*;\)\{5\}\): regex capture group matching five occurrences of the above. In the sed command, this will be matched into \1.
  • s/\(\([^;]*;\)\{5\}\)/\1\n/g: substitute (s/) every occurrence (/g) of the group of five occurrences of all characters up to and including a semicolon (\(\([^;]*;\)\{5\}\)) with itself (\1), but followed by a newline character (\n).

Purely using sed substitution:

sed 's/\(\([^;]*;\)\{5\}\)/\1\n/g' 

Example:

$ cat test.txt a;a;a;a;a;a;a;a;a;a;a;a;a;a;a;a;a;a;a;a;a;a;a;a; etc...... $ cat test.txt | sed 's/\(\([^;]*;\)\{5\}\)/\1\n/g' a;a;a;a;a; a;a;a;a;a; a;a;a;a;a; a;a;a;a;a; a;a;a;a; etc...... 

Explanation:

  • \([^;]*;\): regex capture group matching all characters up to and including a semi-colon.
  • \(\([^;]*;\)\{5\}\): regex capture group matching five occurrences of the above. In the sed command, this will be matched into \1.
  • s/\(\([^;]*;\)\{5\}\)/\1\n/g: substitute (s/) every occurrence (/g) of the group of five occurrences of all characters up to and including a semicolon (\(\([^;]*;\)\{5\}\)) with itself (\1), but followed by a newline character (\n).

Purely using GNU sed substitution:

sed 's/\(\([^;]*;\)\{5\}\)/\1\n/g' 

Example:

$ cat test.txt a;a;a;a;a;a;a;a;a;a;a;a;a;a;a;a;a;a;a;a;a;a;a;a; etc...... $ cat test.txt | sed 's/\(\([^;]*;\)\{5\}\)/\1\n/g' a;a;a;a;a; a;a;a;a;a; a;a;a;a;a; a;a;a;a;a; a;a;a;a; etc...... 

Explanation:

  • \([^;]*;\): regex capture group matching all characters up to and including a semi-colon.
  • \(\([^;]*;\)\{5\}\): regex capture group matching five occurrences of the above. In the sed command, this will be matched into \1.
  • s/\(\([^;]*;\)\{5\}\)/\1\n/g: substitute (s/) every occurrence (/g) of the group of five occurrences of all characters up to and including a semicolon (\(\([^;]*;\)\{5\}\)) with itself (\1), but followed by a newline character (\n).
Added explanation
Source Link

Purely using sed substitution:

sed 's/\(\([^;]*;\)\{5\}\)/\1\n/g' 

Example:

$ cat test.txt a;a;a;a;a;a;a;a;a;a;a;a;a;a;a;a;a;a;a;a;a;a;a;a; etc...... $ cat test.txt | sed 's/\(\([^;]*;\)\{5\}\)/\1\n/g' a;a;a;a;a; a;a;a;a;a; a;a;a;a;a; a;a;a;a;a; a;a;a;a; etc...... 

Explanation:

  • \([^;]*;\): regex capture group matching all characters up to and including a semi-colon.
  • \(\([^;]*;\)\{5\}\): regex capture group matching five occurrences of the above. In the sed command, this will be matched into \1.
  • s/\(\([^;]*;\)\{5\}\)/\1\n/g: substitute (s/) every occurrence (/g) of the group of five occurrences of all characters up to and including a semicolon (\(\([^;]*;\)\{5\}\)) with itself (\1), but followed by a newline character (\n).

Purely using sed substitution:

sed 's/\(\([^;]*;\)\{5\}\)/\1\n/g' 

Example:

$ cat test.txt a;a;a;a;a;a;a;a;a;a;a;a;a;a;a;a;a;a;a;a;a;a;a;a; etc...... $ cat test.txt | sed 's/\(\([^;]*;\)\{5\}\)/\1\n/g' a;a;a;a;a; a;a;a;a;a; a;a;a;a;a; a;a;a;a;a; a;a;a;a; etc...... 

Purely using sed substitution:

sed 's/\(\([^;]*;\)\{5\}\)/\1\n/g' 

Example:

$ cat test.txt a;a;a;a;a;a;a;a;a;a;a;a;a;a;a;a;a;a;a;a;a;a;a;a; etc...... $ cat test.txt | sed 's/\(\([^;]*;\)\{5\}\)/\1\n/g' a;a;a;a;a; a;a;a;a;a; a;a;a;a;a; a;a;a;a;a; a;a;a;a; etc...... 

Explanation:

  • \([^;]*;\): regex capture group matching all characters up to and including a semi-colon.
  • \(\([^;]*;\)\{5\}\): regex capture group matching five occurrences of the above. In the sed command, this will be matched into \1.
  • s/\(\([^;]*;\)\{5\}\)/\1\n/g: substitute (s/) every occurrence (/g) of the group of five occurrences of all characters up to and including a semicolon (\(\([^;]*;\)\{5\}\)) with itself (\1), but followed by a newline character (\n).
Source Link
Loading