Storing timestamp of files in an array


 
Thread Tools Search this Thread
Top Forums UNIX for Beginners Questions & Answers Storing timestamp of files in an array
# 1  
Old 07-27-2017
Storing timestamp of files in an array

Hi,

I have written the below script to get the timestamp of each files and result is as below

Script
Code:
find /home/user -type f -name "*.json" -printf '%Tc %p\n' | awk {'print $1 " " $2 " " $3 " " $4 " " $5 " " $6 " " $7'}

Input
Code:
-rw-r--r-- 1 user domain users    17382 Jul 19 06:10 file1.json
-rw-r--r-- 1 user domain users    20463 Jul 19 06:10 file2.json
-rw-r--r-- 1 user domain users   493244 Jul 19 06:10 file3.json
-rw-r--r-- 1 user domain users    36499 Jul 19 06:10 file4.json
-rw-r--r-- 1 user domain users  2177721 Jul 19 06:13 file5.json

Output
Code:
Wed 19 Jul 2017 06:10:13 AM EDT
Wed 19 Jul 2017 06:10:13 AM EDT
Wed 19 Jul 2017 06:10:14 AM EDT
Wed 19 Jul 2017 06:10:38 AM EDT
Wed 19 Jul 2017 06:13:28 AM EDT

My requirement is store the output into an array.
ie array[1] should hold first row --> Wed 19 Jul 2017 06:10:13 AM EDT
array[2] should hold second row --> Wed 19 Jul 2017 06:10:13 AM EDT
Is it possible to store the entire output in single array through shell script

Thanks

Moderator's Comments:
Mod Comment Please use code tags

Last edited by jim mcnamara; 07-27-2017 at 09:19 AM..
# 2  
Old 07-27-2017
Arrays are declared different ways in different shells. Which OS and shell are you using?

The short answer to your question is yes. If we know the shell we can give you an example.
# 3  
Old 07-27-2017
Please find the answer
/bin/bash

GNU bash, version 4.2.46(1)-release (x86_64-redhat-linux-gnu)

CentOS Linux release 7.3.1611 (Core)
Red Hat Enterprise Linux Server release 7.0 (Maipo)
NAME="CentOS Linux"
VERSION="7 (Core)"

---------- Post updated at 09:11 AM ---------- Previous update was at 08:04 AM ----------

Code:
find /home/user -type f -name "*.json" -printf '%Tc %p\n' | awk {'print $1 " " $2 " " $3 " " $4 " " $5 " " $6 " " $7'}


Last edited by rbatte1; 07-27-2017 at 11:46 AM..
# 4  
Old 07-28-2017
I'm confused. On my Ubuntu 16.04 (Xenial) system the find options you use (i.e. -printf %Tc %p\n produces
Code:
DDD dd mmm YYYY HH:MM:SS ZON /path/to/file

Seven fields. You then pipe through an awk one-liner to print the first seven fields unchanged. Yet your required output shows only the first six fields?

So it occurs to me that the following may work for you (it does on my system with bash)

Code:
IFS=! arr=( $(find . -type f -name "*.json" -printf '%Tc!' ) )

I got rid of the printing of the filepath and use an exclamation mark to separate the timestamps. IFS is similarly modified.

I hope that helps.

Andrew

---------- Post updated at 09:33 AM ---------- Previous update was at 09:21 AM ----------

Quote:
Originally Posted by apmcd47
I'm confused. On my Ubuntu 16.04 (Xenial) system the find options you use (i.e. -printf %Tc %p\n produces
Code:
DDD dd mmm YYYY HH:MM:SS ZON /path/to/file

Seven fields. You then pipe through an awk one-liner to print the first seven fields unchanged. Yet your required output shows only the first six fields?
Okay, after looking at the original post I realise you have an AM/PM marker which I don't. Difference in locale, I suppose. Anyway, my suggestion should still work for you.

Andrew
This User Gave Thanks to apmcd47 For This Post:
# 5  
Old 07-28-2017
This worked, thank you
# 6  
Old 07-28-2017
Would it be safer to use stat to get a known format timestamp then convert it to one that meets your requirements rather than relying on the risks by using ls and being subject to locale etc.?

Perhaps thsi would help:-
Code:
filename=/etc/profile                  # Just as an example
E_seconds=$(stat -c %Y $filename)
timestamp=$(date -d"@$E_seconds" '+%a %d %b %Y %r %p %Z')

echo "Timestamp is \"$timestamp\" for file \"$filename\""

