Copy(append ) the contents of file1 and file2 to file3


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Copy(append ) the contents of file1 and file2 to file3
# 1  
Old 12-10-2014
Copy(append ) the contents of file1 and file2 to file3

Platform : Oracle linux 6.5

I have two log files with the following contents

Code:
[root@tblmanh236 test]# ls -l
total 8
-rw-r--r--. 1 root root 75 Dec 10 20:55 myLogfile1.log
-rw-r--r--. 1 root root 51 Dec 10 20:57 myLogfile2.log

[root@tblmanh236 test]#
[root@tblmanh236 test]# cat myLogfile1.log
hello world
jaded zombies acted quaintly but kept driving the oxen forward

[root@tblmanh236 test]#
[root@tblmanh236 test]#
[root@tblmanh236 test]#
[root@tblmanh236 test]#
[root@tblmanh236 test]# cat myLogfile2.log
The quick brown fox jumped over the lazy dog
hello
[root@tblmanh236 test]#

I want the contents of myLogfile1.log and myLogfile2.log to be copied(appended) to a single logfile called named mySingleLogfile.log .
How can I do this ?
# 2  
Old 12-10-2014
Hello kraljic,

You can try following for same.
Code:
cat myLogfile1.log myLogfile2.log > Output_file

Thanks,
R. Singh
This User Gave Thanks to RavinderSingh13 For This Post:
# 3  
Old 12-10-2014
In this specific case (only the required two files are in the directory)
Code:
cat * > mySingleLogfile.log

should do the thing. If there were other files than the two you want to merge, then RavinderSingh13's approach might be the right thing.

I think it could be even abbreviated like this
Code:
cat myLogfile[12].log > mySingleLogfile.log

Up to 9 files cat myLogfile*.log > mySingleLogfile.log should work too, but if you had, say 15 logfiles, e.g. myLogfile1.log myLogfile2.log .. myLogfile15.log, then cat myLogfile*.log > output.log will likely produce wrong output (Wrong order! Compare the output of ls myLogfile*.log and ls myLogfile*.log | sort -V) .... In this case you can try this, provided your sort supports the -V option:
Code:
ls myLogfile*.log | sort -V | xargs -I{} cat {} > mySingleLogfile.log

Hope this helps.
This User Gave Thanks to junior-helper For This Post:
# 4  
Old 12-10-2014
Thank You Ravinder, Junior-Helper

cat myLogfile[12].log > mySingleLogfile.log solution suits my requirements.

In my case, the logfile names are assigned to variables first
Code:
[root@tblmanh236 test]# export LOG1=myLogfile1.log
[root@tblmanh236 test]# export LOG2=myLogfile2.log
[root@tblmanh236 test]# export LOG3=myLogfile3.log

And the below works well
Code:
[root@tblmanh236 test]# cat $LOG1 $LOG2 $LOG3 > mySingleLogfile.log
[root@tblmanh236 test]#

But, When I am trying to do the equivalent of myLogfile[12].log > mySingleLogfile.log with variables, I am getting the following error because of the [ ] brackets around 123 , I guess

Code:
[root@tblmanh236 test]# cat $LOG[123] > mySingleLogfile.log
cat: [123]: No such file or directory
[root@tblmanh236 test]#
[root@tblmanh236 test]# cat '$LOG[123]' > mySingleLogfile.log
cat: $LOG[123]: No such file or directory
[root@tblmanh236 test]#
[root@tblmanh236 test]#
[root@tblmanh236 test]# cat "$LOG[123]" > mySingleLogfile.log
cat: [123]: No such file or directory
[root@tblmanh236 test]#

## Escaping the brackets didn't work either
[root@tblmanh236 test]# cat $LOG\[123\] > mySingleLogfile.log
cat: [123]: No such file or directory

How can I get this working ?
# 5  
Old 12-10-2014
Quote:
[root@tblmanh236 test]# cat $LOG[123] > mySingleLogfile.log
cat: [123]: No such file or directory
I am getting the following error because of the [ ] brackets around 123 , I guess
No. Let me explain that: It seems like you exported variables LOG1 LOG2 and LOG3, but here you are "catting" $LOG[123]. Most likely, LOG is not defined at all and hence empty, and only [123] remains and cat cannot find a file with name [123]. The shell treated it like a literal filename [123] because the shell was not able to expand it to something appropriate. [123] alone would match only files with name 1 2 and 3

One potential solution:
Code:
export LOG=myLogfile
cat $LOG[123].log > mySingleLogfile.log

Is it viable?

Edit: This should work too:
Code:
export LOG=myLogfile[123].log
cat $LOG > mySingleLogfile.log


Last edited by junior-helper; 12-10-2014 at 01:29 PM..
This User Gave Thanks to junior-helper For This Post:
# 6  
Old 12-10-2014
Thank You. It is viable. I have multiple log file variables like LOG1 , LOG2, ... declared in my scripts, I guess I will have to change it to what you've suggested If there is no other way

Code:
export LOG=myLogfile
cat $LOG[123].log > mySingleLogfile.log

You are not that junior. Are you ? Smilie
# 7  
Old 12-10-2014
Quote:
Originally Posted by kraljic
I have multiple log file variables like LOG1 , LOG2, ... declared in my scripts, I guess I will have to change it to what you've suggested If there is no other way
Not sure, I don't know you script and don't know how you want to handle it exactly, but if you wanted to cat files already stored in LOG1 LOG2 and LOG3, (without doing the stunt with LOG I mentioned before), with bash you could try this:
Code:
for i in {1..3}; do
 x=LOG$i
 cat ${!x} >>mySingleLogfile.log
done

I hope this doesn't complicate the situation further Smilie
Quote:
You are not that junior. Are you ? Smilie
Smilie

Edit: Yet another option/variant:
Code:
for i in $LOG1 $LOG2 $LOG3; do
 cat $i >>mySingleLogfile.log
done


Last edited by junior-helper; 12-10-2014 at 02:18 PM..
This User Gave Thanks to junior-helper For This Post:
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Programming

PYTHON COPY Contents of file1 into a specific entry in file2

file1 cat dog fish file2 This is a bunch of lines <!-- INSERT ANIMALS HERE --> horse cheetah post results file2 This is a bunch of lines <!-- INSERT ANIMALS HERE --> cat dog fish horse (1 Reply)
Discussion started by: TY718
1 Replies

2. Shell Programming and Scripting

awk or any other means to find IP (File1 / MAC (File2)) entries and putting them on File3

Hi everyone, I would like to complete the following could you please find some time and help me to achieve below: File 1 has a list of IP address (more than 1k) File1:1.1.1.1 2.2.2.2 1.1.1.2 3.3.3.3 2.3.3.2File 2 has content like this:Internet 11.165.91.244 0 Incomplete ... (4 Replies)
Discussion started by: redred
4 Replies

3. Shell Programming and Scripting

How to append two files(file1 and file2) Please help me?

Hi I have two files file1 and file2 File1 10,000 entries:It has 3 columns below. conn=232257 client=xxx.xxx.xx.xxx:60491 protocol=LDAP File2 has 500 entries It has two columns. conn=232257 dn="uid=xxxx,ou=xxxx,ou=xxxx,dc=xxxxx,dc=xxxx" conn=232398... (10 Replies)
Discussion started by: buzzme
10 Replies

4. Shell Programming and Scripting

awk read in file1, gsub in file2, print to file3

I'm trying to use awk to do the following. I have file1 with many lines, each containing 5 fields describing an individual set. I have file2 which is a template config file with variable space holders to be replaced by the values in file1. I would like to substitute each set of values in file1 with... (6 Replies)
Discussion started by: msmehaffey
6 Replies

5. Shell Programming and Scripting

Performance issue with fgrep -vf file1 file2>file3

Hi all, My requirement is that i have two files file1 and file2. file1 content is present in file2. i want to have a final which will have file2 contents but not the contents of file1 so I am running below command. fgrep -vf file1 file2>file3 no of records in file1 is 65282 no of records... (10 Replies)
Discussion started by: Lakshman_Gupta
10 Replies

6. UNIX for Dummies Questions & Answers

if matching strings in file1 and file2, add column from file1 to file2

I have very limited coding skills but I'm wondering if someone could help me with this. There are many threads about matching strings in two files, but I have no idea how to add a column from one file to another based on a matching string. I'm looking to match column1 in file1 to the number... (3 Replies)
Discussion started by: pathunkathunk
3 Replies

7. Shell Programming and Scripting

append text from file1 to the end of each line in file2

hi; my file2.txt:portname=1;list=10.11;l- portname=2;list=10.12;l- portname=3;list=10.13;l- ... my file1.txt:;"{'sector=%27'}"\&> so; i want to see:portname=1;list=10.11;l-;"{'sector=%27'}"\&> portname=2;list=10.12;l-;"{'sector=%27'}"\&> portname=3;list=10.13;l-;"{'sector=%27'}"\&>... (4 Replies)
Discussion started by: gc_sw
4 Replies

8. UNIX for Dummies Questions & Answers

cat file1 file2 > file3

file1 has pgap500 500 file2 has bunch of data cat file1 file2 > file3 cp file2 file3.dat then vi pgap500 500 onto 1st line compare file3 and fil3.dat, they are not the same. any idea ? the 1st line, i want to put pg500 xxx ---------- Post updated at 07:35 AM ---------- Previous... (2 Replies)
Discussion started by: tjmannonline
2 Replies

9. UNIX for Advanced & Expert Users

print contents of file2 for matching pattern in file1 - AWK

File1 row is same as column 2 in file 2. Also file 2 will either start with A, B or C. And 3rd column in file 2 is always F2. When column 2 of file 2 matches file1 column, print all those rows into a separate file. Here is an example. file 1: 100 103 104 108 file 2: ... (6 Replies)
Discussion started by: i.scientist
6 Replies

10. UNIX for Dummies Questions & Answers

echo "ABC" > file1.txt file2.txt file3.txt

Hi Guru's, I need to create 3 files with the contents "ABC" using single command. Iam using: echo "ABC" > file1.txt file2.txt file3.txt the above command is not working. pls help me... With Regards / Ganapati (4 Replies)
Discussion started by: ganapati
4 Replies
Login or Register to Ask a Question