LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
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!

Notices


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

Rep: Reputation: 0
awk question


Hi,
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?

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

Rep: Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976
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:
Code:
awk '{if ( FILENAME == "file1" ) print $1,$5; else print $5}' file1 file2 file3
 
Old 05-25-2009, 03:17 AM   #3
sycamorex
LQ Veteran
 
Registered: Nov 2005
Location: London
Distribution: Slackware64-current
Posts: 5,811
Blog Entries: 1

Rep: Reputation: 1191Reputation: 1191Reputation: 1191Reputation: 1191Reputation: 1191Reputation: 1191Reputation: 1191Reputation: 1191Reputation: 1191
To read each file in a directory you need a 'for' loop:
Code:
for file in *
  do
  # do_something_with_the $file, for example:
  awk -F: '{ print $5} $file >> newfile
  done
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 05:30 AM.
 
Old 05-25-2009, 05:29 AM   #4
groys
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.
 
  


Reply


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 07:23 AM
awk question davidkline Linux - General 9 09-29-2006 01:37 PM
awk question denalitastic Linux - Newbie 1 06-07-2005 11:42 AM
AWK question paraiso Linux - Newbie 5 05-12-2005 02:37 PM
awk question dilberim82 Programming 2 10-01-2003 08:12 PM


All times are GMT -5. The time now is 03:36 PM.

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