Visit Our UNIX and Linux User Community


Transposing text using AWK


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Transposing text using AWK
# 1  
Old 06-10-2011
Transposing text using AWK

Well I thought that this would be a fairly straight forward operation but perhaps I'm complicating it more in my mind. I have a simple data.txt file containing candidate information in the following format:
Code:
"ANTONIA ROJAS SALES"	"Feminino"
"13/06/1954"	"Casado(a)"
"Brasileira (naturalizada)"	"CRUZEIRO DO SUL / AC"
"Superior completo"	"Deputado"
"Partido do Movimento Democrático Brasileiro -
"ANTONIO NIZOMAR DOS SANTOS"	"Masculino"
.....

Wanting to transpose each candidate into a *single* row looking like:
Code:
"ANTONIA ROJAS SALES"	"Feminino"	"13/06/1954"	"Casado(a)" Brasileira (naturalizada)"	"CRUZEIRO DO SUL / AC"     "Superior completo"	"Deputado"   "Partido do Movimento Democrático Brasileiro

Current code looks like this:
Code:
awk '{printf "%-15s ", $1}!(NR % 5){printf "\n"}' done.txt

however this only returns the first field, when I want the entire string:
Code:
"ANTONIA        "13/06/1954"    "Brasileira     "Superior       "Partido

Any ideas? Thanks!

Last edited by Franklin52; 06-10-2011 at 03:28 PM.. Reason: adding code tags
# 2  
Old 06-10-2011
Good afternoon (boa tarde), I have a question:
How many fields each "candidate" has?
# 3  
Old 06-10-2011
Quote:
Originally Posted by daveyabe
Current code looks like this:
Code:
awk '{printf "%-15s ", $1}!(NR % 5){printf "\n"}' done.txt

however this only returns the first field, when I want the entire string:
Code:
"ANTONIA        "13/06/1954"    "Brasileira     "Superior       "Partido

Any ideas? Thanks!
Code:
awk '{printf "%-15s ", $0}!(NR % 5){printf "\n"}' done.txt

# 4  
Old 06-10-2011
So records are always 5 lines?

Code:
awk '{ printf("%s\t", $0); if((NR%5)==0) printf("\n"); }' < data

# 5  
Old 06-10-2011
Yes, records are always 5 lines.
# 6  
Old 06-10-2011
Here is one possible solution:
Code:
sed 'N;N;N;N;s/\n/ /g' input_file

Good luck (boa sorte).
# 7  
Old 06-10-2011
Actually, I am embarrassed to say this, but I think my console's width was throwing off what I thought the results were...since I've got thousands of records. I began dumping them into a log file and everything looks good. Thanks for all the help guy, much appreciated!

Previous Thread | Next Thread
Test Your Knowledge in Computers #112
Difficulty: Easy
macOS was not the largest Unix largest installed base in 2017.
True or False?

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk to skip lines find text and add text based on number

I am trying to use awk skip each line with a ## or # and check each line after for STB= and if that value in greater than or = to 0.8, then at the end of line the text "STRAND BIAS" is written in else "GOOD". So in the file of 4 entries attached. awk tried: awk NR > "##"' "#" -F"STB="... (6 Replies)
Discussion started by: cmccabe
6 Replies

2. Shell Programming and Scripting

Awk/sed script for transposing any number of rows with header row

Greetings! I have been trying to find out a way to take a CSV file with a large number of rows, and a very large number of columns (in the thousands) and convert the rows to a single column of data, where the first row is a header representing the attribute name and the subsequent series of... (3 Replies)
Discussion started by: tntelle
3 Replies

3. Shell Programming and Scripting

Transposing a file

Hi Guys, I have file containing this kind of format below: ======== MOBILITY EVENT (G): ATTACH REJECT ========= Time : <date_time> Node : <node> GMM Cause : <code> Details : <details> Attach : <attach type> IMSI : <imsi> PTMSI : <ptmsi> RA New : <ra new> RA... (9 Replies)
Discussion started by: rymnd_12345
9 Replies

4. Shell Programming and Scripting

awk, string as record separator, transposing rows into columns

I'm working on a different stage of a project that someone helped me address elsewhere in these threads. The .docs I'm cycling through look roughly like this: 1 of 26 DOCUMENTS Copyright 2010 The Age Company Limited All Rights Reserved The Age (Melbourne, Australia) November 27, 2010... (9 Replies)
Discussion started by: spindoctor
9 Replies

5. Shell Programming and Scripting

Transposing columns with awk

I want a sweet simple time efficient awk script in online which gets output 001_r 0.0265185 0.0437049 0.0240642 0.0310264 0.0200482 0.0146746 0.0351344 0.0347856 0.036119 1.49 firstcoloumnvalue allvaluesof 'c' in one row 001_r : 002_r c: 0.0265185 N: 548 001_r : 007_r c:... (5 Replies)
Discussion started by: phoenix_nebula
5 Replies

6. Shell Programming and Scripting

Transposing a file

Hi All, I have a input file say FILEA. FILEA -------- empid1 sal1 location1 manager1 empid2 sal2 location2 manager2 empid3 sal3 location3 manager3 . . . (3 Replies)
Discussion started by: 46019
3 Replies

7. UNIX Desktop Questions & Answers

More than transposing!

Hi everyone, I have a poblem like that: I have a file which includes data looks like: 0.65214 0.3597 1.0 0.65244 0.3502 1.0 0.65273 0.3553 1.0 0.65305 0.3544 1.0 0.65327 0.3505 1.0 0.65359 0.3516 1.0 0.65578 0.6464 1.0 0.65605 0.6453 1.0 0.65633 0.6437 1.0 0.65660 0.6488 1.0... (3 Replies)
Discussion started by: bulash
3 Replies

8. Shell Programming and Scripting

Transposing string

Hello guys, can some please give driection as to how to archieve this big issue i am having: I a random number that has been generated and wants the user to guess it, let me illustrate this: say the random i hve generated is 'nice' - i then hide it from the user and display it in ----... (3 Replies)
Discussion started by: unibboy
3 Replies

9. Shell Programming and Scripting

Major Awk problems (Searching, If statements, transposing etc.)

Please bare with me as task is very detailed and I'm extremely new to Awk/sed. Keep in mind I'm running windows so I'm using a dos prompt The attachment is a server report that I'm trying to manipulate with little success. For each server, I need to take the most recent information about them... (2 Replies)
Discussion started by: Blivo
2 Replies

10. Shell Programming and Scripting

file transposing

Hello, Is there a way to transpose a file in shell scripting? For instance, from a1 a2 a3 a4 a5 a6 a7 .... b1 b2 b3 b4 b5 b6 b7 .... c1 c2 c3 c4 c5 c6 c7 .... d1 d2 d3 d4 d5 d6 d7 ... ... ... ... to a1 b1 c1 d1 .... a2 b2 c2 d2 .... a3 b3 c3 d3 .... a4 b3 c3 d4 .... ... ... (24 Replies)
Discussion started by: mskcc
24 Replies

Featured Tech Videos