LinuxQuestions.org
Share your knowledge at the LQ Wiki.
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 03-10-2007, 10:06 AM   #1
ovince
Member
 
Registered: Mar 2007
Posts: 77

Rep: Reputation: 15
making two column-data


hi

I have file like

f1
g1
f2
g2
...
f10
g10

and I would like to make 2-column data like

f1 g1
f2 g2
....
f10 g10

I remember that I read somewhere that this is easily done with cat command. Something like

cat -2 fileName

but I can not find exactly how. Could somebody refresh my memory?

thanks
oliver
 
Old 03-10-2007, 10:18 AM   #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
I think you're talking about paste?

For example,
Code:
paste f1 g1 > f1g1
 
Old 03-10-2007, 10:23 AM   #3
ovince
Member
 
Registered: Mar 2007
Posts: 77

Original Poster
Rep: Reputation: 15
ahhh ... I was so unprecise

I have 1 file with content

f1
g1
f2
g2
...
f10
g10

I would like to make another file from it with content

f1 g1
f2 g2
....
f10 g10


paste will work job
 
Old 03-10-2007, 10:23 AM   #4
ovince
Member
 
Registered: Mar 2007
Posts: 77

Original Poster
Rep: Reputation: 15
paste will work job? how?
 
Old 03-10-2007, 10:52 AM   #5
cfaj
Member
 
Registered: Dec 2003
Location: Toronto, Canada
Distribution: Mint, Mandriva
Posts: 221

Rep: Reputation: 31
Quote:
Originally Posted by ovince
ahhh ... I was so unprecise

I have 1 file with content

f1
g1
f2
g2
...
f10
g10

I would like to make another file from it with content

f1 g1
f2 g2
....
f10 g10


paste will work job
Code:
paste -d ' ' - - FILENAME > NEWFILE
 
Old 03-10-2007, 11:09 AM   #6
ovince
Member
 
Registered: Mar 2007
Posts: 77

Original Poster
Rep: Reputation: 15
thanks for reply

what if I wanna make 20 column data? do I write like

paste -d ' ' - - - - - - - - - - - - - - - - - - - - - ... FILENAME > NEWFILE
 
Old 03-10-2007, 11:14 AM   #7
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
Oops, didn't read that properly. ONE file, not many -- sorry about that.
 
Old 03-10-2007, 12:01 PM   #8
ovince
Member
 
Registered: Mar 2007
Posts: 77

Original Poster
Rep: Reputation: 15
I ask this because I remember in cat the 'column number' can be given by number
 
Old 03-10-2007, 12:42 PM   #9
pwc101
Senior Member
 
Registered: Oct 2005
Location: UK
Distribution: Slackware
Posts: 1,847

Rep: Reputation: 128Reputation: 128
This is an ugly way of doing it (and it assumes you're looking for things which have "f" and "g" in them which are unique):
Code:
grep f input_file > 1.tmp && grep g input_file > 2.tmp && paste 1.tmp 2.tmp $$ \rm 1.tmp 2.tmp
I'm pretty certain that it can be done a lot more efficiently using awk, but I don't know enough awk to get it to work. Just in case you want to look into it more, I've got awk to print 1 column of the input file based on some unique indentifier in the column:
Code:
awk '/f/ {print $1}' input_file > output_file
What I can't get it to do is to sort the f's first, then the g's, something like:
Code:
awk '{if $1=f* print $1 else if $1=g* print $2}' input_file > output_file
Obviously that's totally wrong, but I'm sure it's possible.

Hope the first bit helps!
 
Old 03-10-2007, 02:13 PM   #10
cfaj
Member
 
Registered: Dec 2003
Location: Toronto, Canada
Distribution: Mint, Mandriva
Posts: 221

Rep: Reputation: 31
Quote:
Originally Posted by ovince
thanks for reply

what if I wanna make 20 column data? do I write like

paste -d ' ' - - - - - - - - - - - - - - - - - - - - - ... FILENAME > NEWFILE
Try it and see.
 
Old 03-10-2007, 08:14 PM   #11
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
Quote:
Originally Posted by ovince
hi

I have file like

f1
g1
f2
g2
...
f10
g10

and I would like to make 2-column data like

f1 g1
f2 g2
....
f10 g10

I remember that I read somewhere that this is easily done with cat command. Something like

cat -2 fileName

but I can not find exactly how. Could somebody refresh my memory?

thanks
oliver
try this:
Code:
sed -e 'N' -e 's/\n/ /g' file
 
Old 03-11-2007, 01:18 AM   #12
ovince
Member
 
Registered: Mar 2007
Posts: 77

Original Poster
Rep: Reputation: 15
found it


cat fileName | -n 20


will do the job
 
Old 03-11-2007, 01:41 AM   #13
kshkid
Member
 
Registered: Dec 2005
Distribution: RHEL3, FC3
Posts: 383

Rep: Reputation: 30
Quote:
Originally Posted by ovince
found it


cat fileName | -n 20


will do the job
Am afraid, is there any command like '-n'

I dont think so?
 
Old 03-11-2007, 01:43 AM   #14
kshkid
Member
 
Registered: Dec 2005
Distribution: RHEL3, FC3
Posts: 383

Rep: Reputation: 30
in awk

Code:
awk '{ printf "%s ", $0; getline; printf "%s \n", $0 }' filename

Last edited by kshkid; 03-11-2007 at 01:50 AM.
 
Old 03-11-2007, 01:49 AM   #15
kshkid
Member
 
Registered: Dec 2005
Distribution: RHEL3, FC3
Posts: 383

Rep: Reputation: 30
in sed

Code:
sed 'h; N; h; {s/\n/ /g}' filename
 
  


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
Multiplying data column in awk - can rounding be suppressed? johnpaulodonnell Linux - Newbie 2 02-28-2007 07:56 AM
Parsing rows and column data from a file using perl dav_y2k Programming 1 10-08-2006 11:57 AM
'Last Post' Column showing incorrect data Shade LQ Suggestions & Feedback 1 04-20-2005 01:31 AM
Finding the name and data type of a column in SQL Travis86 Programming 11 08-15-2004 04:20 PM
Making new data cd Beppe83 Linux - Software 11 05-27-2004 03:09 PM

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

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

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