I have tried
sed -n '/- name: string/,/^$/p' dire1/main.yml >> dire2/README.md
This does not add anything, I also want it to add it to the first line of dire2/readme.md if readme.md does not already include '- name: string' in the first line so as not to get duplicates.
For example if a file includes
- name: first step
uses: message
- name: second step
I'd want
- name: first step
uses: message
to be included in the first line of readme.md. But if readme.md already has this in the first line, it should not be added.
Another example The file is a longer file
name: build
another line here
yet another line
- name: title here
another line
another line
- name: another title
more lines here
more lines
- name: third title
more lines
more lines
In this example, i'd want it to populate the readme.md file with
- name: title here
another line
another line
or any one of the other paragraphs beginning with '- name ' and ending at a blank line.
This all has to be searched from one file and added to the beginning of readme file if it doesn't already exist
Thank you for any help
CodePudding user response:
I build a quick awk solution, works for your example. First, the 3 example files:
kent$ head *.yml
==> main.yml <==
- name: first step
uses: message
- name: second step
==> readme1.yml <==
key:
whatever2: 200
==> readme2.yml <==
- name: exists
users: whatever
- key:
whatever2: 100
As you can see, the readme1.yml
doesn't contain the first key (- name: xxx
) in the main.yml. While the readme2.yml
contains the key already.
Now the awk command: (the two commands are the same, only the input params are different):
$ awk -F':' -v RS="" 'NR==FNR{if(NR==1){k=$1;a[k]=$0;}next;} FNR==1 && !($1 in a){print a[k]}1' main.yml readme1.yml
- name: first step
uses: message
key:
whatever2: 200
$ awk -F':' -v RS="" 'NR==FNR{if(NR==1){k=$1;a[k]=$0;}next;} FNR==1 && !($1 in a){print a[k]}1' main.yml readme2.yml
- name: exists
users: whatever
- key:
whatever2: 100
So, if you check the outputs above, it seems they are what you expected.
CodePudding user response:
With GNU sed you can extract the blocks and quit after the first:
sed -n '/^- name.*$/,/^$/{p;/^$/q}' file
- name: title here
another line
another line