Linux - NewbieThis 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
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Hi,
i'm using awk inside awk(via command substitution) and I want to apply the FS of the outer awk to inner awk..
My main goal is to actually insert a line into a table conforming to the format of that table :
Eg :- i ve two files : (i wasnt able to show multiple spaces as they were getting truncated, so i have indicated a space with <space>
---------------------------------------------------------
cat f1
hdr1<space>hdr2<space>hdr3<space>hdr4
Sorry I wasnt very clear about this.. The second file (f2) doesnt have a uniform spacing like (2 spaces between the cols), it can have a more random format like :
So basically there's no clarity on the spacings.. All that i'm sure of is, both the files have the same headers and I need to merge the (only) line from one file to the another file having many lines (with same column headers)..
Ok i really am finding it hard to convey the intended format.. In edit mode, it looks ok but once posted the format is changing.. Anyway i hope i've given a fair idea of what i'm looking for.. In the above sample data, the only thing to keep in mind for column values is, they start from where it's corresponding column header starts.. barring that the above sample data is fine..
Appreciate your helP!!
Thanks!
Last edited by colucix; 05-24-2012 at 06:27 PM.
Reason: Added CODE tags as per request in post #14
In the above sample data, the only thing to keep in mind for column values is, they start from where it's corresponding column header starts.. barring that the above sample data is fine..
In your words there is the solution. Just check where the headers start and set the proper format for each field. This assumes that all the headers are different from each other, otherwise the index function fails because it returns the starting position of the leftmost occurrence.
Code:
awk 'BEGIN {
getline < ARGV[2]
for ( i = 1; i <= NF; i++ )
n[i] = index($0, $i)
for ( i = 2; i <= NF; i++ )
f[i-1] = ( "%-" n[i]-n[i-1] "s" )
f[i-1] = "%-s"
print
print ""
}
FNR > 2 {
for ( i = 1; i <= NF; i++ )
printf f[i], $i
print ""
}' f1 f2
Notice that the arguments must be exactly in this order, f1 then f2. The BEGIN section reads the header of the second file to retrieve the format (and prints it followed by an empty line, as in your example). Then the content of f1 is printed out with the format retrieved from f2. Finally the content of f2 is appended. Hope this helps.
It didnt solve the formatting problem for me , when I gave the two files as inputs to the awk line you suggested...
Actually ... it provided exactly the output you initially requested, ie all data from both files with only a single header and 2 spaces between each data item.
Your new requirement to have the data formatted in columns is best suited to the column command:
colucix, grail, jhwilliams... Thanks so much!! I was struggling for a long time to get one and now I have 3 ways for it.. You guys are awKsome! .. Thanks!
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.