Adding a file at a particular place in another file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Adding a file at a particular place in another file
# 1  
Old 12-18-2008
Adding a file at a particular place in another file

Hi ,

i want to add the content of a file for example :

toto
tata
titi

at a particuler place in a file for example the line before the line containing "EXPLOIT" :

## Traitement des logs HTTP (HTTP uniquement)
#
#
## Purge des fichiers logs (Tout type de serveurs)
#
#
toto
tata
titi
############################### PARTIE DES EXPLOIT

i tried with SED and AWK with no success !

Someonecan help me please ?

Thanks in advance
Christian
# 2  
Old 12-18-2008
Code:
root@isau02:/data/tmp/testfeld> cat text
toto
tata
titi
root@isau02:/data/tmp/testfeld> cat infile
## Traitement des logs HTTP (HTTP uniquement)
#
#
## Purge des fichiers logs (Tout type de serveurs)
#
#
############################### PARTIE DES EXPLOIT
root@isau02:/data/tmp/testfeld> awk 'NR==FNR { _[$1]=$1; next} /EXPLOIT/ {x=$0; for (e in _) {print _[e]} } {print}' text infile
## Traitement des logs HTTP (HTTP uniquement)
#
#
## Purge des fichiers logs (Tout type de serveurs)
#
#
tata
toto
titi
############################### PARTIE DES EXPLOIT

Edit:
Another way:
Code:
awk '/EXPLOIT/ {x=$0; while ( getline < "text" ) {print} print x } {print}' infile| sed '$d'

Though the sed in the end is not pretty Smilie

Last edited by zaxxon; 12-18-2008 at 10:33 AM..
# 3  
Old 12-18-2008
Thank you for your help !

Christian
# 4  
Old 12-18-2008
Or:

Code:
awk '/EXPLOIT/ {                                                                            
  while ((getline _ < "include") > 0)
    print _
}1' infile

# 5  
Old 12-18-2008
..simple..

$sed '/EXPLOIT/i\
toto\
tata\
titi' file
# 6  
Old 01-09-2009
hi

i use with success the following command line :
awk -F [§] 'NR==FNR { filein[$1]=$1; next} /Audit du serveur/ {x=$0; for (e in filein) {print filein[e]} } {print}' /inputtext /inputfile > /ouputfile

but with the following text to insert :
# test insert
0,30 8-17 * * 1-5 . ~/.profile > /dev/null 2>&1;
#
#

i got :

0,30 8-17 * * 1-5 . ~/.profile > /dev/null 2>&1;
# test insert
#

the lines are not in the good order and one line with "#" is suppressed.

thanks
Christian
# 7  
Old 01-12-2009
Hello

anybody can help me i's quite urgent , please !

Christian
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Adding to an array in an external file, and adding elements to it.

I have an array in an external file, "array.txt", which contains: char *testarray={"Zero", "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine"};I want to be able to add an element to this array, and have that element display, whenever I call it, without having to recompile... (29 Replies)
Discussion started by: ignatius
29 Replies

2. Shell Programming and Scripting

Search for a file in all directories and place the file in that directory

Hi All, Daily I am getting the updated file. I have to search for this file in all directories and sub directories. If the file existed in a particular directory then move this updated file to that particular directory. If the file is not existed in any of the directories then place this... (4 Replies)
Discussion started by: ROCK_PLSQL
4 Replies

3. Shell Programming and Scripting

Check file availability and place flag file

I have to check a directory on Linux (via shell Script which I am trying to build) for about 20 different source files with file patterns and if the files are made available in the directory, I should place flag files for which my other ETL jobs are waiting on to kick off. If the source files are... (6 Replies)
Discussion started by: dhruuv369
6 Replies

4. Shell Programming and Scripting

Read from file specific place in file using inode

Hello, I am using tcsh on AIX. I would like to write a script that does the following: 1. given an inode, how do I find exactly the name of the file? I know I could do this using ls -i | grep <inode> but it returns: <inode> <filename>. I need some string manipulation or something to... (1 Reply)
Discussion started by: lastZenMaster
1 Replies

5. Shell Programming and Scripting

Adding new lines to a file + adding suffix to a pattern

I need some help with adding lines to file and substitute a pattern. Ok I have a file: #cat names.txt name: John Doe stationed: 1 name: Michael Sweets stationed: 41 . . . And would like to change it to: name: John Doe employed permanently stationed: 1-office (7 Replies)
Discussion started by: hemo21
7 Replies

6. Shell Programming and Scripting

How to insert a string in a file at specified place?

Hi all, I want to insert a string in a specified place of a very large file. I am giving an example of the task: I love football. Above is a sentence in a file and I want to insert a string "the" between love and football. It is not sure that where this particular line exists. It has to... (4 Replies)
Discussion started by: naw_deepak
4 Replies

7. Shell Programming and Scripting

editing file in place

Is in Unix a tool that could do this: paste -d":" 'file1' 'file2' | special_save "file1" # so that file1 would be treated in a way that would prevent problems with race condition I know it normally doesn't work but is there any intelligent way how to avoid using temporary file? I know methods... (6 Replies)
Discussion started by: MartyIX
6 Replies

8. Shell Programming and Scripting

Jump to a specific place in a file?

If I cat a file And want to go to the first instance of a particular value - what command would I use? And then from that point where I jumped to search for another value - but only search from that point forward not before the file? Thanks~ (2 Replies)
Discussion started by: llsmr777
2 Replies

9. Shell Programming and Scripting

insert file 1 at a specific place of file 2

Hello, I need to search in file2 for class A : public B { and insert right after that the content of file1. I am a bit lost as to which tools (which bash functions, awk...). I should use. Thanks for some directions here. Regards (1 Reply)
Discussion started by: JCR
1 Replies

10. Shell Programming and Scripting

adding lines at special place in crontab

Hi , i export the crontab in a file (i've no root right) and i would add lines from a file at a special place and rewrite the output in an another file. the special place is as this : 45 04 * * * /home/toto.sh > /dev/null 2>&1 # so i would search for toto.sh and insert the lines , the... (5 Replies)
Discussion started by: Nicol
5 Replies
Login or Register to Ask a Question