Skip to main content
deleted 4 characters in body
Source Link
Kusalananda
  • 355.9k
  • 42
  • 735
  • 1.1k
awk '{ print (FNR-1) % 3 ? "H" : "O", $0 }' file

This uses awk to prefix the current line with an H or an O depending on whether the current line's line number is a multiple of three or not.

If you want a tab character as a delimiter between the new column and the others, make sure that OFS has the value \t:

awk -v OFS='\t' '{ print (FNR-1) % 3 ? "H" : "O", $0 }' file

Using GNU sed specifically and assuming that you want the new column separated from the others with a tab character:

sed -e '1~3 s/^/O\t/' -e '1~3! s/^/H\t/' file

The first expression adds O followed by a tab character to every third line starting at line 1. The second adds H and a tab character to all lines that are not every third line starting at the line 1.

This requires GNU sed due to the following two non-standard features:

  1. The address n~m means "every m:th line, starting at line n".
  2. The character sequence \t is expanded to a literal tab character.

Using other tools:

yes 'O H H' | tr ' ' '\n' | head -n "$( wc -l <file )" | paste - file

This produces a steady stream of O, H, H (repeating) on separate lines using yes and tr. This stream is cut short with head to match the number of lines in our file (this is calculated using wc -l). The O:s and H:s are then inserted to the left of the rest of the file's contents using paste, with a tab character as delimiter.

awk '{ print (FNR-1) % 3 ? "H" : "O", $0 }' file

This uses awk to prefix the current line with an H or an O depending on whether the current line's line number is a multiple of three or not.

If you want a tab character as a delimiter between the new column and the others, make sure that OFS has the value \t:

awk -v OFS='\t' '{ print (FNR-1) % 3 ? "H" : "O", $0 }' file

Using GNU sed specifically and assuming that you want the new column separated from the others with a tab character:

sed -e '1~3 s/^/O\t/' -e '1~3! s/^/H\t/' file

The first expression adds O followed by a tab character to every third line starting at line 1. The second adds H and a tab character to all lines that are not every third line starting at the line 1.

This requires GNU sed due to the following two non-standard features:

  1. The address n~m means "every m:th line, starting at line n".
  2. The character sequence \t is expanded to a literal tab character.

Using other tools:

yes 'O H H' | tr ' ' '\n' | head -n "$( wc -l <file )" | paste - file

This produces a steady stream of O, H, H (repeating) on separate lines using yes and tr. This stream is cut short with head to match the number of lines in our file (this is calculated using wc -l). The O:s and H:s are then inserted to the left of the rest of the file's contents using paste, with a tab character as delimiter.

awk '{ print (FNR-1) % 3 ? "H" : "O", $0 }' file

This uses awk to prefix the current line with an H or an O depending on whether the current line's line number is a multiple of three or not.

If you want a tab character as a delimiter between the new column and the others, make sure that OFS has the value \t:

awk -v OFS='\t' '{ print (FNR-1) % 3 ? "H" : "O", $0 }' file

Using GNU sed specifically and assuming that you want the new column separated from the others with a tab character:

sed -e '1~3 s/^/O\t/' -e '1~3! s/^/H\t/' file

The first expression adds O followed by a tab character to every third line starting at line 1. The second adds H and a tab character to all lines that are not every third line starting at line 1.

This requires GNU sed due to the following two non-standard features:

  1. The address n~m means "every m:th line, starting at line n".
  2. The character sequence \t is expanded to a literal tab character.

Using other tools:

yes 'O H H' | tr ' ' '\n' | head -n "$( wc -l <file )" | paste - file

This produces a steady stream of O, H, H (repeating) on separate lines using yes and tr. This stream is cut short with head to match the number of lines in our file (this is calculated using wc -l). The O:s and H:s are then inserted to the left of the rest of the file's contents using paste, with a tab character as delimiter.

added 462 characters in body
Source Link
Kusalananda
  • 355.9k
  • 42
  • 735
  • 1.1k
awk '{ print (FNR-1) % 3 ? "H" : "O", $0 }' file

This uses awk to prefix the current line with an H or an O depending on whether the current line's line number is a multiple of three or not.

If you want a tab character as a delimiter between the new column and the others, usemake sure that OFS has the value \t:

awk -v OFS='\t' '{ print (FNR-1) % 3 ? "H" : "O", $0 }' file

Using GNU sed specifically and assuming that you want the new column separated from the others with a tab character:

sed -e '1~3 s/^/O\t/' -e '1~3! s/^/H\t/' file

The first expression adds O followed by a tab character to every third line starting at line 1. The second adds H and a tab character to all lines that are not every third line starting at the line 1.

This requires GNU sed due to the following two non-standard features:

  1. The address n~m means "every m:th line, starting at line n".
  2. The character sequence \t is expanded to a literal tab character.

