0

Thanks a lot! for providing very good solution for the Question to my earlier post - Insert content of file at specific line number of another file in LINUX/BASH. Now, I have slightly different requirement. Now I want to insert content of 'tempFile' into another file 'File1' before first occurrence of statement '@type ABCD'. I tried with following sed command

 sed -n -i -e '/@type ABCD/r tempFile' -e 1x -e '2,$(x;p)' -e '$(x;p)' File1

However, the above script inserts content of tempFile in File1 multiple times as mentioned below.

File1
 tempFile 01
 tempFile 02
 tempFile 03
 tempFile 03
 @type ABCDF #---Insert 'tempfile' content before First occurrence '@type ABCDF'
File2
File3
 tempFile 01
 tempFile 02
 tempFile 03
 tempFile 03
 @type ABCDF
File4
 tempFile 01
 tempFile 02
 tempFile 03
 tempFile 03
 @type ABCDF
File5

Please help to insert content of tempFile in File1 only before first occurance of '@type ABCD'.

Expected output

File1
 tempFile 01
 tempFile 02
 tempFile 03
 tempFile 03
 @type ABCDF #---Insert 'tempfile' content before First occurrence '@type ABCDF'
File2
File3
 @type ABCDF
File4
 @type ABCDF
File5

Thanks in advance

oguz ismail
  • 1
  • 16
  • 47
  • 69
Anindya Kar
  • 45
  • 1
  • 7
  • Why are you back to trying to use sed when you saw in the answer to your previous question that awk is the tool to use for general purpose text manipulation? In any case, [edit] your question to show the sample input (contents of tempFile and File1) in addition to the expected output. – Ed Morton May 24 '20 at 15:18

1 Answers1

0

This can be done with one sed command, but it's masochistic.

Instead, I'd do it this way:

sed '/@type ABCD/,$d' File1 > temp
cat tempFile >> temp
sed '/@type ABCD/,$!d' File1 >> temp
mv temp File1
Beta
  • 96,650
  • 16
  • 149
  • 150