LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
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 04-19-2024, 11:36 AM   #1
atjurhs
Member
 
Registered: Aug 2012
Posts: 316

Rep: Reputation: Disabled
reformatting a file with columns using AWK


hi guys,

i wrote an AWK script that prints out data using this command
Code:
printf("\n%s,%s,%.5f,%.5f,%s",aray1,aray2,aray3,aray4,aray5)
it gives me output that looks like
Code:
fieldname1,val11,fieldname2,val21,fieldname3,val31,fieldname4,val41,fieldname5,val51
fieldname1,val12,fieldname2,val22,fieldname3,val32,fieldname4,val42,fieldname5,val52
fieldname1,val13,fieldname2,val23,fieldname3,val33,fieldname4,val43,fieldname5,val53
fieldname1,val14,fieldname2,val24,fieldname3,val34,fieldname4,val44,fieldname5,val54
fieldname1,val15,fieldname2,val25,fieldname3,val35,fieldname4,val45,fieldname5,val55
fieldname1,val16,fieldname2,val26,fieldname3,val36,fieldname4,val46,fieldname5,val56
i need my output to look like
Code:
fieldname1,fieldname2,fieldname3,fieldname4,fieldname5
val11,val21,val31,val41,val51
val12,val22,val32,val42,val52
val13,val23,val33,val43,val53
val14,val24,val34,val44,val54
etc.
i don't know how to "transpose" the data. maybe it would be better to use a different kind of print statement in my AWK script? or maybe easier to write a another AWK script to do it, idk?

guidance would be much appreciated, thank you
 
Old 04-19-2024, 11:49 AM   #2
blunix2
LQ Newbie
 
Registered: Apr 2024
Posts: 12

Rep: Reputation: 1
Can you paste us some example input please, and the complete awk script
 
Old 04-19-2024, 12:25 PM   #3
atjurhs
Member
 
Registered: Aug 2012
Posts: 316

Original Poster
Rep: Reputation: Disabled
i would love to, but they are on a separate system that i can't get data on or off. i have to manually type back and forth between here and there.
 
Old 04-21-2024, 04:55 AM   #4
MadeInGermany
Senior Member
 
Registered: Dec 2011
Location: Simplicity
Posts: 2,806

Rep: Reputation: 1207Reputation: 1207Reputation: 1207Reputation: 1207Reputation: 1207Reputation: 1207Reputation: 1207Reputation: 1207Reputation: 1207
Each arayN is a name,val tuple?
Then the following is a way:
Code:
function tupleprintf(fmt, tup1, tup2, tup3, tup4, tup5) {
# Split each tupN at the comma to ArN[]
  split(tup1, Ar1, ",")
  split(tup2, Ar2, ",")
  split(tup3, Ar3, ",")
  split(tup4, Ar4, ",")
  split(tup5, Ar5, ",")
  if (!contd) {
# First elements (names) make the header; print once
    printf fmt, Ar1[1], Ar2[1], Ar3[1], Ar4[1], Ar5[1]
    contd=1
  }
# Print second elements (values)
  printf fmt, Ar1[2], Ar2[2], Ar3[2], Ar4[2], Ar5[2]
}

...

{
#printf("\n%s,%s,%s,%s,%s",aray1,aray2,aray3,aray4,aray5)
# Print all as %s strings
tupleprintf("\n%s,%s,%s,%s,%s",aray1,aray2,aray3,aray4,aray5)
}
 
1 members found this post helpful.
Old 04-21-2024, 06:39 AM   #5
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,008

Rep: Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193
I am curious, of the several hundred search responses, either on google or even just here in LQ, which ones did you try before deciding that your data was too unique for any of the solutions?
 
Old 04-22-2024, 06:08 AM   #6
atjurhs
Member
 
Registered: Aug 2012
Posts: 316

Original Poster
Rep: Reputation: Disabled
grail, i did not know what to search for. if you could give me some clues, i would certainly do the searching.

Last edited by atjurhs; 04-22-2024 at 06:10 AM.
 
Old 04-22-2024, 06:24 AM   #7
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 21,138

Rep: Reputation: 4122Reputation: 4122Reputation: 4122Reputation: 4122Reputation: 4122Reputation: 4122Reputation: 4122Reputation: 4122Reputation: 4122Reputation: 4122Reputation: 4122
I can sympathise with the OP - you almost need to know the answer to the question to be able to frame the query.

Albeit, the comment in post #2 are cogent - we can't provide assistance in the dark. Nice response from MadeInGermany as I have come to expect.
 
Old 04-22-2024, 06:45 AM   #8
sundialsvcs
LQ Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 10,671
Blog Entries: 4

Rep: Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945
When I have to do things like this, I turn to the perl programming language. Which grew out of "awk" when a guy named Larry Wall decided that he could improve it. It is definitely a "quirky" language, but extremely powerful – especially for any sort of "text-file processing."

One important thing that this language has, which "awk" does not, is a vast(!) library of packages (called "CPAN") which do "most of the things that you really need to do" right out of the box.
 
Old 04-22-2024, 06:54 AM   #9
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,923

Rep: Reputation: 7319Reputation: 7319Reputation: 7319Reputation: 7319Reputation: 7319Reputation: 7319Reputation: 7319Reputation: 7319Reputation: 7319Reputation: 7319Reputation: 7319
Quote:
Originally Posted by syg00 View Post
I can sympathise with the OP - you almost need to know the answer to the question to be able to frame the query.
The most important part is to split the task into simple steps.
print header, print values, loop on input ... print etc.
At the end you will get something similar to post #4, which is a relatively compact implementation.
 
Old 04-22-2024, 10:42 AM   #10
atjurhs
Member
 
Registered: Aug 2012
Posts: 316

Original Poster
Rep: Reputation: Disabled
pan64, that's what i did, worked great, thanks

Last edited by atjurhs; 04-22-2024 at 10:43 AM.
 
Old 04-24-2024, 10:54 AM   #11
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,008

Rep: Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193
I know we have a solution, but thought I would throw an awk one in for the hell of it:
Code:
#!/usr/bin/awk -f

BEGINFILE { RS="[,\n]"
            PROCINFO["sorted_in"] = "@ind_str_asc"
}

FNR % 2 == 1 && FNR<10{
  header = (header?header",":"") $0
}

FNR % 10 == 1{ ++cnt }

FNR % 2 == 0{ arr[cnt] = (arr[cnt]?arr[cnt]",":"") $0 }

END{
  print header
  for(idx in arr)
    print arr[idx]
}
Tis can of course be done on one line, but used this format for some clarity
 
1 members found this post helpful.
  


Reply

Tags
awk, csv



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
transpose selected groups of rows of columns into multiple columns (arrays) with other columns retained to single row-group TempleStone4510! Linux - General 15 08-22-2022 06:52 PM
[SOLVED] Converting a file with Rows and Columns to just Columns mphillips67 Linux - Newbie 14 03-05-2014 10:31 AM
How to manipulate a range of columns in a file using awk ksvinaykumar Linux - Newbie 8 04-10-2012 09:11 AM
[SOLVED] AWK: add columns while keep format for other columns cristalp Programming 3 10-13-2011 06:14 AM
Text file manipulation: selecting specific lines/columns using awk and print CHARL0TTE Linux - Newbie 2 02-27-2010 02:40 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 10:42 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