Latest LQ Deal: Linux Power User Bundle
Go Back > Forums > Linux Forums > Linux - Newbie
User Name
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!


  Search this Thread
Old 05-25-2009, 01:59 AM   #1
LQ Newbie
Registered: May 2009
Posts: 2

Rep: Reputation: 0
awk question

I have multiple files with data arranged in columns. I am trying to read create an new file, which will have the 1st column and 5th column from the 1st file and the 5th column from all the other files. Reading some other posts i wrote a poor script like this

#! /bin/bash
pr -m -t -s\ file1 file2 file3 file4 file5 file6 file7 | gawk '{print $1,$5,$12,$19,$26,$33,$40,$47,$54}'

How can I generalize this so that I don't have to hardcode these things? Is there anyway I can read each file in a folder and generate one output file which has the 5th column of each of the files?

Old 05-25-2009, 02:16 AM   #2
LQ Guru
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978
Awk can process multiple files together. If you want to extract the 1st and 5th field from one particular file, you can check the awk variable FILENAME:
awk '{if ( FILENAME == "file1" ) print $1,$5; else print $5}' file1 file2 file3
Old 05-25-2009, 02:17 AM   #3
LQ Veteran
Registered: Nov 2005
Location: London
Distribution: Slackware64-current
Posts: 5,819
Blog Entries: 1

Rep: Reputation: 1209Reputation: 1209Reputation: 1209Reputation: 1209Reputation: 1209Reputation: 1209Reputation: 1209Reputation: 1209Reputation: 1209
To read each file in a directory you need a 'for' loop:
for file in *
  # do_something_with_the $file, for example:
  awk -F: '{ print $5} $file >> newfile
It will go through each file in the current directory and append the fifth column to a new file 'newfile'
Is that what you mean?

Last edited by sycamorex; 05-25-2009 at 04:30 AM.
Old 05-25-2009, 04:29 AM   #4
LQ Newbie
Registered: May 2009
Posts: 2

Original Poster
Rep: Reputation: 0
thanks a lot, colunix and sycomorex...this is what i was trying to do.


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
awk question on handling *.CSV "text fields" in awk jschiwal Programming 8 05-27-2010 06:23 AM
awk question davidkline Linux - General 9 09-29-2006 12:37 PM
awk question denalitastic Linux - Newbie 1 06-07-2005 10:42 AM
AWK question paraiso Linux - Newbie 5 05-12-2005 01:37 PM
awk question dilberim82 Programming 2 10-01-2003 07:12 PM

All times are GMT -5. The time now is 10:47 AM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration