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 |
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.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
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.
|
 |
05-23-2012, 01:34 AM
|
#1
|
LQ Newbie
Registered: Jul 2011
Posts: 18
Rep: 
|
Awk inside Awk ?
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
abcd<space>bcef<space>pqrs<space>xyza
---------------------------------------------------------
cat f2
hdr1<space><space>hdr2<space><space>hdr3<space><space>hdr4
1111<space><space>2222<space><space>3333<space><space>4444
My desired output is :
----------------------------------------------------------
hdr1<space><space>hdr2<space><space>hdr3<space><space>hdr4
abcd<space><space>bcef<space><space>pqrs<space><space>xyza
1111<space><space>2222<space><space>3333<space><space>4444
----------------------------------------------------------
(observe that though headers are same in both files, spacing is different in the two files, so i want line in f1 in f2 with f2's format)
For this I was planning on using the following awk code :
cat f2 | awk -v "n=2" -v "s=` cat f1| sed 1d | awk 'BEGIN { FS = "<outer awk's FS>" }; {printf "%s %s %s %s %s %s %s\n", $1,$2,$3,$4,$5,$6,$7}'`" '(NR==n) { print s } 1'
Is this actually possible ? or is there better approach to achieve the desired output ? Any help/hints would be much appreciated!!
Thanks!!
|
|
|
05-23-2012, 02:14 AM
|
#2
|
Senior Member
Registered: Apr 2007
Location: Portland, OR
Distribution: Debian, Android, LFS
Posts: 1,168
|
The following AWK script would accomplish the output you desire:
Code:
{
gsub(/[ \t]+/," ",$0)
}
/^hdr/ {
if (0 == seen) {
print $0"\n"
}
seen++
next
}
!/^$/ {
print $0
}
Save that as parse.awk, and then invoke like this:
Code:
awk -f parse.awk f1 f2
|
|
|
05-23-2012, 02:33 AM
|
#3
|
LQ Newbie
Registered: Jul 2011
Posts: 18
Original Poster
Rep: 
|
Hi,
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 :
hdr1<space><space>hdr2<space><space><space><space>hdr3<space>hdr4
aaaa<space><space>bbbb<space><space><space><space>cccc<space>dddd
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)..
Thanks for your time though!!
|
|
|
05-23-2012, 02:52 AM
|
#4
|
Senior Member
Registered: Apr 2007
Location: Portland, OR
Distribution: Debian, Android, LFS
Posts: 1,168
|
Hi acc, that shouldn't matter. Did you try it? The first action of the script I provided is to normalize spacing to two spaces, everywhere.
|
|
|
05-23-2012, 03:46 AM
|
#5
|
LQ Newbie
Registered: Jul 2011
Posts: 18
Original Poster
Rep: 
|
Hi jhwilliams, yes i did try.. but also i realized now, I am not giving the best sample example data..
The values under the headers extend longer than the headers...
I tried attaching a file but didnt work.. So please see if below is easy to understand ( _ == space)
Code:
f1 :
Header1_Header2_________Header3______Header4_Header5_Header6_____Header7
AAAAA___QQQQQQQ_________FFFFFF_______SSSSSS__-_______-___________-
f2:
Header1_____________________________Header2_________Header3______Header4_Header5_Header6_____Header7
AAAAAAAAAAAAAAAAAAAAAAAAAAA_________BBBBBBBB________CCCCCCC______DDDDDD__XXXXXX__-___________-
PPPPPPPPPPPPPPPPPPPPPPPPPPPPPPP_____EEEEEEEE________FFFFFFF______GGGGGG__YYYYYY__-___________-
desired:
Header1_____________________________Header2_________Header3______Header4_Header5_Header6_____Header7
AAAAA_______________________________QQQQQQQ_________FFFFFF_______SSSSSS__-_______-___________-
AAAAAAAAAAAAAAAAAAAAAAAAAAA_________BBBBBBBB________CCCCCCC______DDDDDD__XXXXXX__-___________-
PPPPPPPPPPPPPPPPPPPPPPPPPPPPPPP_____EEEEEEEE________FFFFFFF______GGGGGG__YYYYYY__-___________-
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
|
|
|
05-23-2012, 05:30 AM
|
#6
|
LQ Veteran
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 21,414
|
Use [code] tags as above - from "advanced" it's the "#" token.
|
|
|
05-23-2012, 06:46 AM
|
#7
|
LQ Guru
Registered: Sep 2009
Location: Perth
Distribution: Arch
Posts: 10,038
|
Maybe something like:
Code:
awk 'FNR==NR || (FNR > 1 && NF){$1=$1;print}' OFS=" " f1 f2
|
|
|
05-23-2012, 06:57 AM
|
#8
|
LQ Newbie
Registered: Jul 2011
Posts: 18
Original Poster
Rep: 
|
Hi,
Thanks syg00
So finally, here is the format :
Code:
f1
Header1 Header2 Header3 Header4 Header5 Header6 Header7
AAAAA QQQQQQQ FFFFFF SSSSSS - - -
___________________________________________________________________________________________________
f2
Header1 Header2 Header3 Header4 Header5 Header6 Header7
AAAAAAAAAAAAAAAAAAAAAAAAAAA BBBBBBBB CCCCCCC DDDDDD XXXXXX - -
PPPPPPPPPPPPPPPPPPPPPPPPPPPPPPP EEEEEEEE FFFFFFF GGGGGG YYYYYY - -
___________________________________________________________________________________________________
Desired :
Header1 Header2 Header3 Header4 Header5 Header6 Header7
AAAAA QQQQQQQ FFFFFF SSSSSS - - -
AAAAAAAAAAAAAAAAAAAAAAAAAAA BBBBBBBB CCCCCCC DDDDDD XXXXXX - -
PPPPPPPPPPPPPPPPPPPPPPPPPPPPPPP EEEEEEEE FFFFFFF GGGGGG YYYYYY - -
@grail : It didnt solve the formatting problem for me , when I gave the two files as inputs to the awk line you suggested...
Code:
#
awk 'FNR==NR || (FNR > 1 && NF){$1=$1;print}' OFS=" " t1 t2
Header1 Header2 Header3 Header4 Header5 Header6 Header7
AAAAA QQQQQQQ FFFFFF SSSSSS - - -
AAAAAAAAAAAAAAAAAAAAAAAAAAA BBBBBBBB CCCCCCC DDDDDD XXXXXX - -
PPPPPPPPPPPPPPPPPPPPPPPPPPPPPPP EEEEEEEE FFFFFFF GGGGGG YYYYYY - -
|
|
|
05-23-2012, 08:29 AM
|
#9
|
LQ Newbie
Registered: Jul 2011
Posts: 18
Original Poster
Rep: 
|
Hi,
Thanks syg00
So finally, here is the format :
Code:
f1
Header1 Header2 Header3 Header4 Header5 Header6 Header7
AAAAA QQQQQQQ FFFFFF SSSSSS - - -
___________________________________________________________________________________________________
f2
Header1 Header2 Header3 Header4 Header5 Header6 Header7
AAAAAAAAAAAAAAAAAAAAAAAAAAA BBBBBBBB CCCCCCC DDDDDD XXXXXX - -
PPPPPPPPPPPPPPPPPPPPPPPPPPPPPPP EEEEEEEE FFFFFFF GGGGGG YYYYYY - -
___________________________________________________________________________________________________
Desired :
Header1 Header2 Header3 Header4 Header5 Header6 Header7
AAAAA QQQQQQQ FFFFFF SSSSSS - - -
AAAAAAAAAAAAAAAAAAAAAAAAAAA BBBBBBBB CCCCCCC DDDDDD XXXXXX - -
PPPPPPPPPPPPPPPPPPPPPPPPPPPPPPP EEEEEEEE FFFFFFF GGGGGG YYYYYY - -
@grail : It didnt solve the formatting problem for me , when I gave the two files as inputs to the awk line you suggested...
Code:
#
awk 'FNR==NR || (FNR > 1 && NF){$1=$1;print}' OFS=" " t1 t2
Header1 Header2 Header3 Header4 Header5 Header6 Header7
AAAAA QQQQQQQ FFFFFF SSSSSS - - -
AAAAAAAAAAAAAAAAAAAAAAAAAAA BBBBBBBB CCCCCCC DDDDDD XXXXXX - -
PPPPPPPPPPPPPPPPPPPPPPPPPPPPPPP EEEEEEEE FFFFFFF GGGGGG YYYYYY - -
|
|
|
05-23-2012, 09:47 AM
|
#10
|
LQ Guru
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509
|
Quote:
Originally Posted by acc_Wk
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.
|
|
|
05-23-2012, 12:54 PM
|
#11
|
LQ Guru
Registered: Sep 2009
Location: Perth
Distribution: Arch
Posts: 10,038
|
Quote:
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:
Code:
awk 'FNR==NR || (FNR > 1 && NF){$1=$1;print}' f1 f2 | column -t
If you still want the blank line you can play with awk some more.
|
|
|
05-23-2012, 06:41 PM
|
#12
|
Senior Member
Registered: Apr 2007
Location: Portland, OR
Distribution: Debian, Android, LFS
Posts: 1,168
|
Here's a tweak to my original post, to meet your additional "pretty print" requirement:
Code:
column -t f1 f2 | awk '
/^Header/ {
if (!seen) {
print $0"\n"
}
seen++
next
}
!/^$/'
|
|
|
05-24-2012, 02:21 AM
|
#13
|
LQ Newbie
Registered: Jul 2011
Posts: 18
Original Poster
Rep: 
|
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!
|
|
|
05-24-2012, 06:15 PM
|
#14
|
Bash Guru
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852
|
Could you please go back and edit your previous posts to include code tags as well? The long lines are making my screen side-scroll. Thanks.
|
|
|
All times are GMT -5. The time now is 09:37 AM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|