Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I have a text file in the following format:

Surname, Forename: Day.Month.Year: Degree

Sellen, Jo: 03.07.1986: MSc CSE

Parfitt, Harry: 20.03.1984: MSc IT

How can I write a shell script that uses awk to read the file (class.txt) like this:

awk '{gsub(/ +/,"\t");print}' class.txt

and outputs the data in the following format, that exchanges the second and third column:

Surname Forename MSc stream Date of Birth

Sellen Jo MSc CSE 03.07.1986 

Parfitt Harry MSc IT 20.03.1984

(Don't worry if tabbed columns don't line up)

share|improve this question
5  
This site is not for getting other people to do your CS homework for you. Please spend some time working through some awk tutorials on the web and then come back here if you have specific usage questions. –  Caleb Apr 25 '11 at 13:54
1  
@Caleb I left this open because we've generally allowed "script this for me questions" (at least small ones like this), but you can bring it up on meta if you think we should get rid of them –  Michael Mrozek Apr 25 '11 at 14:22
    
@Michael: Thanks for the explanation. I might just do that. –  Caleb Apr 25 '11 at 15:49
    
possible duplicate of My awk program to change spaces into tabs doesn't work –  Mikel Apr 26 '11 at 0:28
1  
@Caleb: On the other sites you get "clues" to the solution if you tag the question with "homework", why not the same here? –  Johan Apr 26 '11 at 6:26
add comment

2 Answers

It appears your fields are separated with colon-space, so you'll need to account for that in awk's FS variable. You're also manipulating the order of the fields, so you'll want to set OFS in the BEGIN section.

share|improve this answer
add comment

You can try something like this code:

awk 'BEGIN { FS="[,:]" } { print $1, $2, $4, $5, $3 }' input_file

Here, we specify multiple field separators (, and :) using the AWK built in variable, FS. Multiple field separators are enclosed within [].

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.