I am going through a file that has lines like the below:
project:
random_project_namerf1 rf2 rf3 rf4 rf5 rf6 rf7
rv1 rv2 rv3 rv4 rv5 rv6 rv7
- other random lines
where
2.
,3.
and4.
always come after a line like1.
- rv7 (random value 7) is either
foo
orbar
Example:
project: potatopotato
rf1 rf2 rf3 rf4 rf5 rf6 rf7
rv1 rv2 rv4 rv4 rv5 rv6 rv7
...
THE END
Best case scenario I want to make a .csv out of it with the fields:
project rf2 rf1 rf7 rf5 rf6
What I have now is:
awk '/foo|bar/{print $2",",$1",",$NF",",$(NF-2)",",$(NF-1)}'
The issue is that when any random value is whitespace it gets messed up since there are less fields. Usually rv6 is NULL
. On the positive side I know rv5 and rv6 follow the ipv4 pattern so like: 1.1.1.1
So my questions are:
- How can I change what I have for
$(NF-2)
and$(NF-1)
to check if they follow the IP pattern so I can successfully grab them. - How can I combine them with something like
awk '/project/{print $2}'
so I can create the .csv I want
If there is a better way or any other suggestions feel free to comment. Thanks.
project rf2 rf1 rf7 rf5 rf6
really the only output you want from that multi-line block of input? You say you want CSV output but then you show the desired output as being space-separated, not CSV.project_name rv2 rv1 rv7 rv5 rv6
(for every line rv7 was either foor/bar) and with the "first"project_name
that was above these lines. That's the best case scenario I am looking. My 1st question doesn't include project at all. I could edit again if you specify what is not that clear.My 1st question doesn't include project at all
- you have 1 question and it includes project in the sample input and expected output. Please edit your question to show concise, testable sample input and the the complete expected output given that input. Make sure your example covers all your requirements. Among other things to clean up, if you want comma-separated output then show comma-separated output.