Visit Our UNIX and Linux User Community


Sort by Column Headers


View Poll Results: Differfent ways to sort by column headers
sort 3 100.00%
awk 0 0%
sed 0 0%
Multiple Choice Poll. Voters: 3. This poll is closed

 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Sort by Column Headers
# 1  
Old 10-07-2009
Sort by Column Headers

Hi All,

I am new to UNIX can you please help me to sort a file with different columns

my file looks like this
Code:
[liveuser@localhost ~]$ cat gaut.txt
UID        PID  PPID  PGID   SID  C STIME TTY          TIME CMD
liveuser  3008  2892  3008  3008  0 11:58 ?        00:00:00 gnome-session
liveuser  3019     1  3008  3008  0 11:58 ?        00:00:00 dbus-launch --sh-syntax --exit-with-session
liveuser  3025     1  3025  3025  0 11:58 ?        00:00:00 /bin/dbus-daemon --fork --print-pid 7 --print-address 9 --session

Please help me the best and fastest way if i need to sort by PGID

Thanks
GautamChennuru

---------- Post updated 10-07-09 at 02:45 AM ---------- Previous update was 10-06-09 at 10:27 PM ----------

Can someone answer to my question please

Last edited by pludi; 10-07-2009 at 04:15 AM.. Reason: code tags, please...
# 2  
Old 10-07-2009
Code:
awk '{ print $4, $0 }' infile | 
  sort -n | 
    cut -d\  -f2-

# 3  
Old 10-07-2009
hi radoulov,

