Concatenate all the files in folder on timestamps


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Concatenate all the files in folder on timestamps
# 1  
Old 08-10-2007
Question Concatenate all the files in folder on timestamps

Hi Gurus, Experts,

I am facing a problem to concatenate all the files based on timeStamps.
The Problem is like this .

The are many files in a folder Which are of the below types

Ack_to_MDS_20070809141159.xml
Ack_to_MDS_20070809141157.xml
Ack_to_MDS_20070809141155.xml
Ack_to_MDS_20070809141151.xml
Ack_to_MDS_20070809141149.xml
Ack_to_MDS_20070809141148.xml
.
.
.
.

The files are of format Ack_to_MDS_YYYYMMDDHHMMSS.xml

The Content of the files are like this


<ACKNOWLEDGEMENTS>
<ACKNOWLEDGEMENT>
<ACK_TYPE>L</ACK_TYPE>
<ACK_ID>

<PROPERTY1>SPC1238</PROPERTY1>
<PROPERTY2>4</PROPERTY2>
<PROPERTY3>SHIP_TO</PROPERTY3>

</ACK_ID>
<ACK_STATUS>S</ACK_STATUS>
<ACK_COST>10.52</ACK_COST>
<ACK_ERROR_MESSAGE>NO ERROR</ACK_ERROR_MESSAGE>
</ACKNOWLEDGEMENT>
<ACKNOWLEDGEMENT>
<ACK_TYPE>L</ACK_TYPE>
<ACK_ID>

<PROPERTY1>NCB10039</PROPERTY1>
<PROPERTY2>-1</PROPERTY2>
<PROPERTY3>BILL_TO</PROPERTY3>

</ACK_ID>
<ACK_STATUS>F</ACK_STATUS>
<ACK_COST>100.55</ACK_COST>
<ACK_ERROR_MESSAGE>ERROR</ACK_ERROR_MESSAGE>
</ACKNOWLEDGEMENT>

</ACKNOWLEDGEMENTS>


Now My Requirement is In Each of the files I need to remove the
<ACKNOWLEDGEMENTS> and </ACKNOWLEDGEMENTS>
Which appear in the first and the last line of the file.

Later I need to cat the files based on the timestamps present in the file format.
The Sort order is the oldest file contents needs to be at the top and the Latest fileat the end.

The Append the Tags <ACKNOWLEDGEMENTS> and </ACKNOWLEDGEMENTS> to the first line and last line of the concatenated file.

Please Gurus
Need Help and Advice.

Thanks & Regards
Srikanth GR

