Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win


Concatenate files into one single file

Posted on 2014-02-26
Medium Priority
Last Modified: 2014-02-26
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.
Question by:baralp
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
LVL 75

Expert Comment

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

Author Comment

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.
LVL 23

Expert Comment

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.
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

LVL 11

Expert Comment

ID: 39889223

## date format ##

## Backup path ##

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

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.

Author Comment

ID: 39889383
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?
LVL 11

Assisted Solution

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.

Author Comment

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?
LVL 11

Expert Comment

ID: 39889446
Yes you can change it to match the file.

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

LVL 23

Accepted Solution

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.
LVL 48

Assisted Solution

Tintin earned 664 total points
ID: 39890583
Assuming the files contain the date format


eg:  26Feb2014

then you can simply do

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

Featured Post

Get your Conversational Ransomware Defense e‑book

This e-book gives you an insight into the ransomware threat and reviews the fundamentals of top-notch ransomware preparedness and recovery. To help you protect yourself and your organization. The initial infection may be inevitable, so the best protection is to be fully prepared.

Question has a verified solution.

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

Introduction We as admins face situation where we need to redirect websites to another. This may be required as a part of an upgrade keeping the old URL but website should be served from new URL. This document would brief you on different ways ca…
FreeBSD on EC2 FreeBSD (https://www.freebsd.org) is a robust Unix-like operating system that has been around for many years. FreeBSD is available on Amazon EC2 through Amazon Machine Images (AMIs) provided by FreeBSD developer and security office…
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.:
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Suggested Courses

604 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