0

I have a command that outputs a few lines of text to the console in this format: <FILE_NAME>: <STRING_VALUE>. Example:

FILE1: abc1
FILE2: def2
FILE3: ghi3

I want to create files, FILE1,FILE2 and FILE3 with the contents of abc1,def2 and ghi3 respectively.

Something like

echo "text" >> FILE1

but have it do recursively

What is a single one-liner command to do this?

2
  • What do you mean by "do it recursively" in the edit? Commented Aug 5, 2021 at 2:51
  • 1
    @tink Oh, I thought I add in a bit more detail. What I meant was doing echo "text" >> FILE1 but on multiple lines of output. Commented Aug 6, 2021 at 1:48

1 Answer 1

1

Just pipe your command to awk:

command | awk -F ': *' '{print $2 > $1}'

We define the field separator to be F ': *' (colon followed by any number of spaces).

We print the second field of each row and redirect it to the value of the first field.

$ ls
FILE1  FILE2  FILE3
$ cat FILE*
abc1
def2
ghi3
Sign up to request clarification or add additional context in comments.

2 Comments

This is perfect! Thank you!
Welcome. And I just saw the edit in your question; you can, of course, also use the >> in awk ...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.