How to remove date from filename linux [duplicate] - linux

This question already has answers here:
Rename multiple files while keeping the same extension on Linux
(4 answers)
Closed 3 years ago.
I have a scenario where I want to remove date from filename
Lets take an example 1 :
ABC_2019_06_12.txt
Lets take an example 2 :
ABCDEF_202012040120456.txt
using cut I cannot delete required text
how to cut to get the required below output like below
ABC.txt
ABCDEF.txt
One command which should work for all scenario which ever filename it is
My solution which I worked is to read the number of position and cut that part but I don't find it effective any other solution will be appreciated

In bash you can cut off the part starting with underscore:
$ filename=ABC_2019_06_12.txt
$ filename=${filename%%_*}
$ echo $filename
ABC

Related

finding specific pattern in linux [duplicate]

This question already has answers here:
Print only matching word, not entire line through grep
(2 answers)
Closed 5 years ago.
I want to find specific pattern in all the files in a directory and copy them to another line
For E.g
I want to find LOG_WARNING in one file XYZ and copy them to another file.
LOG_WARNING (abc, xyz,("WARNING: Error in sending concurrent_ to pdm\n"));
command i have used is :
grep -rin "LOG_WARNING.*" file_name.c > output.txt
but it is not copying till the semicolon, please note that other texts are available in next line. I want to copy till ;(semi-colon)
grep -rh "LOG_WARNING" * > out.txt
This will match the pattern in all the files inside the directory.
Since you mentioned that the texts that are present after the ';' are on the next line, I have provided this command.
This will match the pattern and print the entire line, till the ';'.
Else,
try this
grep -roPh 'LOG_WARNING[^;]*;' * > out.txt

Deleting a certain line on linux [duplicate]

This question already has answers here:
How to delete from a text file, all lines that contain a specific string?
(21 answers)
Closed 6 years ago.
For example I have a caf.txt file and I want to delete a "donut" word in the document without entering the document on linux .How can I do it?
To delete just the word "donut"
sed -i 's/donut//g' caf.txt
To delete lines that contain the word "donut"
sed -i '/donut/d' caf.txt
What I do is:
sed '/text_to_delete/d' filename | sponge filename
This will make the change to the source file.

How to delete last found value in Bash [duplicate]

This question already has answers here:
Bash : extracting part of a string
(4 answers)
Closed 6 years ago.
Say I have a string 0.0.25, how do I delete the last part after dot (including it) to make it like 0.0? Note that last part can have variable number of digits.
In bash you can do the following:
echo "${var%.*}"
See the Shell Parameter Expansion section of the manual.
Using awk you could:
echo "0.0.25" | awk -F. '{print $1"."$2}'

How to sort file names by specific part in linux? [duplicate]

This question already has answers here:
How can I sort file names by version numbers?
(7 answers)
Closed 8 years ago.
I have lots of files in my build folder and I am trying to sort them by using sort command.
The structure of the files are like that:
name - version - 'v' - build date
xxx-2.0.0-SNAPSHOT-V2014-07-10_18-01-05.log
xxx-2.0.0-SNAPSHOT-V2014-07-10_18-02-05.log
xxx-2.0.0-SNAPSHOT-V2014-07-10_18-03-05.log
xxx-2.0.0-SNAPSHOT-V2014-07-10_18-04-05.log
xxx-2.0.0-SNAPSHOT-V2014-07-10_18-05-05.log
if we assume that version string will be stay in 3 digit, sorting them is easy. What if I add different versions like 2.1 or 2.0.0.2 here ? I need a result like this:
xxx-2.1-SNAPSHOT-V2014-07-10_18-05-05.log
xxx-2.0.2-SNAPSHOT-V2014-07-10_18-04-05.log
xxx-2.0.0.2-SNAPSHOT-V2014-07-10_18-03-05.log
xxx-2.0.0.1-SNAPSHOT-V2014-07-10_18-02-05.log
xxx-2.0.-SNAPSHOT-V2014-07-10_18-01-05.log
$ cat file
xxx-2.0.2-SNAPSHOT-V2014-07-10_18-04-05.log
xxx-2.0.0.2-SNAPSHOT-V2014-07-10_18-03-05.log
xxx-2.1-SNAPSHOT-V2014-07-10_18-05-05.log
xxx-2.0.0.1-SNAPSHOT-V2014-07-10_18-02-05.log
xxx-2.0.-SNAPSHOT-V2014-07-10_18-01-05.log
$ sort -V -r -t- -k2,2 < file
xxx-2.1-SNAPSHOT-V2014-07-10_18-05-05.log
xxx-2.0.2-SNAPSHOT-V2014-07-10_18-04-05.log
xxx-2.0.0.2-SNAPSHOT-V2014-07-10_18-03-05.log
xxx-2.0.0.1-SNAPSHOT-V2014-07-10_18-02-05.log
xxx-2.0.-SNAPSHOT-V2014-07-10_18-01-05.log
Note: Some implementations of sort do not support -V option...
Explanation:
-V : Version sort
-t- : Split into columns with delimiter '-'
-k2,2: Sort by field 2 & only 2
-r : reverse sort (based on your expected output. Remove this flag, if not required.)

How to avoid the display of the 2 first line from a linux command output? [duplicate]

This question already has answers here:
What's the opposite of head? I want all but the first N lines of a file
(9 answers)
Closed 8 years ago.
I have a program that displays many line in the output
How I can make it display the all output except the first 2 lines?
easily using tail command:
tail -n+3
You could use awk
awk 'NR>2' file
In order to complete the triplet,
sed '1,2d' file

Resources