LinuxQuestions.org
Share your knowledge at the LQ Wiki.
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-27-2013, 03:29 PM   #1
wilelucho
LQ Newbie
 
Registered: Jun 2012
Location: Brazil
Distribution: Ubuntu
Posts: 19

Rep: Reputation: Disabled
transposing rows to columns


Hi all,

I have the following problem,

I generated a file with 253 columns and 11323 rows, I want to generate separate files from each row, at the same time I need every file to be a single-column file. I tried with the following script to transpose both, the main matrix and the single row files that I also generated
Code:
#! /bin/sh
# Transpose a matrix: assumes all lines have same number
# of fields

exec awk '
NR == 1 {
	n = NF
	for (i = 1; i <= NF; i++)
		row[i] = $i
	next
}
{
	if (NF > n)
		n = NF
	for (i = 1; i <= NF; i++)
		row[i] = row[i] " " $i
}
END {
	for (i = 1; i <= n; i++)
		print row[i]
}' ${1+"$@"}
Nevertheless it gives me 11323 files (which is correct) with 251 rows instead of 253 as expected. I'm pretty new to awk, I used this script in the past and worked fine, I'm puzzled

Luis
-------------
Working on Ubuntu 12.04 LTS
 
Old 03-27-2013, 03:56 PM   #2
colucix
Moderator
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957
Hi.
Quote:
Originally Posted by wilelucho View Post
I'm pretty new to awk, I used this script in the past and worked fine, I'm puzzled
This make me think there's something weird in the input file. Formally the code looks correct (except it doesn't actually creates the files, does it?). Any chance the input file has been created on a windows system and/or has some hidden control character that triggers this strange behaviour?
 
1 members found this post helpful.
Old 03-27-2013, 04:16 PM   #3
danielbmartin
Senior Member
 
Registered: Apr 2010
Location: Apex, NC, USA
Distribution: Ubuntu
Posts: 1,167

Rep: Reputation: 306Reputation: 306Reputation: 306Reputation: 306
With this InFile ...
Code:
apple red honda panama
banana yellow toyota mexico
cherry blue subaru canada
fig brown bmw italy
lemon green volvo ireland
mango pink nissan china
peach tan chevrolet germany
grape black ford sweden
... this code ...
Code:
#!/bin/bash
#   Daniel B. Martin   Mar13
#
#   To execute this program, launch a terminal sesson and enter:
#   bash /home/daniel/Desktop/LQfiles/dbm712.bin
#
# This program inspired by:   
#   http://www.linuxquestions.org/questions/programming-9/
#     transposing-rows-to-columns-4175455825/                                

# File identification
   Path=$(readlink -f $0 | cut -d'.' -f1)
OutFile=$Path"out.txt"
 InFile=$Path"inp.txt"

awk '{gsub("[ \t]","\n"); print > "'"$OutFile"'"++k}' $InFile

echo; echo "Normal end of job."; echo; exit
... produces output files such as these ...
out.txt1 ...
Code:
apple
red
honda
panama
out.txt2 ...
Code:
banana
yellow
toyota
mexico
et cetera ...

This code copes with tab characters which may be in the input file.
All files (InFile, code, and the multiple OutFiles) will be in the same folder.
On my machine all files have similar names, including dbm712.
You would specify complete file identifiers appropriate to your application.

Daniel B. Martin

Last edited by danielbmartin; 03-27-2013 at 10:25 PM. Reason: Improved code
 
1 members found this post helpful.
Old 03-28-2013, 06:25 AM   #4
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950
No need to get all matrix-y here. All you really want to do is convert the space delimiters on each line into newlines, and print each line to a separate file.

Code:
awk '{ for (i=1;i<=NF;i++){ print $i > "file" NR ".txt" } }' infile.txt
There're probably easier ways to do it too, but this is the best I could do at short notice. Using gsub to convert the spaces to newlines would be another option.

And just to round it out, here's a quick bash loop too:
Code:
n=1
while read -ra line; do
    printf '%s\n' "${line[@]}" >"file$((n++)).txt"
done <infile.txt

Last edited by David the H.; 03-28-2013 at 06:36 AM. Reason: forgot something
 
1 members found this post helpful.
Old 03-28-2013, 10:16 AM   #5
grail
Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 7,698

Rep: Reputation: 1988Reputation: 1988Reputation: 1988Reputation: 1988Reputation: 1988Reputation: 1988Reputation: 1988Reputation: 1988Reputation: 1988Reputation: 1988Reputation: 1988
Even less need actually:
Code:
awk '{$1=$1;print > "file" NR ".txt"}' OFS="\n" file
 
3 members found this post helpful.
Old 03-29-2013, 07:18 AM   #6
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950
I knew it! Once again, grail comes through with the shortest solution.

Let me guess. Trying to print $0 alone fails, because it stores the original line intact, field separators and all. But if you modify the line in any way, then the OFS setting will apply to it. So just set one field to itself and suddenly it starts printing with newlines between them.

Now why didn't I think of that?
 
Old 03-29-2013, 09:10 AM   #7
grail
Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 7,698

Rep: Reputation: 1988Reputation: 1988Reputation: 1988Reputation: 1988Reputation: 1988Reputation: 1988Reputation: 1988Reputation: 1988Reputation: 1988Reputation: 1988Reputation: 1988
It's ok David ... you get the kudos for all the other stuff .. I just get the awk one's now and then
 
  


Reply

Tags
awk


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
[SOLVED] Creating columns from every 4 rows kbensch Linux - General 7 05-19-2012 12:35 PM
Formatting rows and columns kdelover Programming 4 08-31-2010 03:56 AM
a2ps - How to get 80 rows by 130 columns packer_fan Linux - Software 1 08-28-2010 12:06 PM
columns & rows Ammad Linux - General 1 08-08-2005 05:02 AM
rows and columns digitalgravy Linux - General 2 03-16-2004 07:47 PM


All times are GMT -5. The time now is 05:17 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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration