Skip to main content
code golfed
Source Link
Adam Katz
  • 4.2k
  • 1
  • 28
  • 35

First, in the spirit of teaching, here's a fixed version of your own logic (wrapped & spaced for legibility):

$ echo machine23,machine094,machine73,machine83 \
    |awk -F"," '{ for (i = 1; i <= NF; i++) { printf "%-20s\n", $i } }'
machine23           
machine094          
machine73           
machine83           

Because you used %-20s, this adds a bit of trailing white space (select the above output to see it), but all I did was move the printf "\n" into the printf inside the for loop.

Since your version had so little white space, here's a code-golfedcode-golfed solution using your logic:

echo machine23,machine094,machine73,machine83|awk -F, '{forwhile(i=1;i<=NF;i++i<NF)print$iprint$++i}'

 

Here's a field-agnostic awk solution using global replacement without needing a loop:

$ echo machine23,machine094,machine73,machine83 |awk '{ gsub(/,/,"\n"); print }'
machine23
machine094
machine73
machine83

This mimics the following sed solution:

$ echo machine23,machine094,machine73,machine83 |sed 's/,/\n/g'
machine23
machine094
machine73
machine83

Both of the above simply replace commas with newlines, just like cas's tr solution.

Here's a grep solution:

$ echo machine23,machine094,machine73,machine83 |grep -o '[^,]*'
machine23
machine094
machine73
machine83

This has grep show only (-o) matching non-comma text, with each item on its own line.

First, in the spirit of teaching, here's a fixed version of your own logic (wrapped & spaced for legibility):

$ echo machine23,machine094,machine73,machine83 \
    |awk -F"," '{ for (i = 1; i <= NF; i++) { printf "%-20s\n", $i } }'
machine23           
machine094          
machine73           
machine83           

Because you used %-20s, this adds a bit of trailing white space (select the above output to see it), but all I did was move the printf "\n" into the printf inside the for loop.

Since your version had so little white space, here's a code-golfed solution using your logic:

echo machine23,machine094,machine73,machine83|awk -F, '{for(i=1;i<=NF;i++)print$i}'

 

Here's a field-agnostic awk solution using global replacement without needing a loop:

$ echo machine23,machine094,machine73,machine83 |awk '{ gsub(/,/,"\n"); print }'
machine23
machine094
machine73
machine83

This mimics the following sed solution:

$ echo machine23,machine094,machine73,machine83 |sed 's/,/\n/g'
machine23
machine094
machine73
machine83

Both of the above simply replace commas with newlines, just like cas's tr solution.

Here's a grep solution:

$ echo machine23,machine094,machine73,machine83 |grep -o '[^,]*'
machine23
machine094
machine73
machine83

This has grep show only (-o) matching non-comma text, with each item on its own line.

First, in the spirit of teaching, here's a fixed version of your own logic (wrapped & spaced for legibility):

$ echo machine23,machine094,machine73,machine83 \
    |awk -F"," '{ for (i = 1; i <= NF; i++) { printf "%-20s\n", $i } }'
machine23           
machine094          
machine73           
machine83           

Because you used %-20s, this adds a bit of trailing white space (select the above output to see it), but all I did was move the printf "\n" into the printf inside the for loop.

Since your version had so little white space, here's a code-golfed solution using your logic:

echo machine23,machine094,machine73,machine83|awk -F, '{while(i<NF)print$++i}'

 

Here's a field-agnostic awk solution using global replacement without needing a loop:

$ echo machine23,machine094,machine73,machine83 |awk '{ gsub(/,/,"\n"); print }'
machine23
machine094
machine73
machine83

This mimics the following sed solution:

$ echo machine23,machine094,machine73,machine83 |sed 's/,/\n/g'
machine23
machine094
machine73
machine83

Both of the above simply replace commas with newlines, just like cas's tr solution.

Here's a grep solution:

$ echo machine23,machine094,machine73,machine83 |grep -o '[^,]*'
machine23
machine094
machine73
machine83

This has grep show only (-o) matching non-comma text, with each item on its own line.

