Skip to main content
Hadn't realised gawk's file and extension loading was so severely broken.
Source Link
Stéphane Chazelas
  • 585.1k
  • 96
  • 1.1k
  • 1.7k

You generally don't want to use shell loops to do text processing. Here, I'd use awk:

export H
awk 'NR == 7 {
  s = ENVIRON["H"]
  gsub(/,/, ") or\n   host(", s)
  print "   host(" s ")"}
  {print}' file

Like GNU sed has that -i option for in-place editing, GNU awk (since 4.1.0) has -i /usr/share/awk/inplace.awk¹ as an equivalent.

If you want to edit a text file, one could argue that you should use a text editor. With vim, you could implement the same approach with:

export H
vim -esc '
  let @a=$H
  6put a
  s/.*/host(&)/
  s/,/) or\rhost(/g
  x' file

¹ do not use -i inplace as gawk tries to load the inplace extension (as inplace or inplace.awk) from the current working directory first, where someone could have planted malware. The path of the inplace extension supplied with gawk may vary with the system, see the output of gawk 'BEGIN{print ENVIRON["AWKPATH"]}'

You generally don't want to use shell loops to do text processing. Here, I'd use awk:

export H
awk 'NR == 7 {
  s = ENVIRON["H"]
  gsub(/,/, ") or\n   host(", s)
  print "   host(" s ")"}
  {print}' file

Like GNU sed has that -i option for in-place editing, GNU awk (since 4.1.0) has -i inplace as an equivalent.

If you want to edit a text file, one could argue that you should use a text editor. With vim, you could implement the same approach with:

export H
vim -esc '
  let @a=$H
  6put a
  s/.*/host(&)/
  s/,/) or\rhost(/g
  x' file

You generally don't want to use shell loops to do text processing. Here, I'd use awk:

export H
awk 'NR == 7 {
  s = ENVIRON["H"]
  gsub(/,/, ") or\n   host(", s)
  print "   host(" s ")"}
  {print}' file

Like GNU sed has that -i option for in-place editing, GNU awk (since 4.1.0) has -i /usr/share/awk/inplace.awk¹ as an equivalent.

If you want to edit a text file, one could argue that you should use a text editor. With vim, you could implement the same approach with:

export H
vim -esc '
  let @a=$H
  6put a
  s/.*/host(&)/
  s/,/) or\rhost(/g
  x' file

¹ do not use -i inplace as gawk tries to load the inplace extension (as inplace or inplace.awk) from the current working directory first, where someone could have planted malware. The path of the inplace extension supplied with gawk may vary with the system, see the output of gawk 'BEGIN{print ENVIRON["AWKPATH"]}'

added 14 characters in body
Source Link
Stéphane Chazelas
  • 585.1k
  • 96
  • 1.1k
  • 1.7k

You generally don't want to use shell loops to do text processing. Here, I'd use awk:

export H
awk 'NR == 7 {
  s = ENVIRON["H"]
  gsub(/,/, ") or\n   host(", s)
  print "   host(" s ")"}
  {print}' file

Like GNU sed has that -i option for in-place editing, GNU awk (since 4.1.0) has -i inplace as an equivalent.

If you want to edit a text file, one could argue that you should use a text editor. With vim, you could implement the same approach with:

export H
vim -esc '
  let @a=$H
  6put a
  s/.*/host(&)/
  s/,/) or\rhost(/g
  x' file

You generally don't want to use shell loops to do text processing. Here, I'd use awk:

export H
awk 'NR == 7 {
  s = ENVIRON["H"]
  gsub(/,/, ") or\n   host(", s)
  print "   host(" s ")"}
  {print}' file

Like GNU sed has that -i option for in-place editing, GNU awk has -i inplace as an equivalent.

If you want to edit a text file, one could argue that you should use a text editor. With vim, you could implement the same approach with:

export H
vim -esc '
  let @a=$H
  6put a
  s/.*/host(&)/
  s/,/) or\rhost(/g
  x' file

You generally don't want to use shell loops to do text processing. Here, I'd use awk:

export H
awk 'NR == 7 {
  s = ENVIRON["H"]
  gsub(/,/, ") or\n   host(", s)
  print "   host(" s ")"}
  {print}' file

Like GNU sed has that -i option for in-place editing, GNU awk (since 4.1.0) has -i inplace as an equivalent.

If you want to edit a text file, one could argue that you should use a text editor. With vim, you could implement the same approach with:

export H
vim -esc '
  let @a=$H
  6put a
  s/.*/host(&)/
  s/,/) or\rhost(/g
  x' file
added 267 characters in body
Source Link
Stéphane Chazelas
  • 585.1k
  • 96
  • 1.1k
  • 1.7k

You generally don't want to use shell loops to do text processing. Here, I'd use awk:

export H
awk 'NR == 7 {
  s = ENVIRON["H"]
  gsub(/,/, ") or\n   host(", s)
  print "   host(" s ")"}
  {print}' file

Like GNU sed has that -i option for in-place editing, GNU awk has -i inplace as an equivalent.

If you want to edit a text file, one could argue that you should use a text editor. With vim, you could implement the same approach with:

export H
vim -esc '
  let @a=$H
  6put a
  s/.*/host(&)/
  s/,/) or\rhost(/g
  x' file

You generally don't want to use shell loops to do text processing. Here, I'd use awk:

export H
awk 'NR == 7 {
  s = ENVIRON["H"]
  gsub(/,/, ") or\n   host(", s)
  print "   host(" s ")"}
  {print}' file

Like GNU sed has that -i option for in-place editing, GNU awk has -i inplace as an equivalent.

You generally don't want to use shell loops to do text processing. Here, I'd use awk:

export H
awk 'NR == 7 {
  s = ENVIRON["H"]
  gsub(/,/, ") or\n   host(", s)
  print "   host(" s ")"}
  {print}' file

Like GNU sed has that -i option for in-place editing, GNU awk has -i inplace as an equivalent.

If you want to edit a text file, one could argue that you should use a text editor. With vim, you could implement the same approach with:

export H
vim -esc '
  let @a=$H
  6put a
  s/.*/host(&)/
  s/,/) or\rhost(/g
  x' file
Source Link
Stéphane Chazelas
  • 585.1k
  • 96
  • 1.1k
  • 1.7k
Loading