Move sets of files with date encoded names - unix

I have a set of files that have dates in them.
lets call them:
a20120528_120001.log
b20120528_120003.log
(name)(year)(month)(day)_(hour)(minute)(second).log
It is easy enough to move these two files simultaneously by doing:
mv *20120528_12* file/
But now I have a situation where I want to move several hours worth of files in the same day ie:
a20120528_120001.log
b20120528_120003.log
a20120528_130001.log
b20120528_130003.log
a20120528_140001.log
b20120528_140003.log
Now if i wanted to transfer all of them i could just do the day:
mv *20120528* file/
but what can I do if I only want to move hours 12 and 13, but exclude 14.
Please note this will need to be generic enough that i can input the date, because this will extend to be used across multiple days where there are 24 logs per day and several (between 3-8) will be excluded from each day.
How would I do this?

You can use ranges:
mv *20120528_1[23]* file/
For excluding everything from 3-8, go with the slightly more complicated:
mv *20120528_{0[0-29],[12]*}*
[0-29] breaks down to the range 0-2 and 9.
{A,B} expands to A or B.

This is a good question because in Bash, filename expansion uses only *, ?, and [. So you can deal with hours 12 and 13 only with the following:
[ab]20??????_1[23]????.log
Note that this takes you up to the year 2099 only; adjust accordingly if that bothers you.
But if you need a general range of values, you will need multiple commands. If you have hours 00 through 23 and you want to exclude 03 through 08, I think you will need the following:
[ab]20??????_0[0129]????.log
[ab]20??????_1?????.log
[ab]20??????_2[0123]????.log
Of course you can also write a script to generate the proper patterns, using something more complex than filename globbing. Not sure if you need that much complexity, though.

you may use THIS BATCH FILE
for /R SOURCEPATH %%f in (20120528_12,20120528_13) do MOVE "%%f" DESTINATION PATH

Related

Extract exactly one file (any) from each 7zip archive, in bulk (Unix)

I have 1,500 7zip archives, each archive contains 2 to 10 files, with no subdirectories.
Each file has the same extension, however the filename varies.
I only want one file out of each archive, but I'd like to perform this in bulk. I do not care which file is taken out, as long as only one file is taken out. It can be the first file, the newest, the biggest, the smallest, it doesn't matter.
Here's an example:
aa.7z {blah 56.smc, blah 57.smc, 1 blah 58.smc}
ab.7z {xx.smc, xx 1.smc, xx_2.smc}
ac.7z {1.smc}
I want to run something equivalent to:
7z e *.7z # But somehow only extract one file
Thank you!
Ultimately my solution was to extract all files and run the following in the directory:
for n in *; do echo "$n"; done > files.txt
I then imported that list into excel, and split the files by a special character that divided the title of the file with the qualifying data inside the filename (for example: Some Title (V1) [X2].smc), specifically I used a brackets delimiter.
Then I removed all duplicates, leaving me with only one edition of each from the zip. I finally remerged the columns (unfortunately the bracket was deleted during the splitting so wrote a function to add it back on the condition of whether there was content in the next column) and then resaved files.txt, after a bit of reviewing StackOverflow for answers, deleted files based on an input file (files.txt). A word of warning on this, spaces in filenames cause problems with rm and xargs so I had to encapsulate the variable with quotes.
Ultimately this still didn't serve me well enough so I just used a different resource entirely.
Posting this answer so others who find themselves in a similar predicament find an alternative resolution.

Modify the dates in a huge file (around 1000 rows) using script

I have a requirement in which I need to subtract x number of days from dates present in a delimited file if the date exists excluding the first and last row. If the date does not exist in the specified field, ignore the same.
For example, aaa.txt contains
header
abc|20160431|dhadjs|20160325|hjkkj|kllls
ddd||dhajded|20160320|dwdas|hfehf
footer
I want the modified file to have the dates subtracted by 10 days. Something like below:-
header
abc|20160421|dhadjs|20160315|hjkkj|kllls
ddd||dhajded|20160310|dwdas|hfehf
footer
I don't want to use a programming language like Java to read the file but rather use a scripting language on unix. Any suggestions on how this can be done?

How can I enter a command that is over 256 Characters Long in IRIX

I connect to different types of computers every day. When I Telnet in, the first thing I do is run a command line script that is about 1150 characters long. I have no problem with Linux based systems, but if it is Unix based (ie IRIX), then my command is truncated at ~256 Chars.
The Final result of the Command will be a data dump (the results of the commands) to the Telnet window. This data will then be copied and pasted into a tool for analysis. Also the Command string that is being entered is a series of Commands (mostly egreps) separated by semi-colons, but when combined together it gets very long.
I need to be able to enter all 1150 Chars on the command line. The systems I access are not mine, So I need to be as Benign as possible when interacting with them.
Your Help is appreciated.
If its a parameter list thats making the command that long then xargs is your friend
I'm not sure if this is the answer you're looking for, but as you stated in your comment, all of the commands are less than 256 characters. So, you can break the commands up into 5-6 groups being sure to only separate at the semi-colon (not at pipes). Then execute each group in sequence. It's more work if your use to just copying and pasting, but not much if you already have the groups created in a text file.

Striping out time components for data in a csv file with | seperated variables

A bit new to UNIX but I have a question with reagrds altering csv files going into a datafeed.
There are a few | seperated columns where the date has come back as (for example)
|07-04-2006 15:50:33:44:55:66|
and this needs to be changed to
|07-04-2006|
It doesn't matter if all the data gets written to another file. There are thousands of rows in these files.
Ideally, I'm looking for a way of going to the 3rd and 7th piped columns and taking the first 10 characters and removing anything else till the next |
Thanks in advance for your help.
What exactly do you want?
You can replace |07-04-2006 15:50:33:44:55:66| by |07-04-2006| using File IO.
This operates on all columns, but should do unless there are date columns which must not be changed:
sed 's/|\(..-..-....\) ..:..:..:..:..:..|/|\1|/g'
If you want to change the files in place, you can use sed's -i option.

Sequence number inside a txt file in UNIX

I want to generate a unique sequence number for each row in the file in unix. I can not make identity column in database as it has some other sources which also inserts data in it. I tried using NR number in awk but since i have filters in my script it may skip rows in the file so i may not get sequential numbers.
my requirements are - This sequence number needs to be persistent since everday i would receive this file and should start from where i left of. also the number needs to be preceded by "EMP_" for each line in the file.
Please suggest.
Thanks in advance.
To obtain unique id in UNIX you may use file to store and read the value. however this method is so tedious and require mechanism on file IO locking. the easiest way is to use date time to obtain unique id example :
#!/bin/sh
uniqueVal = `date '+%Y%m%d%H%M%S'`

Resources