cut -d\ is giving error
HTML Code:
[liveuser@localhost ~]$ awk '{print $6 , $0}' gaut.txt |sort -n |cut -d\ -f2-
cut: the delimiter must be a single character
Try `cut --help' for more information.
[liveuser@localhost ~]$

can u please suggest

thanks
gautam
# 4  
Old 10-07-2009
You need two spaces after the backslash, not one.
This should be easier to understand:

Code:
cut -d' ' -f2-

# 5  
Old 10-07-2009
either
Code:
awk '{ print $4, $0 }' infile | 
  sort -n | 
    cut -d ' '  -f2-

or
Code:
awk '{ print $4 "\t" $0 }' infile | 
  sort -n | 
    cut -f2-

# 6  
Old 10-07-2009
But if sort with columns having single word letter then it is not sorting exactly please see below if i try to sort with column C

HTML Code:
[liveuser@localhost ~]$ ps -fuliveuser|awk '{print $4,$0}' |sort -n|cut -d\  -f2-
liveuser  4378  4352  0 20:31 pts/1    00:00:00 bash
liveuser  4525  4378  0 20:52 pts/1    00:00:00 awk {print $4,$0}
liveuser  4526  4378  0 20:52 pts/1    00:00:00 sort -n
liveuser  4527  4378  0 20:52 pts/1    00:00:00 cut -d  -f2-
UID        PID  PPID  C STIME TTY          TIME CMD
liveuser  3404  3389  2 18:33 ?        00:03:41 /usr/lib/firefox-3.5b4/firefox
liveuser  4524  4378  2 20:52 pts/1    00:00:00 ps -fuliveuser


[liveuser@localhost ~]$ ps -fuliveuser
UID        PID  PPID  C STIME TTY          TIME CMD
liveuser  3006  2907  0 18:32 ?        00:00:00 gnome-session
liveuser  3022     1  0 18:32 ?        00:00:00 /bin/dbus-daemon --fork --print-pid 7 --print-address 9 --session
liveuser  3025     1  0 18:32 ?        00:00:00 dbus-launch --sh-syntax --exit-with-session
liveuser  3129     1  0 18:32 ?        00:00:01 /usr/libexec/gconfd-2
liveuser  3133     1  0 18:32 ?        00:00:00 /usr/libexec/gnome-settings-daemon
liveuser  3137     1  0 18:32 ?        00:00:00 gnome-keyring-daemon --start
liveuser  3146  3006  0 18:32 ?        00:00:02 metacity
liveuser  3149  3006  0 18:32 ?        00:00:02 gnome-panel
liveuser  3152     1  0 18:32 ?        00:00:00 /usr/bin/pulseaudio --start --log-target=syslog
liveuser  3154     1  0 18:32 ?        00:00:00 /usr/libexec/gvfsd
liveuser  3169     1  0 18:32 ?        00:00:00 /usr/libexec//gvfs-fuse-daemon /home/liveuser/.gvfs
liveuser  3175  3152  0 18:32 ?        00:00:00 /usr/libexec/pulse/gconf-helper
liveuser  3176  3006  0 18:32 ?        00:00:01 nautilus
liveuser  3178     1  0 18:32 ?        00:00:00 /usr/libexec/bonobo-activation-server --ac-activate --ior-output-fd=19
liveuser  3182  3006  0 18:32 ?        00:00:00 gpk-update-icon
liveuser  3199  3006  0 18:32 ?        00:00:00 bluetooth-applet
liveuser  3200  3006  0 18:32 ?        00:00:00 kerneloops-applet
liveuser  3201  3006  0 18:32 ?        00:00:00 python /usr/share/system-config-printer/applet.py
liveuser  3203     1  0 18:32 ?        00:00:00 /usr/libexec/im-settings-daemon
liveuser  3204  3006  0 18:32 ?        00:00:00 /usr/libexec/gdu-notification-daemon
liveuser  3209  3006  0 18:32 ?        00:00:00 gnome-volume-control-applet
liveuser  3213  3006  0 18:32 ?        00:00:02 gnome-power-manager
liveuser  3220  3006  0 18:32 ?        00:00:00 nm-applet --sm-disable
liveuser  3250  3006  0 18:32 ?        00:00:00 /usr/bin/seapplet
liveuser  3326     1  0 18:32 ?        00:00:00 /usr/libexec/gconf-im-settings-daemon
liveuser  3336     1  0 18:32 ?        00:00:00 /usr/libexec/notification-daemon
liveuser  3345     1  0 18:32 ?        00:00:04 /usr/libexec/wnck-applet --oaf-activate-iid=OAFIID:GNOME_Wncklet_Factory --oaf-ior-fd=18
liveuser  3351     1  0 18:32 ?        00:00:00 /usr/libexec/trashapplet --oaf-activate-iid=OAFIID:GNOME_Panel_TrashApplet_Factory --oaf-ior-fd=24
liveuser  3352     1  0 18:32 ?        00:00:00 /usr/libexec/gvfs-gdu-volume-monitor
liveuser  3355     1  0 18:32 ?        00:00:00 /usr/libexec/gvfs-gphoto2-volume-monitor
liveuser  3357     1  0 18:32 ?        00:00:00 /usr/libexec/gvfsd-trash --spawner :1.9 /org/gtk/gvfs/exec_spaw/0
liveuser  3360     1  0 18:32 ?        00:00:00 /usr/libexec/clock-applet --oaf-activate-iid=OAFIID:GNOME_ClockApplet_Factory --oaf-ior-fd=19
liveuser  3363     1  0 18:32 ?        00:00:00 /usr/libexec/gdm-user-switch-applet --oaf-activate-iid=OAFIID:GNOME_FastUserSwitchApplet_Factory --oaf-ior-fd=
liveuser  3366     1  0 18:32 ?        00:00:00 /usr/libexec/notification-area-applet --oaf-activate-iid=OAFIID:GNOME_NotificationAreaApplet_Factory --oaf-ior
liveuser  3375     1  0 18:32 ?        00:00:01 gnome-screensaver
liveuser  3377     1  0 18:32 ?        00:00:00 /usr/libexec/gvfsd-burn --spawner :1.9 /org/gtk/gvfs/exec_spaw/1
liveuser  3389     1  0 18:33 ?        00:00:00 /bin/sh /usr/lib/firefox-3.5b4/run-mozilla.sh /usr/lib/firefox-3.5b4/firefox
liveuser  3404  3389  2 18:33 ?        00:03:49 /usr/lib/firefox-3.5b4/firefox
liveuser  4352     1  0 20:28 ?        00:00:09 gnome-terminal
liveuser  4353  4352  0 20:28 ?        00:00:00 gnome-pty-helper
liveuser  4378  4352  0 20:31 pts/1    00:00:00 bash
liveuser  4537  4378  1 20:54 pts/1    00:00:00 ps -fuliveuser
Can you please check and let me know

Thanks
Gautam
# 7  
Old 10-07-2009
Quote:
Originally Posted by cgk1983
But if sort with columns having single word letter then it is not sorting exactly please see below if i try to sort with column C
[...]
You said you wanted to sort by PGID ...
If you want to sort by different columns, the code should be modified ...
 

Previous Thread | Next Thread
Test Your Knowledge in Computers #608
Difficulty: Easy
Python does not need compilation to binary to run.
True or False?

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Row bind multiple csv files having different column headers

All, I guess by this time someone asked this kind of question, but sorry I am unable to find after a deep search. Here is my request I have many files out of which 2 sample files provided below. File-1 (with A,B as column headers) A,B 1,2 File-2 (with C, D as column headers) C,D 4,5 I... (7 Replies)
Discussion started by: ks_reddy
7 Replies

2. Shell Programming and Scripting

Capturing column headers in an array

Hello, I am processing a tab delimited text file and need to grab all of the column headers in an array. The input looks like, num Name PCA_A1 PCA_A2 PCA_A3 0 compound_00 -3.5054 -1.1207 -2.4372 1 compound_01 -2.2641 0.4287 ... (5 Replies)
Discussion started by: LMHmedchem
5 Replies

3. Shell Programming and Scripting

Merge csvs with column headers

hello gurus, Somebody must have done this before, I couldn't find anything. Please redirect me if this was solved before, and if not please help. To the problem now, I have multiple csv files (about 1000) which I need to concatenate by column header. The final file should have a superset... (4 Replies)
Discussion started by: abh.kumar
4 Replies

4. Shell Programming and Scripting

Sar -u generates multiple column headers in csv file

Hi All, The below sar -u command generates multiple column headers in csv file Expected output should print column headers only once in the csv file shell script: $cat sar_cpu_EBS.sh #!/bin/bash while ; do sar -u 15 1 | awk '/^/ {print $1,$2,$4,$6,$7}' | tr -s ' ' ',' >>... (6 Replies)
Discussion started by: a1_win
6 Replies

5. Shell Programming and Scripting

Merge column headers and transpose

Hello Everyone! I am new on this forum and this is my first post. I wish to apologize for my, not canonical, English. I would like to solve this problem but I have no clue of how do it!I will be grateful if someone could help me! I have a table like this: gene TF1 TF2 TF3 TF4 gene1 1 2 3 4... (5 Replies)
Discussion started by: giuliangiuseppe
5 Replies

6. Shell Programming and Scripting

Transpose field names from column headers to values in one column

Hi All, I'm looking for a script which can transpose field names from column headers to values in one column. for example, the input is: IDa;IDb;IDc;PARAM1;PARAM2;PARAM3; a;b;c;p1val;p2val;p3val; d;e;f;p4val;p5val;p6val; g;h;i;p7val;p8val;p9val; into the output like this: ... (6 Replies)
Discussion started by: popesk
6 Replies

7. Shell Programming and Scripting

Matching words based on column headers

Hi , Pls help on this. Input file: NAME1 BSC1 TEXT ID 1 MAINSFAIL TEXT ID 2 DGON TEXT ID 3 lOADONDG NAME2 BSC2 TEXT ID 1 DGON TEXT ID 3 lOADONG (1 Reply)
Discussion started by: bha148
1 Replies

8. Shell Programming and Scripting

Merging of files with different headers to make combined headers file

Hi , I have a typical situation. I have 4 files and with different headers (number of headers is varible ). I need to make such a merged file which will have headers combined from all files (comman coluns should appear once only). For example - File 1 H1|H2|H3|H4 11|12|13|14 21|22|23|23... (1 Reply)
Discussion started by: marut_ashu
1 Replies

9. Shell Programming and Scripting

Excel Column Headers

cat ABC.log | egrep "Error 500" >> /tmp/Logs.log egrep "<Mango>.*<.Mango>" Logs.log | sed -e "s/^.*<Mango/<Mango/" | cut -f2 -d">"| cut -f1 -d"<" >> /tmp/temp1.xls egrep "<Apple>.*<.Apple>" Logs.log | sed -e "s/^.*<Apple/<Apple/" | cut -f2 -d">"| cut -f1 -d"<" >> /tmp/temp2.xls print Heading1,... (1 Reply)
Discussion started by: pk_eee
1 Replies

10. Shell Programming and Scripting

Removing Headers and a Column

I have a text file in unix with a layout like this Column 1 - 1-12 Column 2 - 13-39 Column 3 - 40-58 Column 4 - 59-85 Column 5 - 86-120 Columbn 6 - 121-131 The file also has a header on the first 6 lines of each page. Each page is 51 lines long. So I want to remove the header from each... (30 Replies)
Discussion started by: DerangedNick
30 Replies

Featured Tech Videos