Using other tools:

yes 'O H H' | tr ' ' '\n' | head -n "$( wc -l <file )" | paste - file

This produces a steady stream of O, H, H (repeating) on separate lines using yes and tr. This stream is cut short with head to match the number of lines in our file (this is calculated using wc -l). The O:s and H:s are then inserted to the left of the rest of the file's contents using paste, with a tab character as delimiter.

awk '{ print (FNR-1) % 3 ? "H" : "O", $0 }' file

This uses awk to prefix the current line with an H or an O depending on whether the current line's line number is a multiple of three or not.

If you want a tab character as a delimiter between the new column and the others, use

awk -v OFS='\t' '{ print (FNR-1) % 3 ? "H" : "O", $0 }' file

Using GNU sed specifically and assuming that you want the new column separated from the others with a tab character:

sed -e '1~3 s/^/O\t/' -e '1~3! s/^/H\t/' file

The first expression adds O followed by a tab character to every third line starting at line 1. The second adds H and a tab character to all lines that are not every third line starting at the line 1.

This requires GNU sed due to the following two non-standard features:

  1. The address n~m means "every m:th line, starting at line n".
  2. The character sequence \t is expanded to a literal tab character.
awk '{ print (FNR-1) % 3 ? "H" : "O", $0 }' file

This uses awk to prefix the current line with an H or an O depending on whether the current line's line number is a multiple of three or not.

If you want a tab character as a delimiter between the new column and the others, make sure that OFS has the value \t:

awk -v OFS='\t' '{ print (FNR-1) % 3 ? "H" : "O", $0 }' file

Using GNU sed specifically and assuming that you want the new column separated from the others with a tab character:

sed -e '1~3 s/^/O\t/' -e '1~3! s/^/H\t/' file

The first expression adds O followed by a tab character to every third line starting at line 1. The second adds H and a tab character to all lines that are not every third line starting at the line 1.

This requires GNU sed due to the following two non-standard features:

  1. The address n~m means "every m:th line, starting at line n".
  2. The character sequence \t is expanded to a literal tab character.

Using other tools:

yes 'O H H' | tr ' ' '\n' | head -n "$( wc -l <file )" | paste - file

This produces a steady stream of O, H, H (repeating) on separate lines using yes and tr. This stream is cut short with head to match the number of lines in our file (this is calculated using wc -l). The O:s and H:s are then inserted to the left of the rest of the file's contents using paste, with a tab character as delimiter.

added 220 characters in body
Source Link
Kusalananda
  • 355.9k
  • 42
  • 735
  • 1.1k
awk '{ print (FNR-1) % 3 ? "H" : "O", $0 }' file

This uses awk to prefix the current line with an H or an O depending on whether the current line's line number is a multiple of three or not.

If you want a tab character as a delimiter between the new column and the others, use

awk -v OFS='\t' '{ print (FNR-1) % 3 ? "H" : "O", $0 }' file

Using GNU sed specifically and assuming that you want the new column separated from the others with a tab character:

sed -e '1~3 s/^/O\t/' -e '1~3! s/^/H\t/' file

The first expression adds O followed by a tab character to every third line starting at line 1. The second adds H and a tab character to all lines that are not every third line starting at the line 1.

This requires GNU sed due to the following two non-standard features:

  1. The address n~m means "every m:th line, starting at line n".
  2. The character sequence \t is expanded to a literal tab character.

Using GNU sed specifically and assuming that you want the new column separated from the others with a tab character:

sed -e '1~3 s/^/O\t/' -e '1~3! s/^/H\t/' file

The first expression adds O followed by a tab character to every third line starting at line 1. The second adds H and a tab character to all lines that are not every third line starting at the line 1.

This requires GNU sed due to the following two non-standard features:

  1. The address n~m means "every m:th line, starting at line n".
  2. The character sequence \t is expanded to a literal tab character.
awk '{ print (FNR-1) % 3 ? "H" : "O", $0 }' file

This uses awk to prefix the current line with an H or an O depending on whether the current line's line number is a multiple of three or not.

If you want a tab character as a delimiter between the new column and the others, use

awk -v OFS='\t' '{ print (FNR-1) % 3 ? "H" : "O", $0 }' file

Using GNU sed specifically and assuming that you want the new column separated from the others with a tab character:

sed -e '1~3 s/^/O\t/' -e '1~3! s/^/H\t/' file

The first expression adds O followed by a tab character to every third line starting at line 1. The second adds H and a tab character to all lines that are not every third line starting at the line 1.

This requires GNU sed due to the following two non-standard features:

  1. The address n~m means "every m:th line, starting at line n".
  2. The character sequence \t is expanded to a literal tab character.
Source Link
Kusalananda
  • 355.9k
  • 42
  • 735
  • 1.1k
Loading