Path: utzoo!utgpu!news-server.csri.toronto.edu!bonnie.concordia.ca!thunder.mcrcim.mcgill.edu!mouse From: mouse@thunder.mcrcim.mcgill.edu (der Mouse) Newsgroups: comp.unix.questions Subject: Re: file manipulation (easy question I think - REWORDED) Keywords: awk sed Message-ID: <1991Jun20.092927.160@thunder.mcrcim.mcgill.edu> Date: 20 Jun 91 09:29:27 GMT References: <1991Jun18.014539.22085@msuinfo.cl.msu.edu> <1991Jun18.195126.9916@gorm.ruc.dk> Organization: McGill Research Centre for Intelligent Machines Lines: 23 In article <1991Jun18.195126.9916@gorm.ruc.dk>, jba@gorm.ruc.dk (Jan B. Andersen) writes: > jpd@tardis.cl.msu.edu (Joe P. DeCello) writes: >> I would like to be able to output the first field of each line into >> a new file. I would like these fields to be on one line in the new >> file and separated by commas. > Easy. We'll use cut(1) to select field no. 1 using ':' as the > delimiter, and will then use tr(1) to translate the newlines into > commas: > $ cat OLDFILE | cut -d: -f1 | tr "\012" "," > NEWFILE > The only problem is what to do with the last comma? Not the only problem; you're also missing the trailing newline. < OLDFILE awk -F: 'BEGIN { pref = ""; } { printf("%s%s",pref,$1); pref = ","; } END { printf("\n"); }' > NEWFILE der Mouse old: mcgill-vision!mouse new: mouse@larry.mcrcim.mcgill.edu