1

I have a folder full of .yaml files. in each yaml file i have a url arg among other things. im just trying to get a spreadsheet that has the name of the file in the first column and have the value of the url arg in the second column. is there a simple console command that can do this?

in the yaml file it looks essentially like this

args:

- {arg: file_name, value: "testfile"}

- {arg: url, value: "fakesite.com"}

the goal is to have a spreadsheet were those two values are listed side by side for each yaml file in the folder.

  • Can you show a representative example of the yaml file? – Jeff Schaller Feb 23 '18 at 19:24
  • 1
    would a CSV output be acceptable? (Unix filenames can contain almost any character, so it can be tricky to carefully represent them) – Jeff Schaller Feb 23 '18 at 19:36
  • 1
    Why does the sample input look more like JSON than YAML? – Jeff Schaller Feb 23 '18 at 19:37
  • 2
    @JeffSchaller actually YAML does support this JSON-like syntax for inlined arrays – joH1 Feb 23 '18 at 19:39
  • @Teddy What do you mean by "simple command"? Something like yaml2csv my_file.yml? Unlessyou make it a standalone command you would end up using awk, sed and co... – joH1 Feb 23 '18 at 19:41
0

Have a look at yq, which is YAML-wrapper for jq.

0

For one file:

res=$(echo 'args:

- {arg: file_name, value: "testfile"}

- {arg: url, value: "fakesite.com"}' | egrep "file|url")
echo $res
- {arg: file_name, value: "testfile"} - {arg: url, value: "fakesite.com"}

You might need to narrow the grep pattern, to reduce false positives:

egrep -- "- \{arg: (file_name|url), value: ")

So collecting the filenames could be done with find, or with a flat directory just with a for loop. You don't have blanks in filenames or funky characters?

for f in *.yaml; do res=$(egrep -- "- \{arg: (file_name|url), value: " $f); echo $res; done > yaml.csv 
cat yaml.csv 
- {arg: file_name, value: "testfile"} - {arg: url, value: "fakesite.com"}
- {arg: file_name, value: "testfile"} - {arg: url, value: "fakesite.com"}

Or better readable

for f in *.yaml
do 
  res=$(egrep -- "- \{arg: (file_name|url), value: " $f)
  echo $res
done > yaml.csv 

From there, it should just be a step.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.