sort / cut question


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting sort / cut question
# 1  
Old 07-13-2007
sort / cut question

Hi All,

I have a small problem, hope you can help me out here.

I have a file that contains the same format of lines in 99% of the cases.
906516 XYZ.NNN V 0000 20070711164648 userID1 userID2 hostname 20070711164641

There are unfortunately several lines with these format:
906516 XYZ V 0000 20070711164647 userID1 userID2 hostname 20070711164641

The difference is in the 2nd coloumn. Sometimes its length is 3 , sometimes 7 digits long. Thus the number of delimiters between 2nd and 3rd column are also varied. Imagine the two lines as "V" in coloumn 3 would be in the same line...


My scenario is:
cut out the 1st(the number) and 5th (timestamp) coloumn and sort the result in reverse order using the timestamp column as key.

My command is:
cat filename |cut -f 1,15 -d " " |sort -r -k 2,2

Obviously this cannot work properly because sometimes the 5th column does not start at the 15th " " delimiter due the above mentioned reasons.

Could you guys let me know a solution I can carry out this scenario? I checked man for cut but could not find an option of using sophisticated delimiters, etc...

Thanks a lot!
BearCheese
# 2  
Old 07-13-2007
Code:
awk '{ print $5, $1}' filename | sort -rn > newfile

# 3  
Old 07-13-2007
Thank you!
Now I have a file with the content like this:
20070711172841 905994
20070711172822 905994
20070711171431 906608
20070711171412 906608
20070711171142 905994
20070711171140 905994
20070711171139 905994
20070711164813 905994

I would like to keep only one line where the second column is the same. But that line should be the most lastest entry (timestamp in 1st coloumn).
I tried command:
sort -u -k 2,2
but this keeps the oldest timestamp Smilie

I tought that -u suppress everything but the first match. The first match (that should be kept) should be :
20070711172841 905994

but my command magically keeps the oldest line:
20070711164813 905994

Could you please advise on why -u does not work as anticipated?

Thank you!
BearCheese
# 4  
Old 07-13-2007
Code:
mLastValue=''
mLastDate=''
sort -k2 -k1 input_file | \
while read mDate mValue
do
  if [ "$mValue" != "$mLastValue" ]; then
    if [ "$mLastValue" != "" ]; then
      echo $mLastDate" "$mLastValue
    fi
  fi
  mLastValue=$mValue
  mLastDate=$mDate
done
if [ "$mLastValue" != "" ]; then
  echo $mLastDate" "$mLastValue
fi

# 5  
Old 07-13-2007
Code:
awk '{ print $5, $1}' filename | sort -rn | \
     awk '
     { arr[$2]=$0 }      
     END { for ( item in arr) { print arr[item]}
     } ' filename > newfile

# 6  
Old 07-13-2007
Code:
awk '!arr[$2] { arr[$2] = $0 } 
       END { for ( item in arr ) { print arr[item] }
       }' file > newfile

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Question on cut

Korn Shell I have a file whose values are delimited using colon ( : ) $ cat test.txt hello:myde:temp:stiker $ cut -d: -f2,4 test.txt myde:stikerI want field 2 and field 4 to be returned but separated by a hyphen. The output should look like myde-stiker How can do this ? (without awk... (11 Replies)
Discussion started by: kraljic
11 Replies

2. UNIX for Advanced & Expert Users

cut and sort

Please help. I have a file containing rows of information. The row needs to be broken down into blocks of 5 and then sorted. Example: 10381 1042010046 ... (4 Replies)
Discussion started by: Dolph
4 Replies

3. Shell Programming and Scripting

Using a combination of sort/cut/grep/awk/join/paste/sed

I have a file and need to only select users that have a shell of “/bin/bash” in the line using awk or sed please help (4 Replies)
Discussion started by: boyboy1212
4 Replies

4. Homework & Coursework Questions

Help with cut and sort

<B>andan100:Anders:Andersson:800101-1234:TNCCC_1:TDDB46 TDDB80:berbe101:Bertil:Bertilsson:800102-1234:TNCCC_1:TDDB46 TDDB80:The top is how it looks right now I want it t look like this under and I want it to be sorted. I have tried with cut -f -d studenter.txt and so on but it still doesnt work... (2 Replies)
Discussion started by: aannaann
2 Replies

5. UNIX for Dummies Questions & Answers

Using the Sort, Cut, and Awk commands

Hello, I am trying, utilizing the few commands I know, to extract all records within my file that were generated in November of 2007. Each record within the file has a "date" field giving the month, day, and year (9-8-88). How do I extract those records to their own file? Once I extract... (4 Replies)
Discussion started by: babbabooey
4 Replies

6. Solaris

concatenate/sort/cut

I have the following requirement. 1. I have to concatenate the 10 fixed width files. 2. sort based on first 10 characters 3. after that i have remove first 10 chacters from the file. can you please tell me how to do it. Thanks in Advance Samba (1 Reply)
Discussion started by: samba
1 Replies

7. UNIX for Dummies Questions & Answers

Cut Question

Hi, I have created a variable abc within my script which can have values as follows abc = Ram,Iam or it can be abc = Uam or it can be abc = Sam,Tam,Pam Basically it can have a max of 3 values , seperated by comma. I want to assign these 3 values to 3 different variables In case of... (2 Replies)
Discussion started by: samit_9999
2 Replies

8. UNIX for Dummies Questions & Answers

cut question

#!/bin/bash echo "UserName PID Command" ps -ef > ps.temp grep '^\{2,3\}\{4\}' ps.temp > ps.temp2 cut -f1,2,8 ps.temp2 rm ps.temp* I am having some problems with the cut command. I only want to display the UID (field 1), PID(field 2), and Command(field 8). Right now the whole ps -ef... (5 Replies)
Discussion started by: knc9233
5 Replies

9. Shell Programming and Scripting

using cut command with sort

how to cut for pattern in the file and then count each occurance? say, each line has unique pattern and u want to grep but at last, you want to see how many of them occur? say, cut -d'\" -f15 filename | sort -? or.. do i need to use sed or something.. i need to count lets say how... (5 Replies)
Discussion started by: hankooknara
5 Replies

10. UNIX for Dummies Questions & Answers

Help with Last,uniq, sort and cut

Using the last, uniq, sort and cut commands, determine how many times the different users have logged in. I know how to use the last command and cut command... i came up with last | cut -f1 -d" " | uniq i dont know if this is right, can someone please help me... thanks (1 Reply)
Discussion started by: jay1228
1 Replies
Login or Register to Ask a Question