solved the problem by fixing the question's logic
Source Link
Adam Katz
  • 4.2k
  • 1
  • 28
  • 35

First, in the spirit of teaching, here's a fixed version of your own logic (wrapped & spaced for legibility):

$ echo machine23,machine094,machine73,machine83 \
    |awk -F"," '{ for (i = 1; i <= NF; i++) { printf "%-20s\n", $i } }'
machine23           
machine094          
machine73           
machine83           

Because you used %-20s, this adds a bit of trailing white space (select the above output to see it), but all I did was move the printf "\n" into the printf inside the for loop.

Since your version had so little white space, here's a code-golfed solution using your logic:

echo machine23,machine094,machine73,machine83|awk -F, '{for(i=1;i<=NF;i++)print$i}'

 

Here's a field-agnostic awk solution using global replacement without needing a loop:

$ echo machine23,machine094,machine73,machine83 |awk '{ gsub(/,/,"\n"); print }'
machine23
machine094
machine73
machine83

This mimics the following sed solution:

$ echo machine23,machine094,machine73,machine83 |sed 's/,/\n/g'
machine23
machine094
machine73
machine83

Both of the above simply replace commas with newlines, just like cas's tr solution.

Here's a grep solution:

$ echo machine23,machine094,machine73,machine83 |grep -o '[^,]*'
machine23
machine094
machine73
machine83

This has grep show only (-o) matching non-comma text, with each item on its own line.

Here's a field-agnostic awk solution using global replacement:

$ echo machine23,machine094,machine73,machine83 |awk '{ gsub(/,/,"\n"); print }'
machine23
machine094
machine73
machine83

This mimics the following sed solution:

$ echo machine23,machine094,machine73,machine83 |sed 's/,/\n/g'
machine23
machine094
machine73
machine83

Both of the above simply replace commas with newlines, just like cas's tr solution.

Here's a grep solution:

$ echo machine23,machine094,machine73,machine83 |grep -o '[^,]*'
machine23
machine094
machine73
machine83

This has grep show only (-o) matching non-comma text, with each item on its own line.

First, in the spirit of teaching, here's a fixed version of your own logic (wrapped & spaced for legibility):

$ echo machine23,machine094,machine73,machine83 \
    |awk -F"," '{ for (i = 1; i <= NF; i++) { printf "%-20s\n", $i } }'
machine23           
machine094          
machine73           
machine83           

Because you used %-20s, this adds a bit of trailing white space (select the above output to see it), but all I did was move the printf "\n" into the printf inside the for loop.

Since your version had so little white space, here's a code-golfed solution using your logic:

echo machine23,machine094,machine73,machine83|awk -F, '{for(i=1;i<=NF;i++)print$i}'

 

Here's a field-agnostic awk solution using global replacement without needing a loop:

$ echo machine23,machine094,machine73,machine83 |awk '{ gsub(/,/,"\n"); print }'
machine23
machine094
machine73
machine83

This mimics the following sed solution:

$ echo machine23,machine094,machine73,machine83 |sed 's/,/\n/g'
machine23
machine094
machine73
machine83

Both of the above simply replace commas with newlines, just like cas's tr solution.

Here's a grep solution:

$ echo machine23,machine094,machine73,machine83 |grep -o '[^,]*'
machine23
machine094
machine73
machine83

This has grep show only (-o) matching non-comma text, with each item on its own line.

Source Link
Adam Katz
  • 4.2k
  • 1
  • 28
  • 35

Here's a field-agnostic awk solution using global replacement:

$ echo machine23,machine094,machine73,machine83 |awk '{ gsub(/,/,"\n"); print }'
machine23
machine094
machine73
machine83

This mimics the following sed solution:

$ echo machine23,machine094,machine73,machine83 |sed 's/,/\n/g'
machine23
machine094
machine73
machine83

Both of the above simply replace commas with newlines, just like cas's tr solution.

Here's a grep solution:

$ echo machine23,machine094,machine73,machine83 |grep -o '[^,]*'
machine23
machine094
machine73
machine83

This has grep show only (-o) matching non-comma text, with each item on its own line.