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)

  • 10
    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
  • 2
    @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
  • 2
    @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

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.


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 [].

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