0

I am going through a file that has lines like the below:

  1. project: random_project_name
  2. rf1 rf2 rf3 rf4 rf5 rf6 rf7
  3. rv1 rv2 rv3 rv4 rv5 rv6 rv7
  4. other random lines

where

  1. 2., 3. and 4. always come after a line like 1.
  2. rv7 (random value 7) is either foo or bar

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:

  1. 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.
  2. 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.

5
  • You have a data file where field 6 might be missing? Are the fields delimited by exactly one space (or tab), so that when field 6 is missing there are two spaces between field 5 and the last field?
    – roaima
    Sep 11, 2020 at 16:33
  • Its not at all clear what you;re trying to do. Is 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.
    – Ed Morton
    Sep 11, 2020 at 16:33
  • Technically these are the .csv fields. The output would be many lines of 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.
    – Lev
    Sep 11, 2020 at 16:40
  • 1
    @roaima There isn't in the input, but there is in the output, using the awk command I wrote above.
    – Lev
    Sep 11, 2020 at 16:43
  • idk what you mean by 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.
    – Ed Morton
    Sep 11, 2020 at 17:14

1 Answer 1

0
$ awk 'NR==2{print p, $2, $1, $7, $5, $6} {sub(/:/,""); p=$1}' file
project rf2 rf1 rf7 rf5 rf6

$ awk -v OFS=',' 'NR==2{print p, $2, $1, $7, $5, $6} {sub(/:/,""); p=$1}' file
project,rf2,rf1,rf7,rf5,rf6

If that's not all you need then edit your question to clarify your requirements and provide more useful and accurate sample input/output.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .