Insert a line into multiple files


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Insert a line into multiple files
# 1  
Old 05-27-2013
Insert a line into multiple files

HI All,

I want to know if it is possible to print the same message but into 2 different files in the same command?

Something like

.
..
...
echo "Text" >> file1 && file2

this is because i creating a script which i use a log but i don't want to duplicate lines of command just to send to 2 different files

Regards
# 2  
Old 05-27-2013
Code:
echo "Text" | tee -a file1 >> file2

# 3  
Old 05-27-2013
Another approach could be:
Code:
for file in file1 file2
do
    echo "Text" >> $file
done

# 4  
Old 05-27-2013
Bartus' method would insert only into 2 files....
try balajesuri's code if you have more than 2 files.
rather than piling up the names in or loop, put the filenames in a txt file and use that in the loop.
this way you dont have to change the code when you have to add new files.
# 5  
Old 05-28-2013
Thnxs all

Thanx to all for your advices, bartus solution is what i am going for since i need to write 2 files only.

But thnxs all for your solutions Smilie
# 6  
Old 05-28-2013
Contrary to what grep_me implied, you can use tee to write to more than just two files. The following example appends to 5 files and discards standard output:
Code:
echo text | tee -a file1 file2 file3 file4 file5 > /dev/null

Regards,
Alister
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Perl command line option '-n','-p' and multiple files: can it know a file name of a printed line?

I am looking for help in processing of those options: '-n' or '-p' I understand what they do and how to use them. But, I would like to use them with more than one file (and without any shell-loop; loading the 'perl' once.) I did try it and -n works on 2 files. Question is: - is it possible to... (6 Replies)
Discussion started by: alex_5161
6 Replies

2. Shell Programming and Scripting

Insert a header record (tab delimited) in multiple files

Hi Forum. I'm struggling to find a solution for the following issue. I have multiple files a1.txt, a2.txt, a3.txt, etc. and I would like to insert a tab-delimited header record at the beginning of each of the files. This is my code so far but it's not working as expected. for i in... (2 Replies)
Discussion started by: pchang
2 Replies

3. Shell Programming and Scripting

Insert Filename into Multiple Files

Hi, We have a folder that has files in the following structure abc.sql def.sql efg.sql . . . xyz.sql I have to find a certain string (say "test") in each file and replace it with the name of the file. For eg. if "test" is present in abc.sql, I want to replace it with "test abc". If... (8 Replies)
Discussion started by: jerome_rajan
8 Replies

4. Shell Programming and Scripting

insert filename into each line of multiple files

I need to insert <filename + comma> into each line of multiple files. Any idea how to script that? Regards, Manu (5 Replies)
Discussion started by: linux.yahoo
5 Replies

5. UNIX for Dummies Questions & Answers

awk, extract last line of multiple files

Hi, I have a directory full of *.txt files. I would like to print the last line of every file to screen. I know you can use FNR for printing the first line of each file, but how do I access the last line of each file? This code doesn't work, it only prints the last line of the last file:BEGIN... (5 Replies)
Discussion started by: Liverpaul09
5 Replies

6. Shell Programming and Scripting

compare three files and insert a blank line at each mismatch

i need to compare three files in unix a.txt b.txt c.txt 1 2 1 2 5 3 4 6 5 5 6 6 i need to insert a blank line in the file if i don't find a match and put the items at the same column if found a match The items in the files... (4 Replies)
Discussion started by: mad_man12
4 Replies

7. Shell Programming and Scripting

how to view last line of multiple files

Dear All, can anybody help me out in generating a command that can be used to view the last line of multiples files. e.g: file 1 contains 100 records file 2 contains 200 records file 3 contails 300 records now i need a command that can be used to display the last line of each... (7 Replies)
Discussion started by: jojo123
7 Replies

8. Shell Programming and Scripting

Insert first line of a file to first column of remaining files

I want to extraxt data from a html table the html file is downloaded from UG / PG Univ - Exam.Results April/May 2008 After processing the html file using sed i got the output like this 11305106082,RANJANI R, CS1251,20,69,P CS1302,20,45,P EC1006,20,52,P EC1351,20,53,P... (5 Replies)
Discussion started by: a_artha
5 Replies

9. Shell Programming and Scripting

IBM Informix Load and Insert with multiple files

Hi , Can you guys please help as I have list of files xaa, xab, xac.........xza for eg in which to perform load the 1st (xaa) and insert into table, then only proceed for the 2nd , 3rd and so forth. In other words, before 1st one finished, 2nd one shall not load and insert to table, and so... (0 Replies)
Discussion started by: rauphelhunter
0 Replies

10. Shell Programming and Scripting

How to Eliminate first line of multiple files

hi gurus ,, I have multiple files with same file pattern..in a particular directory for ex: file20061101.trf file20061102.trf file20061103.trf Each of the file has a header as column names.. My questions is how can i eliminate the first row of each of these... (11 Replies)
Discussion started by: sish78
11 Replies
Login or Register to Ask a Question