extract date from a file name in unix using shell scripting
echo abcd_2014-05-20.tar.gz |grep -Eo '[[:digit:]]{4}-[[:digit:]]{2}-[[:digit:]]{2}'
Output:
2014-05-20
grep
got input as echo
stdin or you can also use cat
command if you have these strings in a file.
-E
Interpret PATTERN as an extended regular expression.
-o
Show only the part of a matching line that matches PATTERN.
[[:digit:]]
It will fetch digit only from input.
{N}
It will check N number of digits in given string, i.e.: 4 for years 2 for months and days
Most importantly it will fetch without using any separators like "_" and "." and this is why It's most flexible solution.
Extract date from filename using bash script
#!/bin/bash
# ^-- important: bash, not not /bin/sh
for f in *.csv; do # Don't use ls for iterating over filenames
[[ $f =~ [[:digit:]]{8} ]] && { # native built-in regex matching
number=${BASH_REMATCH[0]} # ...refer to the matched content...
echo "Found $number in filename $f" # ...and emit output.
}
done
How to get date and string separately in a given file name using shell script
To extract date and name:
$ name="95FILRDF01PUBLI20170823XEURC0V41000.XML"
$ echo "$name" | sed -E 's/.*([[:digit:]]{8})([[:alpha:]]{4}).*/date=\1 name=\2/'
date=20170823 name=XEUR
The key part of the regex is ([[:digit:]]{8})([[:alpha:]]{4})
. The first part of that, ([[:digit:]]{8})
matches 8 digits and saves them as group 1. The second part of that, ([[:alpha:]]{4})
matches four letters that follow the date and saves them as group 2.
The key part is surrounded by .*
before and .*
after which matches whatever is left over.
The replacement text is date=\1 name=\2
which formats the output.
How can I extract date, time and filename from ls -l within a loop?
The output of ls -l
is meant to be human readable, rather than suitable for scripts.
Example from my machine:
$ ls -l /
total 22
-rw-r--r--. 1 root root 0 Nov 7 17:41 1
lrwxrwxrwx. 1 root root 7 Feb 4 2016 bin -> usr/bin
dr-xr-xr-x. 6 root root 1024 Dec 27 11:20 boot
# ...
As you can see, the items modified last year get the year instead of time. Of course you can add more options to get around that, like with --time-style=+%Y-%m-%d %H:%M:%S %4Z"
you can get 2017-03-16 17:31:12 EET
. Once you throw enough options to make the output somewhat reliable, you can start parsing it, which depending on the files can also prove more complex.
That said, unless you are stuck with ls
and for
loops for some reason, it would be better to use find
, which does a great job in finding and displaying files and directories and their details, as well as running commands on them.
find /somePath/log.out* -printf "Date: %TY-%Tm-%Td - Time: %TT - File: %f\n" -exec tail -n5 {} \;
or perhaps better
find /somePath/ -maxdepth 1 -name "log.out*" -type f -printf "Date: %TY-%Tm-%Td - Time: %TT - File: %f\n" -exec tail -n5 {} \;
Check man find
for more details on its options and how to further format the output (eg. date and time formats).
One more note: In Linux usually there is no such thing as create date/time. The files store the times for their last modification, change and maybe access (if enabled for the file system). The above command uses the modification time.
how to extract a part of file name in unix scripting and check if it exists
Try:
FILE="abc_def_xxx_yyy_20210515.txt"
DLS="${FILE%_*}.txt"
DLL="$HOME/$DLS"
if [ -f "$DLL"]; then
echo "$DLL exists"
else
touch "$DLL"
fi
extract date from a txt file using shell script
Since you haven't given more information on the exact file format except that example:
date_end=$(tail -n 2 file.txt|head -n 1|tr -cd '[0-9-]')
(assuming that the input is stored in file.txt).
How do i extract the date from multiple files with dates in it?
This is a typical application of a for-loop
in bash to iterate thru files.
At the same time, this solution utilizes GNU [ shell param substitution ].
for file in /path/to/files/*\.log\.*
do
foldername=${file#*-}
foldername=${foldername%%-*}
mkdir -p "${foldername}" # -p suppress errors if folder already exists
[ $? -eq 0 ] && mv "${file}" "${foldername}" # check last cmd status and move
done
Related Topics
Linux Assembly: How to Call Syscall
Calculate Word Occurrences from File in Bash
Comparison of Integer and Floating Point Numbers in Shell Script
Stack Resident Buffer Overflow on 64-Bit
Cmake Include_Directories Order After/Before
How Does Ancillary Data in Sendmsg() Work
Critical Timing in an Arm Linux Kernel Driver
Accessing Linux /Dev/Usb as Standard Files to Communicate with Usb Device
Extract Date from a File Name in Unix Using Shell Scripting
Redirect Process Stdin and Stdout to Netcat
Shell Script Password Security of Command-Line Parameters
How to Run an Opengl Application Installed on a Linux MAChine from My Windows MAChine
Where Do You Download Linux Source Code
How to Overcome an Incompatibility Between the Ksh on Linux VS. That Installed on Aix/Solaris/Hpux