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...
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
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?
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