LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 11-13-2009, 11:42 AM   #1
paragkalra
Member
 
Registered: Sep 2006
Location: Nagpur, Maharashtra, India
Distribution: Debian, Ubuntu, Redhat, Fedora, SLES, OpenSUSE, FreeBSD, Mac OS X
Posts: 221

Rep: Reputation: 31
Using GAWK to combine files


Hello All,

I have a folder containing few files. Each & every file contain only 1 column.

I want to combine only column of all the files through GAWK, separate them by a delimiter and store it to a new file.

So basically using GAWK, I want to combine '$1' of all files, separate them by a delimiter and save it in a file:
i.e. '$1:$1:$1:$1:$1.....'
 
Old 11-13-2009, 01:31 PM   #2
tronayne
Senior Member
 
Registered: Oct 2003
Location: Northeastern Michigan, where Carhartt is a Designer Label
Distribution: Slackware 32- & 64-bit Stable
Posts: 3,541

Rep: Reputation: 1065Reputation: 1065Reputation: 1065Reputation: 1065Reputation: 1065Reputation: 1065Reputation: 1065Reputation: 1065
Have you tried paste? Heckuva lot easier...
 
Old 11-13-2009, 11:06 PM   #3
paragkalra
Member
 
Registered: Sep 2006
Location: Nagpur, Maharashtra, India
Distribution: Debian, Ubuntu, Redhat, Fedora, SLES, OpenSUSE, FreeBSD, Mac OS X
Posts: 221

Original Poster
Rep: Reputation: 31
I think I missed out one information....

Although all the files have 1 column but that 1 column may have multiple rows but number of rows in all the rows will remain same...
 
Old 11-14-2009, 04:26 AM   #4
paragkalra
Member
 
Registered: Sep 2006
Location: Nagpur, Maharashtra, India
Distribution: Debian, Ubuntu, Redhat, Fedora, SLES, OpenSUSE, FreeBSD, Mac OS X
Posts: 221

Original Poster
Rep: Reputation: 31
Ok got paste to serve the purpose...

However I was actually combining windows file format files which had windows specific line break characters. As a result of which columns were not visible on same row...

Hence had to convert the result file to unix file as shown below:

Quote:
paste -d\# Columns/*.out > Table.windows
tr -d '\15\32' < Table.windows > Table.unix
I have few more questions:

1. If I want to use tab as the delimiter then I guess following should be sufficient but somehow its not giving the desired results:
Quote:
paste -d Columns/*.out > Table.windows
Any pointers?

2. Is there a way I can use a long single delimiter like '##--##' or may be '&+&+&'
I tried following but it doesn't seem to work.
Quote:
paste -d '##--##' Columns/*.out > Table.windows
Any idea?
 
Old 11-14-2009, 07:43 AM   #5
paragkalra
Member
 
Registered: Sep 2006
Location: Nagpur, Maharashtra, India
Distribution: Debian, Ubuntu, Redhat, Fedora, SLES, OpenSUSE, FreeBSD, Mac OS X
Posts: 221

Original Poster
Rep: Reputation: 31
Ok got the answers -

1. Need to execute without '-d' to use default delimiter as 'tab'

2. I think following hack will do the trick....

Quote:
for file in `ls *.out`; do gawk 'BEGIN{FS = "##--##" }{print $1"##--#"}' $file > ../Mod_Columns/$file ; done
Changing FS as I am interested in the space characters after the columns otherwise GAWK will trim the white spaces...

Quote:
paste -d# Mod_Columns/*.out > Table.windows
Advantage of this is that there no need use 'tr' to remove line break characters - GAWK takes of care of it...
 
  


Reply



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
combine multiple files into one file? baddah Linux - Newbie 10 09-06-2011 11:22 AM
How would you combine files excluding the lines that are different? darcman Linux - Software 10 01-19-2009 07:23 PM
Combine and extract .rar files villumanati Linux - Newbie 2 09-10-2008 03:59 PM
combine shadow files packets Linux - Newbie 2 04-18-2008 05:14 AM
Best way to combine files? JockVSJock Programming 4 04-25-2004 07:38 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 02:07 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
Open Source Consulting | Domain Registration