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

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.)

Related

how do i sort this list of emails using bash? [duplicate]

This question already has an answer here:
Output from sort does not appear to be sorted
(1 answer)
Closed 2 years ago.
I'm trying to sort a list of emails using a bash script with code below but I'm getting the wrong output, advice
sort -t # -k1,1
You will need to add -d for dictionary order and so:
sort -r -d -t# -k1,1

How to remove date from filename linux [duplicate]

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

Performing set operations using linux command [duplicate]

This question already has answers here:
difference between the content of two files
(4 answers)
Closed 8 years ago.
I have the following two lists of numbers stored in two different files:
File A:
7
1
2
9
File B:
10
8
4
9
Now I want to find out the set operation A-B (i.e. find only those numbers that are in A but not in B). Is there some way by which I may do the same using linux command (like sed). I know it is possible to do it using python, but I am just curious to know if it is possible to do the same using some linux command?
The simple, almost-working version is this:
grep -v -f file2 file1
That is, use lines from file2 as patterns; match them in file1 and print the ones not found (i.e. file1 - file2). However, what if file1 contains 10 and file2 contains 1? Then we have a problem because substrings are matched too. We can fix it this way:
grep -v -f <(sed 's/\(.*\)/^\1$/' file2) file1
That is, preprocess file2 to prepend ^ and append $ so the matching occurs against entire lines, not substrings in file1.
diff is the tool for this:
diff f1 f2
1,3c1,3
< 7
< 1
< 2
---
> 10
> 8
> 4
f1 has this unique number 7,1,2
f2 has this unique number 10,8,4

Best way to find the numeric value in UNIX file system [duplicate]

This question already has answers here:
How to find all files containing specific text (string) on Linux?
(54 answers)
Closed 8 years ago.
I need to grep for a particular port number from a huge set of files.
I am using a command:
find . |xargs grep "9461"
But it does not finds all the occurrences for number 9461.
Can anyone suggest a better unix/linux command to do so.
The kind of files it gets is : x.log, y.txt,z.htm, a.out etc files
But it was not able to get abc.conf files
You surely have some reason for using find in combination with grep, but just in case:
You can replace your command by:
grep -r "9461" .
and if you want even line numbers
grep -rn "9461" .
As JonathanLefflero commented, there is also the option -e that make grep match againt a regular expression, so, the ultimate command would be
grep -rne 9461
You should take a look on grep man page
A final note, you should check if what you want to grep is "9461" or 9461 without "".

How to do the opposite of diff? [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
how to show lines in common (reverse diff)?
Is there a command to do the opposite of diff? I want to compare two files if the same thing exists in both create a list of them. i am trying to figure out what entry's exist in both files.
Here is a solution that WILL NOT change the order of the lines:
fgrep -x -f file1 file2
Use the join command:
join a.txt b.txt
assuming the files are sorted; if not:
sort a.txt > sorted_a.txt; sort b.txt > sorted_b.txt; join sorted_a.txt sorted_b.txt

Resources