Skip to main content
added 332 characters in body
Source Link
Stéphane Chazelas
  • 584.6k
  • 96
  • 1.1k
  • 1.7k

With perl:

perl -pe 's/\n/, / unless eof' IP

Add the -i option to edit the file inplace. Or -i.orig to edit it in place but keep the original as IP.orig.

With the zsh shell, you can also load those IPs into an array with:

ips=( ${(f)"$(<IP)"} )

And then write them back joined with commas with:

print -r -- ${(j[, ])ips} > IP

Contrary to the perl one which processes one line at a time, that one loads the file whole in memory.

Two other differences:

  • empty lines are discarded
  • a newline character is always added at the end even if the original file didn't contain any like for an empty file or a file whose last line was not properly delimited.

With perl:

perl -pe 's/\n/, / unless eof' IP

Add the -i option to edit the file inplace. Or -i.orig to edit it in place but keep the original as IP.orig.

With the zsh shell, you can also load those IPs into an array with:

ips=( ${(f)"$(<IP)"} )

And then write them back joined with commas with:

print -r -- ${(j[, ])ips} > IP

With perl:

perl -pe 's/\n/, / unless eof' IP

Add the -i option to edit the file inplace. Or -i.orig to edit it in place but keep the original as IP.orig.

With the zsh shell, you can also load those IPs into an array with:

ips=( ${(f)"$(<IP)"} )

And then write them back joined with commas with:

print -r -- ${(j[, ])ips} > IP

Contrary to the perl one which processes one line at a time, that one loads the file whole in memory.

Two other differences:

  • empty lines are discarded
  • a newline character is always added at the end even if the original file didn't contain any like for an empty file or a file whose last line was not properly delimited.
added 208 characters in body
Source Link
Stéphane Chazelas
  • 584.6k
  • 96
  • 1.1k
  • 1.7k

With perl:

perl -pe 's/\n/, / unless eof' IP

Add the -i option to edit the file inplace. Or -i.orig to edit it in place but keep the original as IP.orig.

With the zsh shell, you can also load those IPs into an array with:

ips=( ${(f)"$(<IP)"} )

And then write them back joined with commas with:

print -r -- ${(j[, ])ips} > IP

With perl:

perl -pe 's/\n/, / unless eof' IP

Add the -i option to edit the file inplace. Or -i.orig to edit it in place but keep the original as IP.orig.

With perl:

perl -pe 's/\n/, / unless eof' IP

Add the -i option to edit the file inplace. Or -i.orig to edit it in place but keep the original as IP.orig.

With the zsh shell, you can also load those IPs into an array with:

ips=( ${(f)"$(<IP)"} )

And then write them back joined with commas with:

print -r -- ${(j[, ])ips} > IP
Source Link
Stéphane Chazelas
  • 584.6k
  • 96
  • 1.1k
  • 1.7k

With perl:

perl -pe 's/\n/, / unless eof' IP

Add the -i option to edit the file inplace. Or -i.orig to edit it in place but keep the original as IP.orig.