So I have obviously been looking at this problem for too long now as
I am unable to come up with what I know should be a simple solution,
so here goes:
I have a file like so:
I would like to insert prior to the word "three" all items from this
second file with the following contents:
Now my issue is, and I have been using both sed and awk currently,
that after the second line of the new file is read there will of course now be 2 copies of the word "three" but I would like to only insert the final 2 words, ie "two" and "one" prior to only the first occurrence of the word "three" so final file will look like:
Code:
one
two
four
two
one
three
three
four
So here there is now only one of each word from the second file joined to make the new file.
For simple code I have tried something like the following:
Code:
while read line
do
awk -v n=$line '!f && /three/{print n;f++}1' file1 > tmp_file
mv tmp_file file1
done < file2
Now this works but seems very clumsy to me. There is obviously a better sed and / or awk out there.
Look forward to replies