Visit Our UNIX and Linux User Community


Sort from start index and end index in line


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Sort from start index and end index in line
# 1  
Old 09-17-2009
Question Sort from start index and end index in line

Hi All,

I have a file (FileNames.txt) which contains the following data in it.
$ cat FileNames.txt
MYFILE17XXX208Sep191307.csv
MYFILE19XXX208Sep192124.csv
MYFILE20XXX208Sep192418.csv
MYFILE22XXX208Sep193234.csv
MYFILE21XXX208Sep193018.csv
MYFILE24XXX208Sep194053.csv
MYFILE23XXX208Sep193837.csv
$

I want to sort these files names using the number avaialable after the "08Sep" in each file name. The starting index of this number in this filename string will be 17 and end index will 22.

Please help me on how to sort these file names using this number.

Cheers ,
Krish.
# 2  
Old 09-17-2009
Code:
sort -k1.11,1.17 -n  -o myfile.txt myfile.txt

# 3  
Old 09-17-2009
Are you sure the starting index is 17 (not 18) and the ending index is 22 (not 23) since you say the number after the "08Sep" string...
Code:
sort -k1.18,1.23 infile

# 4  
Old 09-18-2009
Hi jim mcnamara, shamrock,

Thanks for your great support .. It worked well.. Smilie

Shamrock,

Yes, the index should be 18, 23. The thought the string's staring index is 0, that is the reason I have put the index as 17,22.
Do we need to consider the start index as 1 for sort ?? Just curious. Smilie

Thanks again!!!!!!!

Cheers,
Krish.
# 5  
Old 09-18-2009
Code:
open FH,"<b.txt";
my @arr=<FH>;
print  map {$_->[0]} 
          sort {$a->[1] <=> $b->[1]} 
            map {/.*([0-9]{6})\..*/;[$_,$1]} @arr;

# 6  
Old 09-18-2009
Quote:
Originally Posted by shamrock
Are you sure the starting index is 17 (not 18) and the ending index is 22 (not 23) since you say the number after the "08Sep" string...
Code:
sort -k1.18,1.23 infile

Can you please explain why you use 1.18 and 1.23 above?
Can't we use simple 18 and 23 like below
Code:
sort -k18,23 infile


Previous Thread | Next Thread
Test Your Knowledge in Computers #876
Difficulty: Medium
All operational code must be POSIX compliant in order to be cost effective.
True or False?

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Printing string from last field of the nth line of file to start (or end) of each line (awk I think)

My file (the output of an experiment) starts off looking like this, _____________________________________________________________ Subjects incorporated to date: 001 Data file started on machine PKSHS260-05CP ********************************************************************** Subject 1,... (9 Replies)
Discussion started by: samonl
9 Replies

2. Shell Programming and Scripting

Need Help in Index

I am having data in XML format and trying to extract codes form two fields called <String>, below is the data. <Node>tollfree<Condition>BooleanOperator<Operation>AND</Operation><Condition>BooleanOperator<Operation>NOT</Operation><Condition>FieldSelection<Field Context="ALL fields"... (7 Replies)
Discussion started by: rramkrishnas
7 Replies

3. Shell Programming and Scripting

How to get index values for multiple matches in the same line with awk?

Hi, I know that echo "bob alice robert alice" | awk '{print index($0,"alice")}' 5Will output the index of the first alice match, is there any way to get the index of all matches?, eg: echo "bob alice robert alice" | awk 'unknown magic' 5:18Thanks for your time. (6 Replies)
Discussion started by: chilicuil
6 Replies

4. Programming

Merge sort when array starts from zero(0) index???

Hi friends, I have implemented the merge sort algorith in c, before I put forward my question, you please have a look at my code. // The array is sorted, as 1234 #include <stdio.h> #include <stdlib.h> #include <math.h> int A = {4, 3, 2, 1}; void Merge_Sort(int , int, int); void... (0 Replies)
Discussion started by: gabam
0 Replies

5. Shell Programming and Scripting

get out the lines by line index

Hi, I have two files and want to get out the lines from file2.txt by the line number in file1.txt. The first one includes the line index numbers file1.txt 2 6 10 ... And the second file has lots of lines like: file2.txt A B C F G H R T P K L T T F F (8 Replies)
Discussion started by: senayasma
8 Replies

6. Shell Programming and Scripting

awk index

1 2 000060000 How do i return the point in the string where the 6 is? i.e what I want on output is 1 2 5 something like awk '{print $1 $2 index($3,6) }' but I can't get it to work Thanks in advance (3 Replies)
Discussion started by: garethsays
3 Replies

7. UNIX for Dummies Questions & Answers

wh inode index starts from 1 unlike array index (0)

brothers why inode index starts from 1 unlike array inex which starts from 0 its a question from the design of unix operating system of maurice j.bach i need to know the answer urgently...someone help please (1 Reply)
Discussion started by: sairamdevotee
1 Replies

8. Filesystems, Disks and Memory

why the inode index of file system starts from 1 unlike array index(0)

why do inode indices starts from 1 unlike array indexes which starts from 0 its a question from "the design of unix operating system" of maurice j bach id be glad if i get to know the answer quickly :) (0 Replies)
Discussion started by: sairamdevotee
0 Replies

9. Shell Programming and Scripting

What is index?

hi, :) In a shell script i came accross the following lines 1.for i in ` find /home/oracle -name ch' 2.do 3.echo $i 4.idx=`expr index $i .` 5.done Here iam not able to understand the porpose of the word "index" in line 4. any help ? cheers RRK (3 Replies)
Discussion started by: ravi raj kumar
3 Replies

Featured Tech Videos