?
Solved

Concatenate files into one single file

Posted on 2014-02-26
10
Medium Priority
?
457 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
  • 2
  • +2
10 Comments
 
LVL 75

Expert Comment

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

Author Comment

by:baralp
ID: 39889205
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
ID: 39889210
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
Get proactive database performance tuning online

At Percona’s web store you can order full Percona Database Performance Audit in minutes. Find out the health of your database, and how to improve it. Pay online with a credit card. Improve your database performance now!

 
LVL 9

Expert Comment

by:Sean
ID: 39889223
#!/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
ID: 39889383
@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
 
LVL 9

Assisted Solution

by:Sean
Sean earned 668 total points
ID: 39889420
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
ID: 39889438
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
ID: 39889446
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 668 total points
ID: 39889754
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 664 total points
ID: 39890583
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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Attention: This article will no longer be maintained. If you have any questions, please feel free to mail me. jgh@FreeBSD.org Please see http://www.freebsd.org/doc/en_US.ISO8859-1/articles/freebsd-update-server/ for the updated article. It is avail…
Linux users are sometimes dumbfounded by the severe lack of documentation on a topic. Sometimes, the documentation is copious, but other times, you end up with some obscure "it varies depending on your distribution" over and over when searching for …
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
Learn how to get help with Linux/Unix bash shell commands. Use help to read help documents for built in bash shell commands.: Use man to interface with the online reference manuals for shell commands.: Use man to search man pages for unknown command…
Suggested Courses
Course of the Month11 days, 2 hours left to enroll

770 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