Why doesn't sort -k $number work ??


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Why doesn't sort -k $number work ??
# 1  
Old 06-02-2003
Question Why doesn't sort -k $number work ??

I know this seems like a stupid question.
I am trying to sort an address book. Some peole have first, middle and last names, some only have first and last names.

Eg:
Bob Hope
John Bon Jovi
etc ..

I want to sort this by last name.
I was thinking of using something like sort -k $variable
So when the name is
bob hope variable=2
jon bon jovi variable=3
Thus sorting by the approriate field.

But as I suspected that doesn't work.
Any ideas ???
# 2  
Old 06-02-2003
You can sort that list with this command:
awk '{print $NF,$0}' | sort | cut -d' ' -f 2- < inputfile
# 3  
Old 06-02-2003
Thanks, although that didnt seem to work
I figured it out.

Thanks Anyway !

Last edited by kevin80; 06-02-2003 at 05:50 PM..
# 4  
Old 06-02-2003
Opps.. this this...

awk '{printf $NF,$0}' <inputfile | sort | cut -d' ' -f 2-

I gotta learn to test this stuff before I post.

But also, your test data really needs a few more lines. Those three names are already sorted by last name. You need to have some lines out of order so that the sort program has something to do.
# 5  
Old 06-02-2003
I had picked up on the "printf" typo you made (Im not that new) but to no avail. But I found a different way to do it.
Thanks Anyway,

I now know how to use awk (after reading up on the man page)
# 6  
Old 06-02-2003
That's a creative solution..

It worked for me perfectly (both with print and with printf) when I tried it in ksh at work.. then I went home and tried again in ksh but needed to replace the 2- with 1- to make it work correctly. Also.. it hangs unless I move < inputfile from the end of the script to immediately after the awk command and before the pipe. What exactly didn't work for you?
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

-ne 0 doesn't work -le does

Hi, I am using korn shell. until ] do echo "\$# = " $# echo "$1" shift done To the above script, I passed 2 parameters and the program control doesn't enter inside "until" loop. If I change it to until ] then it does work. Why numeric comparison is not working with -ne and works... (3 Replies)
Discussion started by: ab_2010
3 Replies

2. UNIX for Dummies Questions & Answers

Why doesn't this work?

find . -name "05_scripts" -type d -exec mv -f {}/'*.aep\ Logs' {}/.LogFiles \; Returns this failure: mv: rename ./019_0120_WS_WH_gate_insideTEST/05_scripts/*.aep\ Logs to ./019_0120_WS_WH_gate_insideTEST/05_scripts/.LogFiles/*.aep\ Logs: No such file or directory I don't know why it's trying... (4 Replies)
Discussion started by: scribling
4 Replies

3. Shell Programming and Scripting

echo doesn't work right

Hi,when I run my first shell script,I got something that doesn't work right. I wrote this code in the script. echo -e "Hello,World\a\n"But the screen print like this: -e Hello,World The "-e" wasn't supposed to be printed out. Can anyone help me out?:wall: Many thanks!:) (25 Replies)
Discussion started by: Demon
25 Replies

4. Shell Programming and Scripting

HELP: If Doesn't Work in AWK

Hi! I have a somehow big file (almost 3000 lines long and thirteen columns). Some lines have no value at all or, at least, are incomplete. The columns' values that have no data are marked with a "-" and the corresponding line (the line that owns that value) should be discarded and not used. ... (5 Replies)
Discussion started by: Marcelo de Brit
5 Replies

5. Shell Programming and Scripting

Lipo doesn't work

Hi guys, Am using lipo to merge ppc and i386 version of a static/dylib file based on "file type to load". I am working on Mac OS 10.5.6 and new to shell scripting. Please help me out. This is my code. echo "This file combine ppc and i386 file to form universal library" echo "source... (4 Replies)
Discussion started by: vishwesh
4 Replies

6. Shell Programming and Scripting

alias doesn't work

Hi I have put alias ll='ls -la' in .profile file but it doesn't work. On hand it works it looks like the .profile file is not beeing read. How to check whitch file is loaded? ,profile? .bash_profile? My system: SunOS mion 5.10 Generic Shell: /bin/pfksh Thanks (2 Replies)
Discussion started by: miojamo
2 Replies

7. UNIX for Dummies Questions & Answers

Script doesn't work, but commands inside work

Howdie everyone... I have a shell script RemoveFiles.sh Inside this file, it only has two commands as below: rm -f ../../reportToday/temp/* rm -f ../../report/* My problem is that when i execute this script, nothing happened. Files remained unremoved. I don't see any error message as it... (2 Replies)
Discussion started by: cheongww
2 Replies

8. Shell Programming and Scripting

sed doesn't work

Hello I' m confused a bit. I want to replace string "&amp" with "&" using this command. sed 's/&amp/&/g' and it doesn't work. Nothing happens. On the other side this works: sed 's/&amp/@/g' or sed 's/&amp/^/g' !!! Can somebody help please? Thanks (3 Replies)
Discussion started by: billy5
3 Replies

9. UNIX for Dummies Questions & Answers

my ctrl+c doesn't work

it seams that my ctrl+c and my ctrl+d don't work. if I type a bunch of jiberish on a line and ctrl+c I expect the command to be cancelled and to be given a fresh prompt, but instead it just putts ^C at the end of the line. Also, ctrl+d should close the session, but instead mine just puts ^D at... (3 Replies)
Discussion started by: yankee428
3 Replies

10. Shell Programming and Scripting

Why doesn't this work?

cat .servers | while read LINE; do ssh jason@$LINE $1 done exit 1 ./command.ksh "ls -l ~jason" Why does this ONLY iterate on the first server in the list? It's not doing the command on all the servers in the list, what am I missing? Thanks! JP (2 Replies)
Discussion started by: jpeery
2 Replies
Login or Register to Ask a Question