Score:0

Convert output file with the following columns to CSV removing the pipes

in flag

This is what I have in my output file:

    v6_ntoa    |   v6_ntoa    |     id      | client_syn_packets
---------------+--------------+-------------+--------------------
 105.245.90.61 | 82.17.112.84 | 15426766476 |                  1

I would like this to be converted to csv file in the following format:

105.245.90.61,82.17.112.84,15426766476,1

Need your help please (sed, awk or any other way). I tried several different ways but unable to remove pipes and proper spacing.

hr flag
Is there only a single line of data? Do you want to preserve the header information?
in flag
don't have to keep the header info. yes, it's a single line of data
gi flag
have you tried my solution? https://askubuntu.com/a/1395720/796708
Score:1
gi flag

Using Miller and tail, and running

<input.txt tail -n +3 | mlr --csv --ifs "|" -N clean-whitespace

you have

105.245.90.61,82.17.112.84,15426766476,1

Some notes:

  • <input.txt tail -n +3 to output only the record you want
  • --ifs "|", to set the field separator
  • -N to set no heading input and output
  • clean-whitespace to clean the whitespaces
Score:0
hr flag

Probably the simplest:

$ sed -n '3s/ *| */,/gp' file
 195.245.90.61,62.17.112.84,15426766476,1

(note however that this does leave a leading space; you could add another substitute to remove that if it's a problem).

If you want to get fancy, use GNU awk and define a field as any sequence of non-space-or-bar characters:

$ gawk 'BEGIN{FPAT="[^ |]+"; OFS=","} NR==3{$1=$1; print}' file
195.245.90.61,62.17.112.84,15426766476,1

or to preserve headers

$ gawk 'BEGIN{FPAT="[^ |]+"; OFS=","} NR%2{$1=$1; print}' file
v6_ntoa,v6_ntoa,id,client_syn_packets
195.245.90.61,62.17.112.84,15426766476,1
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.