If you build that into a loop called in by your find, that might do it.
(converting them only to display)

As for storing the values as an array, would you be better with an associative array? Have a look at the bash manual page for a description. Basically you could then map the filename to the timestamp as a pair of items keyed by filename (timestamp may not be unique)


Moving on from that, what do you plan to do with the timestamp values? if you are looking to sort them, process them, get items based on a date range, then you would be better storing them as either seconds from the Epoch or using a format such as YYYYMoDDHHMiSS so that it makes sense to numeric comparisons.

Computer code is not easy with human format dates, so one of the above could help tremendously. If you store the value as seconds, then you can display it whenever you need to by calling the date command above.



I hope that this helps,
Robin
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Storing two dimensional array for postprocessing

Hi Community, Would love to get some quick help on below requirement. I am trying to process mpstat output from multiple blades of my server I would like to assign this the output to an array and then use it for post processing. How can I use a two dimensional array and assign these value ... (23 Replies)
Discussion started by: sshark
23 Replies

2. Shell Programming and Scripting

Storing the SQL results in array variables

Requirement 1) I need to execute 15 SQL queries in oracle through linux script. All these query results needs to be stored in array variables. Requirement 2) And these 15 queries needs to be executed in parallel. Requirement 3) Once all the queries executed then the shell script should... (3 Replies)
Discussion started by: Niranjancse
3 Replies

3. Shell Programming and Scripting

Storing command output in an array

Hi, I want keep/save one command's output in an array and later want to iterate over the array one by one for some processing. instead of doing like below- for str in `cat /etc/passwd | awk -F: '$3 >100 {print $1}' | uniq` want to store- my_array = `cat /etc/passwd | awk -F: '$3 >100 {print... (4 Replies)
Discussion started by: sanzee007
4 Replies

4. Programming

C; storing strings in an array

I am trying to get userinput from stdin and store the lines in an array. If i do this: using a char **list to store strings allocate memory to it #include <stdio.h> #include <stdlib.h> int main(int argc, char *argv) { char *prog = argv; char **linelist; int... (5 Replies)
Discussion started by: tornow
5 Replies

5. Shell Programming and Scripting

Storing data in perl 2D array

Respected All, Kindly help me out. I have got file listings in a directory like this: -rw-r--r-- 1 root root 115149 2011-11-17 07:15 file1.stat.log -rw-r--r-- 1 root root 115149 2011-11-18 08:15 file2.stat.log -rw-r--r-- 1 root root 115149 2011-11-19 09:15 file3.stat.log -rw-r--r-- 1... (2 Replies)
Discussion started by: teknokid1
2 Replies

6. Shell Programming and Scripting

Storing blanks in an array in bash

Hi Guys, I have a file which is as follows: 1 2 4 6 7 I am trying to store these values in an array in bash. I have the following script: FILE=try.txt ARRAY=(`awk '{print}' $FILE`) echo ${ARRAY} (3 Replies)
Discussion started by: npatwardhan
3 Replies

7. Shell Programming and Scripting

storing values in a list or array

i have a file called file.txt having the following entries. 2321 2311 2313 4213 i wnat to store these values in a list and i want to iterate the list using loop and store it in another list (1 Reply)
Discussion started by: KiranKumarKarre
1 Replies

8. Shell Programming and Scripting

storing records in awk array

hi i have a file as follows: 1 2 3 4 5 6 i want to store all these numbers in an array using awk.. so far i have: awk '{for(i=1;i<=NR;i++) {a=$1}} END {for(i=1;i<=NR;i++) {printf("%1.11f",a)}}' 1.csv > test however, i am getting all values as zero in the "test" file..... (3 Replies)
Discussion started by: npatwardhan
3 Replies

9. Shell Programming and Scripting

storing variables in array.Please help

Hi All, I need some help with arrays. I need to take input from the user for hostname, username and password until he enters .(dot) or any other character and store the values in the variable array. I would further connect to the hostname using username and passwd and copy files from server to... (7 Replies)
Discussion started by: nua7
7 Replies

10. UNIX for Dummies Questions & Answers

Storing pointer array in C

All .. I am having a pointer array . And trying to store the addess into that pointer array . please see below the problem i faced code: int cnt1; char *t_array; char *f_array; for(cnt1=0; cnt1<1000; cnt1++) { t_array =... (1 Reply)
Discussion started by: arunkumar_mca
1 Replies
Login or Register to Ask a Question