- Subscribe to RSS Feed
- Mark Question as New
- Mark Question as Read
- Float this Question for Current User
- Bookmark
- Subscribe
- Mute
- Printer Friendly Page
how to sort the file name in shell script
Created ‎07-13-2018 08:06 AM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
How to sort the filename in shell.
My files looks like
abcd_2_20180703
abcd_4_20180703
abcd_5_20180703
abcd_1_20180703
abcd_3_20180703
abcd_6_20180703
And i expect after the sorting
abcd_1_20180703
abcd_2_20180703
abcd_3_20180703
abcd_5_20180703
..
Please help me sort the files. TIA
Created ‎07-15-2018 07:53 AM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Try sort with delimiter
# ls -1 | sort -t '_' -nk3 # ls -1 | sort -t '_' -nk3 abcd_1_20180703 abcd_2_20180703 abcd_3_20180703 abcd_4_20180703 abcd_5_20180703 abcd_6_20180703 abcd_1_20180704 abcd_2_20180704 abcd_3_20180704 abcd_4_20180704 abcd_5_20180704 abcd_6_20180704 #cat test.out | sort -t '_' -nk3
Created ‎07-13-2018 10:01 AM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
You can simply use "sort" command.
[root@node1 ~]# cat /tmp/a.txt abcd_2_20180703 abcd_4_20180703 abcd_5_20180703 abcd_1_20180703 abcd_3_20180703 abcd_6_20180703 [root@node1 ~]# sort /tmp/a.txt abcd_1_20180703 abcd_2_20180703 abcd_3_20180703 abcd_4_20180703 abcd_5_20180703 abcd_6_20180703
Let me know if this helps.
Created ‎07-15-2018 07:01 AM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Thanks for your reply. the above sort will give correct output if the directory have only one day files.
if the directory have 20180704 files.
it sorted output like
abcd_1_20180703
abcd_1_20180704
abcd_2_20180703
abcd_2_20180704
abcd_3_20180703
abcd_3_20180704
abcd_4_20180703
abcd_4_20180704
abcd_5_20180703
abcd_5_20180704
abcd_6_20180703
abcd_6_20180704
But i expect
abcd_1_20180703
abcd_2_20180703
abcd_3_20180703
abcd_4_20180703
abcd_5_20180703
abcd_6_20180703
abcd_1_20180704
abcd_2_20180704
abcd_3_20180704
abcd_4_20180704
abcd_5_20180704
abcd_6_20180704
Any idea?
Created ‎07-15-2018 09:34 AM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
@Sundar Lakshmanan "sort" was for the requirement this is mentioned in the question details. You can use the command which @rguruvannagari has mentioned in the above comment to sort with the date (3rd column).
Created ‎07-15-2018 07:53 AM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Try sort with delimiter
# ls -1 | sort -t '_' -nk3 # ls -1 | sort -t '_' -nk3 abcd_1_20180703 abcd_2_20180703 abcd_3_20180703 abcd_4_20180703 abcd_5_20180703 abcd_6_20180703 abcd_1_20180704 abcd_2_20180704 abcd_3_20180704 abcd_4_20180704 abcd_5_20180704 abcd_6_20180704 #cat test.out | sort -t '_' -nk3
Created ‎07-15-2018 01:56 PM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Thanks @ Sandeep Nemuri @ rguruvannagari works fine
