Shell script Sed read one file and write to another only on the first occurrence of a string

1

Good afternoon,

I'm trying to read from an X file and write to a Y file when a specific String appears. But I just want to do the inclusion once. Example I'm trying to do:

sed -i '/, pasta/r teste.txt' Report.html

This command is reading all strings, "folder," and it is including the contents of the test.txt file underneath. But I want to make it include only the first occurrence of the string, "folder". A command like:

sed -i '0,/, pasta/r teste.txt' Report.html

The syntax above is wrong, how would it be correct to include the contents of the file only in the first occurrence of the string, "folder"?

    
asked by anonymous 01.12.2015 / 17:22

2 answers

1

If it's just to find an occurrence, without making substitutions, or if there are too many conditions for this match to be true, I'd use grep .

grep -o -a -m 1  -h  "pasta" teste.txt > report.html

Parameters:

-o = Show only the search word, if you remove it, show the entire line of the match.

-a = Process the file as if it were text.

-m 1 = Stop searching again after the first real match

-h = Does not show the name of the file where the match was found.

    
03.12.2015 / 15:41
0
perl -p0e  's/(, pasta)/"$1\n" . 'cat teste.txt'/e ' Report.html

that is: it replaces the first occurrence of " , pasta " by the result of

eval("$1\n" . 'cat teste.txt')

( perl -0 ... file loads the entire file; s/ / / only makes a substitution)

    
04.12.2015 / 18:20