Meaning of "b" modifier in "sort" command


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Meaning of "b" modifier in "sort" command
# 1  
Old 02-02-2011
Meaning of "b" modifier in "sort" command

I need to sort the following file by the rhdiskpower devices in the last column:

HTML Code:
Total_MB  Free_MB   OS_MB  Name           Failgroup      Library  Label  UDID  Product  Redund   Path
    1024      851    1024  OCRVOT1_0000   OCRVOT1_0000   System                         UNKNOWN  /dev/rhdiskpower1
    1024      853    1024  OCRVOT1_0002   OCRVOT1_0002   System                         UNKNOWN  /dev/rhdiskpower11
    1024      855    1024  OCRVOT1_0001   OCRVOT1_0001   System                         UNKNOWN  /dev/rhdiskpower10
    1024      884    1024  OCRVOT1_0003   OCRVOT1_0003   System                         UNKNOWN  /dev/rhdiskpower2
    1024      884    1024  OCRVOT1_0005   OCRVOT1_0005   System                         UNKNOWN  /dev/rhdiskpower9
    1024      885    1024  OCRVOT1_0004   OCRVOT1_0004   System                         UNKNOWN  /dev/rhdiskpower8
    8192     7563    8192  DBFCRED2_0000  DBFCRED2_0000  System                         UNKNOWN  /dev/rhdiskpower12
    8192     7564    8192  DBFCRED1_0000  DBFCRED1_0000  System                         UNKNOWN  /dev/rhdiskpower3
    8192     7568    8192  DBFCRED1_0001  DBFCRED1_0001  System                         UNKNOWN  /dev/rhdiskpower4
    8192     7569    8192  DBFCRED2_0001  DBFCRED2_0001  System                         UNKNOWN  /dev/rhdiskpower13
   65536    65441   65536  DBFCFLR1_0000  DBFCFLR1_0000  System                         UNKNOWN  /dev/rhdiskpower7
  409600   126488  409600  DBFCDAT1_0002  DBFCDAT1_0002  System                         UNKNOWN  /dev/rhdiskpower5
  409600   126492  409600  DBFCDAT1_0000  DBFCDAT1_0000  System                         UNKNOWN  /dev/rhdiskpower14
  409600   126504  409600  DBFCDAT1_0001  DBFCDAT1_0001  System                         UNKNOWN  /dev/rhdiskpower15
After spending more time than I care to admit, I finally was able to do it with the following sort command:

Code:
sort -k 8.17bn <name of file>

My question is this: The "b" was the critical piece to making this sort work. Without the "b", it sorted numerically by the first column. I've read the sort man pages, and it says that the "b" ignores leading blank characters. Any idea how that relates to what I was trying to do here?

Last edited by wjssj; 02-02-2011 at 01:15 PM..
# 2  
Old 02-02-2011
You have the answer from the rather terse man page. Basically what you have without using -b is a problem where the sort command will consider each blank as a field separator, so for a line linke this:
Code:
123456789012345678901234567890    (Just column refernece numbers)
    1234   abcd    zyxw
    2345   bcde    yxwv
   34567   cdef    xwvu
   45678   defg    wvut

... then it will assume that abcd is actually field 9 and everything gets skewed off. On the third line, cdef is field 8 (the number is one digit longer and replaces one blank)

What is nice for a human to look at because it is in nice columns does not mean the same to the sort command.

Using the -b flag means it will ignore leading and multiple blanks, so you get 1234 as the first field, abcd as the second and zyxv as the third from the top line, which is probalby more what a human would naturally do.





Does that help?




Robin
Liverpool/Blackburn
UK
This User Gave Thanks to rbatte1 For This Post:
# 3  
Old 02-02-2011
THANKS for your reply. I just went back and re-read the AIX man page for "sort", where it explained that the the blanks after one field are actually part of the following field (as you explained). No wonder I couldn't get it to work! Thanks again!
# 4  
Old 02-04-2011
You are welcome. If it's not obvious, then the manual author hasn't done a good job. Better still, they often use confusing language to explain something and get the reader even more confused. I've been there plenty of times and only got the hang of things by trial & error with very simple examples.