Last edited by srikanthgr1; 08-10-2007 at 02:42 AM..
# 2  
Old 08-10-2007
Does the order in which the files are concatenated matter? If not (you don't mind plain alphabetical order), you can use this simple pseudocode:

Code:
echo "<ACKNOWLEDGEMENTS>" > new_file_name.xml
for file in *; do
   grep -v "<.*ACKNOWLEDGEMENTS>" $file
done >> new_file_name.xml
echo "</ACKNOWLEDGEMENTS>" >> new_file_name.xml

You may have to make changes in the selection of the files for the for loop (you may have other criteria, like checking only for today's files).

-EDIT
Untested
# 3  
Old 08-10-2007
Hi BlowTorch,

Thanks for you Help.

The File order is like this .
The Oldest File content needs to be concatenated at the begining and the latest ones at end.
It will be in the descending order of the timestamp.

I found out another method like First I will remove the Tags <ACKNOWLEDGEMENTS> and </ACKNOWLEDGEMENTS> in all the files and later concatenate them.

Concatenating the files are fine but the problem is How to use the sort command


Later I need to cat the files based on the timestamps present in the file format i.e Ack_to_MDS_YYYYMMDDHHMMSS.xml.
The Sort order is the oldest file contents needs to be at the top and the Latest file at the end.


i.e. If theese are the files

Ack_to_MDS_20070809141159.xml
Ack_to_MDS_20070809141157.xml
Ack_to_MDS_20070809141155.xml
Ack_to_MDS_20070809141151.xml
Ack_to_MDS_20070809141149.xml
Ack_to_MDS_20070809141148.xml


Then The Sorted Order is
Ack_to_MDS_20070809141148
Ack_to_MDS_20070809141149
Ack_to_MDS_20070809141151
Ack_to_MDS_20070809141155
Ack_to_MDS_20070809141157
Ack_to_MDS_20070809141159

Later The concatenation order is also as the above .


Then I will Append the Tags <ACKNOWLEDGEMENTS> and </ACKNOWLEDGEMENTS> to the first line and last line of the concatenated file.
# 4  
Old 08-10-2007
The script I've posted should cat the files in the order that you need.
# 5  
Old 08-10-2007
Quote:
Originally Posted by blowtorch
Does the order in which the files are concatenated matter? If not (you don't mind plain alphabetical order), you can use this simple pseudocode:

Code:
echo "<ACKNOWLEDGEMENTS>" > new_file_name.xml
for file in *; do
   grep -v "<.*ACKNOWLEDGEMENTS>" $file
done >> new_file_name.xml
echo "</ACKNOWLEDGEMENTS>" >> new_file_name.xml

You may have to make changes in the selection of the files for the for loop (you may have other criteria, like checking only for today's files).

-EDIT
Untested
to get the right order:
Code:
#!/bin/ksh
echo "<ACKNOWLEDGEMENTS>" > new_file_name.xml
for file in $(nawk -F'[_.]' '{print $(NF-1) OFS $0}' Ack_to_MDS_[0-9][0-9]*.xml | sort -n | cut -d' ' -f2-); do
   grep -v "<.*ACKNOWLEDGEMENTS>" $file
done >> new_file_name.xml
echo "</ACKNOWLEDGEMENTS>" >> new_file_name.xml

# 6  
Old 08-10-2007
Hi srikanthgr1 ,

Let me clarify about your files content, whether all the file has same content or different?

if all the has some content of information i will suggest you one solution,

do the following steps:

1. check the line of the each and every files, if equal
2. set the line number of the every files
eg: suppose your file has 15 lines, you want remove the 14 th and 15 th lines from the file and store it in the new file.
3. sed -n '1,13p' <filename.extn> > Newfile

let me know if it's through error.

Regards,
Siva.P
Bangalore
# 7  
Old 08-10-2007
Quote:
Originally Posted by vgersh99
to get the right order:
Code:
#!/bin/ksh
echo "<ACKNOWLEDGEMENTS>" > new_file_name.xml
for file in $(nawk -F'[_.]' '{print $(NF-1) OFS $0}' Ack_to_MDS_[0-9][0-9]*.xml | sort -n | cut -d' ' -f2-); do
   grep -v "<.*ACKNOWLEDGEMENTS>" $file
done >> new_file_name.xml
echo "</ACKNOWLEDGEMENTS>" >> new_file_name.xml

vgersh99, can you please explain the nawk? Didn't get it...
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Request for Shell script to move files from Subfolder to Parent folder and delete sub folder

Hi Team, I am new to shell script and there is a requirement where files should be moved from Subfolder to parent folder. Eg: parent folder --> /Interface/data/test/IN Sub folder -->/Interface/data/test/IN/Invoice20180607233338 Subfolder will be always with timestamp... (6 Replies)
Discussion started by: srivarun15
6 Replies

2. Shell Programming and Scripting

Shell scripting for moving folder specific files into target directory of that country folder.

I need help to write shell script to copy files from one server to another server. Source Directory UAE(inside i have another folder Misc with files inside UAE folder).I have to copy this to another server UAE folder( Files should be copied to UAE folder and Misc files should be copied in target... (3 Replies)
Discussion started by: naresh2389
3 Replies

3. Shell Programming and Scripting

Find matching timestamps in two files.

OK. if the first file is : 3184 2014-07-28 04:15 global.Remote-Access 10.111.8.25 81.245.6.25 tcp 3268 3035 2014-07-28 04:16 global.Remote-Access 10.111.8.12 81.245.6.25 tcp 3268If the second file is: 1 Jul 28 04:12 2014-07-28 id967254(BGC-TLW-Cert) Tunneling: User with IP... (8 Replies)
Discussion started by: fxsme
8 Replies

4. Shell Programming and Scripting

Issues with comparing 2 files timestamps

Hi, Im trying to write a script to first check a directory and if the filename has "ACK" in it do nothing and exit but if it has "ORD" in the filename then compare it with a dummy file created 2 minutes previous and see which one is newer Im getting a few errors which im unsure how to rectofy... (5 Replies)
Discussion started by: 02JayJay02
5 Replies

5. Shell Programming and Scripting

Sorting the files on their timestamps

Hi, The file names are like XXXX_20111101.gz for 01 November 2011, etc. There might be several files for 01 November 2011 and I can find them using find DIR -name *_20111101.gz -exec ls -lt {} \; But I want to sort the listing returned by the above command on the timestamps of the files... (6 Replies)
Discussion started by: sagarparadkar
6 Replies

6. Shell Programming and Scripting

Bash script to copy timestamps of multiple files

Hi, I have a bunch of media files in a directory that have been converted (from MTS to MOV format), so my directory contains something like this: clip1.mts clip1.mov clip2.mts clip2.mov The problem is that the .mov files that have been created have the timestamps of the conversion task,... (2 Replies)
Discussion started by: Krakus
2 Replies

7. UNIX for Advanced & Expert Users

Recursively concatenate files in subdirectories with the same folder name

I'm trying to concatenate files in subdirectories with the same folder name. Say concatenate all the files in the 'current' subdirectories in 'Literature' parent directory. Literature/USA/current/ Literature/Europe/current/ Can anyone help with it? Thanks a lot! (2 Replies)
Discussion started by: joyce007
2 Replies

8. UNIX for Dummies Questions & Answers

recursively concatenate files in subdirectories with same folder name

I'm trying to concatenate files in subdirectories with the same folder name. Say concatenate all the files in the 'current' subdirectories in 'Literature' parent directory. Literature/USA/current/ Literature/Europe/current/ Can anyone help with it? Thanks a lot! (2 Replies)
Discussion started by: joyce007
2 Replies

9. Shell Programming and Scripting

Getting all the .gz files between Two Date/TimeStamps

Hi, I am trying to write a script to ftp and get all the files between two date/time stamps from a archive directory. I have sent an attatchment of my archive directory. With the script I intend to get files for ex: between request.log.2008-08-22-03-53-49.gz &... (3 Replies)
Discussion started by: openspark
3 Replies

10. UNIX for Advanced & Expert Users

Auto copy for files from folder to folder upon instant writing

Hello all, I'm trying to accomplish that if a file gets written to folder /path/to/a/ it gets automatically copied into /path/to/b/ the moment its get written. I thought of writing a shell script and cron it that every X amount of minutes it copies these files over but this will not help me... (2 Replies)
Discussion started by: Bashar
2 Replies
Login or Register to Ask a Question