LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices



Closed Thread
 
Search this Thread
Old 06-24-2008, 11:21 AM   #1
matt007
LQ Newbie
 
Registered: Jun 2008
Posts: 14

Rep: Reputation: 0
sorting a file at a certain offset in bash


how should i sort a file on a certain field .....i am trying this....


awk 'substr($0, 471, 10)' filename | sort >> out

i.e. pick up the 471st position and 10 characters from there and pass each line to the sort command......
10 chars at postiion 471 are :

0500010001
1500010001
0500180020
0500180020
0500180020
0500180020
0500180020
0500180020
0500180020
0500180020
0500180020
0500180020
 
Old 06-24-2008, 01:04 PM   #2
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374
Hi,

It's not clear if the lines in the file are one long line or if they are divided into fields.

If the last is the case, you can use sort alone to do this:

sort -k<fieldnumber> infile
This is the global form, a space being the field separator.

Here's a real example:
Quote:
$ cat infile
54321:333:9876
12345:222:6789
12345:444:6789
12345:111:6789

$ sort -t":" -k2 infile
12345:111:6789
12345:222:6789
54321:333:9876
12345:444:6789
The -t option can be used to set the actual separator (not a space in this case, but a semicolon).

Hope this helps.

PS: Don't crosspost........
 
Old 06-24-2008, 01:09 PM   #3
crabboy
Moderator
 
Registered: Feb 2001
Location: Atlanta, GA
Distribution: Slackware
Posts: 1,823

Rep: Reputation: 120Reputation: 120
Please post your thread in only one forum. Posting a single thread in the most relevant forum will make it easier for members to help you and will keep the discussion in one place.

http://www.linuxquestions.org/questi...02#post3187702
 
Old 06-24-2008, 02:36 PM   #4
matt007
LQ Newbie
 
Registered: Jun 2008
Posts: 14

Original Poster
Rep: Reputation: 0
the file does not have delimiters and also does not have fields (delimited by white space)

It is just one contiguous string of characters and i need to sort it
by the charater string from 471 to 480.

the solution you provided will work if i had some delimiter....

still wondering if this is possible....

thanks!
 
Old 06-24-2008, 05:03 PM   #5
solarkash
LQ Newbie
 
Registered: Jun 2008
Posts: 11

Rep: Reputation: 1
sort -k 1.471,1.480 filename
 
  


Closed Thread


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Bash script for sorting and renaming multiple mp3 files by id3 tags simonloach Linux - General 8 02-16-2013 10:07 AM
bash: sorting a bunch of files into two halfs by size, and other tasks PatrickMay16 General 2 01-19-2008 02:04 AM
Bash: sorting by two fields humbletech99 Programming 7 09-25-2007 04:30 PM
Sorting files in BASH deleted/ Linux - Newbie 16 01-26-2006 07:03 AM
[Flex] How can I get a file offset during scanning? chuanyung Programming 2 03-07-2004 08:59 PM


All times are GMT -5. The time now is 08:58 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration