I have a file which looks like this:
1. result = 1.2.3.4 (1.2.3.4)
info: [Affected]
2. result = www.addr.com (2.3.4.5)
info: [not Affected]
And now I want to split it in three columns, example:
1.2.3.4 1.2.3.4 Affected
www.addr.de 2.3.4.5 not Affected
I am using awk for that: cat filename.txt | awk -F "[=()'']" '{print $2 $3 $4}'
but I still not get three columns in a row. How can I fix it? the second question: is there a better alternative than awk?
You can unset the record separator to read in each block separately, like this:
The ternary at the end handles the two different numbers of fields (7 or 8, depending on "Affected" or "not Affected"). If there are 8 fields, then the seventh one is printed after a space, otherwise, nothing is printed.
To achieve a more neatly formatted output, you can use
printf
instead ofprint
:The format specifiers dictate the width of each field. A
-
causes the content to be left-aligned.ORS
is the Output Record Separator, which is a newline on your platform by default.In terms of aligning the columns, it depends on whether you're looking for something human- or machine-readable. If you're looking to import this data into a spreadsheet, perhaps you could separate each column using a tab character
\t
(for example), which could be done by adding-v OFS='\t'
to the first version of my answer.