Print Nth to last field


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Print Nth to last field
# 1  
Old 11-09-2012
Print Nth to last field

Hey,

I'm sure this is answered somewhere but my Googling has turned up nothing. I have a file with data in the following format:

Code:
<desription of event> at <time and date>

The desription of the event is variable length and hence when the list is displayed it is hard to easily see the date (and it looks messy). To resolve this, I would like to print the information with the time and date first followed by the description and just ditch the word at. For example:

Code:
Layout in file:

Eat some pies at Thu Oct 11 03:52:30 GMT 2012
Got stuck on this code at Thu Oct 11 04:51:30 GMT 2012
Eat many more pies at Thu Oct 11 05:52:30 GMT 2012

desired printed output:

Thu Oct 11 03:52:30 GMT 2012 Eat some pies
Thu Oct 11 04:51:30 GMT 2012 Got stuck on this code
Thu Oct 11 05:52:30 GMT 2012 Eat many more pies

I can do this easily using awk when I know the length of each line but I don't.

Is there a way to get awk to count from the end rather than the start as the date will always be the same length?

Alternatively is there a way to switch the text either side of "at" as I don't think "at" will ever appear in any of the real descriptions?

If anyone can help, it would be really appreciated.

Thanks

Last edited by RECrerar; 11-09-2012 at 11:15 AM.. Reason: Typos
# 2  
Old 11-09-2012
Code:
awk 'match($0, " at *" $(NF-5)){print substr($0, RSTART+4) substr($0,1, RSTART)}' myFile

# 3  
Old 11-09-2012
Perl:
Code:
perl -lpe 's/(.*) at (.*)/$2 $1/' file

# 4  
Old 11-09-2012
bartus11: Thanks, but we're not meant to use perl in our scripts - I don't know why it's just part of the standards (I should have mentioned that before.

vgersh99: That syntax errors ("near line 1"), I'm trying to work from your suggestion at the moment though.

A couple of other things I should have mentioned before:

1: The script is in ksh and it has to work on both Linux and Solaris.
2: I do not have multiple lines in one file but multiple files each containing only one line. I don't know if that makes a difference.

Thanks for the suggestions so far.

Last edited by RECrerar; 11-09-2012 at 01:01 PM.. Reason: Add more information
# 5  
Old 11-09-2012
use 'nawk' instead of 'awk' on Solaris
# 6  
Old 11-09-2012
sed?
Code:
sed '/ at /s/\(.*\) at \(.*\)/\2 \1/' infile

This User Gave Thanks to rdrtx1 For This Post:
# 7  
Old 11-09-2012
Quote:
Originally Posted by rdrtx1
sed?
Code:
sed '/ at /s/\(.*\) at \(.*\)/\2 \1/' infile

Dude that is perfect! Thanks so so much.
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Print lines based upon unique values in Nth field

For some reason I am having difficulty performing what should be a fairly easy task. I would like to print lines of a file that have a unique value in the first field. For example, I have a large data-set with the following excerpt: PS003,001 MZMWR/ L-DWD// * PS003,001... (4 Replies)
Discussion started by: jvoot
4 Replies

2. UNIX for Advanced & Expert Users

Help changing date format in the nth field

Hi, I have two (2) things that I want to do. First is to change the date format that is in the nth field from MM/DD/YY to YY/MM/DD. Preferably, I wish I know how to make it a 4-digit year but I don't. Problem is I can only assume it is a 20 century Second is somehow know how to figure out... (1 Reply)
Discussion started by: newbie_01
1 Replies

3. Shell Programming and Scripting

Replace pattern from nth field from a file

I have posted this again as old post is closed and I am not able to reopen. so please consider this new post Input File : 1,A,Completed,06.02_19.36,Jun 30 20:00 2,BBB,Failed,07.04_05.12,Jul 21 19:06 3,CCCCC,New,07.21_03.03,Jul 26 12:57 4,DDDDD,Pending,, I wast output file as: ... (7 Replies)
Discussion started by: Amit Joshi
7 Replies

4. Shell Programming and Scripting

Replace a value of Nth field of nth row

Using Awk, how can I achieve the following? I have set of record numbers, for which, I have to replace the nth field with some values, say spaces. Eg: Set of Records : 4,9,10,55,89,etc I have to change the 8th field of all the above set of records to spaces (10 spaces). Its a delimited... (1 Reply)
Discussion started by: deepakwins
1 Replies

5. Shell Programming and Scripting

Nth field should have space and rest commas

Hello I was working on a script where the output of my file is 1234 4567 8973 43214 78965 I need the value in below format of this file.The nth field should have space instead of ,(comma) 1234,4567,8973,43214 78965 I tried the code but not working completely xargs <temp_PP.7250... (3 Replies)
Discussion started by: anuragpgtgerman
3 Replies

6. Shell Programming and Scripting

Awk product of nth field

How do I do the product of nth filed just like sum. For sum I know like awk '{ sum += $12 } END {printf "%.2f\n", sum}' works as initial sum = 0. But for product how do initialize the variable to 1? (2 Replies)
Discussion started by: madhavb
2 Replies

7. UNIX for Dummies Questions & Answers

change field separator only from nth field until NF

Hi ! input: 111|222|333|aaa|bbb|ccc 999|888|777|nnn|kkk 444|666|555|eee|ttt|ooo|ppp With awk, I am trying to change the FS "|" to "; " only from the 4th field until the end (the number of fields vary between records). In order to get: 111|222|333|aaa; bbb; ccc 999|888|777|nnn; kkk... (1 Reply)
Discussion started by: beca123456
1 Replies

8. Shell Programming and Scripting

How to Print from nth field to mth fields using awk

Hi, Is there any short method to print from a particular field till another filed using awk? Example File: File1 ==== 1|2|acv|vbc|......|100|342 2|3|afg|nhj|.......|100|346 Expected output: File2 ==== acv|vbc|.....|100 afg|nhj|.....|100 (8 Replies)
Discussion started by: machomaddy
8 Replies

9. Shell Programming and Scripting

Need help to get the nth field of the variable

Below is the format of my file which consist of 1)BSB 2)BSB/ADS 3)CIB 4)CIB/CRH 5)CIB/DCC 6)CIB/EMD 7)CIB/GDSPresentation 8)CIB/HCH 9)CIB/HSM 10)CIB/MCH 11)CIB/RCH 12)COB 13)DCI 14)DIB 15)DIB/H2H 16)DIB/HotelSync 17)DIB/UADBA (11 Replies)
Discussion started by: rohit22hamirpur
11 Replies

10. Shell Programming and Scripting

awk substitute variable value in nth field

I have a large csv file that looks like this: The 3rd field is a unix time stamp that I want to convert to human readable. I wrote a bash script with this code: IFS=$',' cat $1 | while read ID user DATE text flags read; do echo -e "$ID,$user,$(date -d @$DATE),$text,$flags,$read... (3 Replies)
Discussion started by: stumpyuk
3 Replies
Login or Register to Ask a Question