Вопрос

Let's say I have a file copy_pars.txt, and I want to extract the 6th, 7th, and 8th value on the last line, which looks something like this:

detector xxx xxx xxx xxx XXX XXX XXX xxx xxx

where the XXX values are the ones I want to copy. All values, besides the 1st one "detector", are unknown floats or integers, of unknown length. They are separated by single spaces.

I have a second file, paste_pars.txt in which I want to search for the FIRST line starting with "detector", and replace the 6th, 7th, and 8th values of the line with the ones from copy_pars.txt.

I prefer to use sed or awk, but I'm welcome to suggestions. Thanks.

Это было полезно?

Решение

try this line:

awk 'NR==FNR{f6=$6;f7=$7;f8=$8;next}!f&&/^detector/{f=1;$6=f6;$7=f7;$8=f8}7' copy.txt paste.txt

didn't test, but should work for your requirement.

Другие советы

awk 'NR==FNR{split($0,a);next} !f && /^detector/{for (i=6;i<=8;i++) $i=a[i]; f=1} 1' copy_pars.txt paste_pars.txt
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top