It's why I like the discussion forum so much. Real people with real problems discussing so we work our what it all means. I have found the help I have gained invaluable.



Robin
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. AIX

Apache 2.4 directory cannot display "Last modified" "Size" "Description"

Hi 2 all, i have had AIX 7.2 :/# /usr/IBMAHS/bin/apachectl -v Server version: Apache/2.4.12 (Unix) Server built: May 25 2015 04:58:27 :/#:/# /usr/IBMAHS/bin/apachectl -M Loaded Modules: core_module (static) so_module (static) http_module (static) mpm_worker_module (static) ... (3 Replies)
Discussion started by: penchev
3 Replies

2. Shell Programming and Scripting

Bash script - Print an ascii file using specific font "Latin Modern Mono 12" "regular" "9"

Hello. System : opensuse leap 42.3 I have a bash script that build a text file. I would like the last command doing : print_cmd -o page-left=43 -o page-right=22 -o page-top=28 -o page-bottom=43 -o font=LatinModernMono12:regular:9 some_file.txt where : print_cmd ::= some printing... (1 Reply)
Discussion started by: jcdole
1 Replies

3. UNIX for Beginners Questions & Answers

Extract delta records using with "comm" and "sort" commands combination

Hi All, I have 2 pipe delimited files viz., file_old and file_new. I'm trying to compare these 2 files, and extract all the different rows between them into a new_file. comm -3 < sort file_old < sort file_new > new_file I am getting the below error: -ksh: sort: cannot open But if I do... (7 Replies)
Discussion started by: njny
7 Replies

4. UNIX for Dummies Questions & Answers

Using "mailx" command to read "to" and "cc" email addreses from input file

How to use "mailx" command to do e-mail reading the input file containing email address, where column 1 has name and column 2 containing “To” e-mail address and column 3 contains “cc” e-mail address to include with same email. Sample input file, email.txt Below is an sample code where... (2 Replies)
Discussion started by: asjaiswal
2 Replies

5. Shell Programming and Scripting

how to use "cut" or "awk" or "sed" to remove a string

logs: "/home/abc/public_html/index.php" "/home/abc/public_html/index.php" "/home/xyz/public_html/index.php" "/home/xyz/public_html/index.php" "/home/xyz/public_html/index.php" how to use "cut" or "awk" or "sed" to get the following result: abc abc xyz xyz xyz (8 Replies)
Discussion started by: timmywong
8 Replies

6. Shell Programming and Scripting

awk command to replace ";" with "|" and ""|" at diferent places in line of file

Hi, I have line in input file as below: 3G_CENTRAL;INDONESIA_(M)_TELKOMSEL;SPECIAL_WORLD_GRP_7_FA_2_TELKOMSEL My expected output for line in the file must be : "1-Radon1-cMOC_deg"|"LDIndex"|"3G_CENTRAL|INDONESIA_(M)_TELKOMSEL"|LAST|"SPECIAL_WORLD_GRP_7_FA_2_TELKOMSEL" Can someone... (7 Replies)
Discussion started by: shis100
7 Replies

7. UNIX for Dummies Questions & Answers

the meaning of "!:*" in "alias foo 'command\!:*' filename"

Hi: How can I remove my own post? Thanks. (2 Replies)
Discussion started by: phil518
2 Replies

8. Shell Programming and Scripting

cat $como_file | awk /^~/'{print $1","$2","$3","$4}' | sed -e 's/~//g'

hi All, cat file_name | awk /^~/'{print $1","$2","$3","$4}' | sed -e 's/~//g' Can this be done by using sed or awk alone (4 Replies)
Discussion started by: harshakusam
4 Replies

9. UNIX for Dummies Questions & Answers

Explain the line "mn_code=`env|grep "..mn"|awk -F"=" '{print $2}'`"

Hi Friends, Can any of you explain me about the below line of code? mn_code=`env|grep "..mn"|awk -F"=" '{print $2}'` Im not able to understand, what exactly it is doing :confused: Any help would be useful for me. Lokesha (4 Replies)
Discussion started by: Lokesha
4 Replies
Login or Register to Ask a Question