Solved

Concatenate files into one single file

Posted on 2014-02-26
10
442 Views
Last Modified: 2014-02-26
Hi,
I am trying to concatenate the following individual files into one file based on the current date that is attached to the individual file names.
For eg: There could be several files. One of the file name appears like SVCTURN_ON_SO2_29JAN2014_075144.txt and another as PNDGSVCORD_SO1_29JAN2014_075153.txt and so on..
I need to find a way to concatenate these individual files that includes the current date in the name.
0
Comment
Question by:baralp
  • 3
  • 3
  • 2
  • +2
10 Comments
 
LVL 74

Expert Comment

by:käµfm³d 👽
Comment Utility
Are you familiar with the cat command?
0
 

Author Comment

by:baralp
Comment Utility
yes I am familiar with cat. I can do something like this cat *.txt >> all.txt. But I don't know how to concatenate those files that have current date attached in the file name. For eg. you can see the current date on the file SVCTURN_ON_SO2_26Feb2014_075144.txt that I need to look to concatenate.
0
 
LVL 23

Expert Comment

by:savone
Comment Utility
This should do it:

for i in `ls | grep 29JAN`; do cat $i >> newfile.txt; done


It will find all the files in the current working directory that have 29JAN in them (current date) and then one by one put the contents of that file into newfile.txt.
0
 
LVL 9

Expert Comment

by:Sean
Comment Utility
#!/bin/sh

## date format ##
NOW=$(date)

 
## Backup path ##
BAK="/etc/backup/"
FILE="$BAK/$NOW.log"


for i in /etc/logs/*
do
cat $i >> $FILE
done

This should do close to what you want. May need to change the paths/names etc but just put this into a shell script and run it.
0
 

Author Comment

by:baralp
Comment Utility
@savone
In your command for i in `ls | grep 29JAN`; do cat $i >> newfile.txt; done
Can we replace 29Jan with the current date variable since the file name changes according to the run date?
0
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 9

Assisted Solution

by:Sean
Sean earned 167 total points
Comment Utility
now=$(date +"%m_%d_%Y")

for i in `ls | grep $now`; do cat $i >> newfile.txt; done

This will use the current date.
0
 

Author Comment

by:baralp
Comment Utility
I think now=$(date +"%m_%d_%Y")  would be now=$(date +"%d_%m_%Y") as the file name appears like SVCTURN_ON_SO2_26Feb2014_075144.txt. Correct?
0
 
LVL 9

Expert Comment

by:Sean
Comment Utility
Yes you can change it to match the file.

see reference for the different ways to use the date command.

http://www.cyberciti.biz/faq/unix-linux-appleosx-bsd-shell-appending-date-to-filename/
0
 
LVL 23

Accepted Solution

by:
savone earned 167 total points
Comment Utility
Yes, but it has to be in the format that is in the filename.

Try this:

today=`date +%d%b`; for i in `ls | grep $today`; do echo $i >> newfile.txt; done

This will format the date to look like this: 26Feb

Which matches the examples you provided.
0
 
LVL 48

Assisted Solution

by:Tintin
Tintin earned 166 total points
Comment Utility
Assuming the files contain the date format

ddmmmyyyy

eg:  26Feb2014

then you can simply do

cat *$(date +%d%b%Y)* >concatenated_file
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Little introduction about CP: CP is a command on linux that use to copy files and folder from one location to another location. Example usage of CP as follow: cp /myfoder /pathto/destination/folder/ cp abc.tar.gz /pathto/destination/folder/ab…
Java performance on Solaris - Managing CPUs There are various resource controls in operating system which directly/indirectly influence the performance of application. one of the most important resource controls is "CPU".   In a multithreaded…
Learn how to find files with the shell using the find and locate commands. Use locate to find a needle in a haystack.: With locate, check if the file still exists.: Use find to get the actual location of the file.:
Learn how to navigate the file tree with the shell. Use pwd to print the current working directory: Use ls to list a directory's contents: Use cd to change to a new directory: Use wildcards instead of typing out long directory names: Use ../ to move…

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now