input.txt
EN1
EN2
EN3
EN4
EN5
output
EN1,EN2,EN3,EN4,EN5
I have tried awk.But it is not printing with comma
awk 'BEGIN { OFS = ","} { printf $1}' input.txt
I have GNU Awk 4.0.0 version
|
input.txt
output
I have tried awk.But it is not printing with comma
I have GNU Awk 4.0.0 version |
|||||
|
yields this:
so is printing with a comma (so I'm not sure I understand your comment in your post about this not happening) though I suspect the trailing comma is a problem. Tested with GNU Awk 3.1.7 |
||||
|
You can use
This replaces the final newline by a comma as well. To avoid this, on Linux, if you know that the input file does end with a newline:
Add Alternatively, you can get the shell to remove a trailing comma if there is one.
|
||||
|
There's also
An advantage here is that there is no trailing comma to get rid of.
(FYI NOTE: This only works if the input file is as described (one field per line, no spaces). If there are more fields and/or spaces in the input you can use awk or sed to pre-process the input. For example, with input like this:
Here awk is used to extract only the first field:
In this second (sed) example, spaces in the original input are replaced with some other string (chosen as unlikely to be in the original input), then fed into xargs. sed then replaces the spaces added by xargs, and then restores the strings from the input:
Now for some gratuitous op-ed commentary: One of the most useful pieces of knowledge about unix text processing tools is that you can and should should think of data as being almost infinitely malleable - you can transform it into whatever form you need either to provide input to another process or to produce the output you want or both. This is part of the reason why unix people tend to hate proprietary data formats - it's not just a philosophical disapproval or a wish to avoid vendor lock-in, it's also the very pragmatic fact that they make it difficult for us to manipulate and use our data in ways that weren't foreseen by the software's developers. |
|||||||
|
output: no trailing
|
||||
|
|