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 |
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.
|
 |
03-15-2011, 02:27 AM
|
#1
|
Member
Registered: Nov 2008
Location: Tegucigalpa
Posts: 78
Rep:
|
Modify script loading array with ranges of lines in awk
Hi to all,
As awk programmers may know, we can print range of lines with awk, from an initial pattern until a final pattern as follow:
Code:
awk '/Initial_String/,/Final_String/' inputfile
Well, I have this inputfile:
Code:
_________________________________________________
Category
--------------------------------------------------
Adventure
______________________________________________________________________________
XXXX XXXXX XXXXXXXXX
Y-W-K YYYYYYYYYY YYYY
Z-R-T ZZZZZZZZ ZZZZ
--------------------------------------------------
______________________________________________________________________________
Titles
--------------------------------------------------
Robinson-Crusoe XXXXXXXXXXXX XXXX
Saturday XXXXXXXX XX XXXXXX
______________________________________________________________________________
XXXX X
M-U-J M
______________________________________________________________________________
Authors
--------------------------------------------------
Daniel-Defoe XXXXXXXXXXXXXXXX
Ian-McEwan XXXXXXXXXXXX
______________________________________________________________________________
and I have written this code so far:
Code:
awk '
/^Category/,/^$/{print $1}; # I'm interested in field 1
/Titles/,/^$/{print $1};
/Authors/,/^$/{print $1}' inputfile
With this code I get this output:
Code:
Category
--------------------------------------------------
Adventure
Titles
--------------------------------------------------
Robinson-Crusoe
Saturday
Authors
--------------------------------------------------
Daniel-Defoe
Ian-McEwan
and I'm trying to:
First:
Instead of print $1 for every range of lines e.g ( /^Category/, /^$/{ print $1}), send this range of lines to a variable or array ( a[]) in order
to have in that array ( a[]) the following output ( the same output as above, but without blank lines or lines containing "--" or "__"):
Code:
Category
Adventure
Titles
Robinson-Crusoe
Saturday
Authors
Daniel-Defoe
Ian-McEwan
Second:
Once having those elements in that way within an array( a[]), I want to be able to manipulate the array ( a[]) and copy its elements
to another array ( b[]) in different order ( all lines joined in a single line separated with commas), as follow:
Code:
Category,Adventure,Titles,Robinson,Crusoe,Saturday,Authors,Daniel-Defoe,Ian-McEwan
Only inserting the missing "array" coding and based and follow the code I've written, please someone can help me sharing the way to do this?
*Not needed to create a new code to do it with other method, I'm particulary interested to know how to improve my code and learn
from you the missing part of my algorithm idea I have.
Many thanks in advance.
Best regards.
Last edited by cgcamal; 03-15-2011 at 02:28 AM.
|
|
|
03-15-2011, 03:27 PM
|
#2
|
Member
Registered: Sep 2003
Location: 19th moon ................. ................Planet Covid ................Another Galaxy;............. ................Not Yours
Posts: 705
Rep: 
|
cgcamal,
I've attached a file prog.txt containing a modified version of your awk program which appears to work with your input file to get the values you want into an array, for your step 1. However, I'm not quite clear on what you want to do with your step 2. You seem to want to join the elements of the first array together separated by commas, as a "single line". I'm not quite sure how that would involve a second array. It would seem to require just a single variable. However, in case step 2 really does need to involve an array, I've attached a second awk program prog2.txt which joins all the elements of the first array separated by commas, into the first element of a second array.
Hope this helps.
|
|
|
03-15-2011, 09:05 PM
|
#3
|
LQ Guru
Registered: Sep 2009
Location: Perth
Distribution: Arch
Posts: 10,039
|
Are we talking arrays in awk or you want to output to an array in say bash?
If we are staying in awk then I would recommend:
Code:
awk '/[[:alpha:]]/{array1[++i]=$0;array2[0]=(array2[0])?array2[0]","$0:$0}' file
I am with kakaka that the second may as well just be a string
|
|
|
03-16-2011, 03:51 AM
|
#4
|
Member
Registered: Nov 2008
Location: Tegucigalpa
Posts: 78
Original Poster
Rep:
|
Hi both kakaka and grail, many thanks for reply.
I've tested both kakaka's programs, prog.txt and prog2.txt but I'm not sure why they don't work, it looks they're printing list of files in ~usr/bin.
The grail's code doesn't return any error, but is not printing anything.
Well, my idea is to save the searches in a variable or array in order to manipulate its elements to get
first:
Code:
Category,Adventure,Titles,Robinson,Crusoe,Saturday,Authors,Daniel-Defoe,Ian-McEwan
and manipulate them again to get
Second:
Code:
Category|Titles|Authors
Adventure|Robinson-Crusoe,Saturday|Daniel-Defoe,Ian-McEwan
Fantasy||
Literature|Ulysses|
Sure there are best ways to get this output, only is a thing to try to get the algorithm I think.
1-) Search and extract ranges;
2-) Store those ranges in a variable or array
3-) Remove from the array all blank lines and lines with "--" & "__" .
4-) Transform the content of the variable or array to put in a single line separated by commas.
5-) Separate lines using "\n" and fields with "|".
Thanks again.
Best regards.
|
|
|
03-16-2011, 06:36 AM
|
#5
|
LQ Guru
Registered: Sep 2009
Location: Perth
Distribution: Arch
Posts: 10,039
|
Quote:
The grail's code doesn't return any error, but is not printing anything.
|
Correct. You asked for the information to be stored in arrays, which I did. It is up to you to now manipulate as you see fit.
I notice also that you have modified the input file and your requested output is similar to that from question raised here
Is this an adaptation of that question or are we looking at a different problem?
i imagine with what has been provided here and in previous question you should probably be able to manipulate the scripts to retrieve the desired results.
|
|
|
03-16-2011, 02:23 PM
|
#6
|
Member
Registered: Nov 2008
Location: Tegucigalpa
Posts: 78
Original Poster
Rep:
|
Quote:
Originally Posted by grail
Correct. You asked for the information to be stored in arrays, which I did. It is up to you to now manipulate as you see fit.
I notice also that you have modified the input file and your requested output is similar to that from question raised here
Is this an adaptation of that question or are we looking at a different problem?
i imagine with what has been provided here and in previous question you should probably be able to manipulate the scripts to retrieve the desired results.
|
Thanks grail for your advices, actually is an adaption of that problem, I'm trying to learn in the process to do it in more than one way and see if the method I thought originally is reachable. I'll try to follow in my attempt with things I learned from people willing to shared their knowledge like you.
Many thanks again,
Best regards.
|
|
|
03-16-2011, 11:02 PM
|
#7
|
LQ Guru
Registered: Sep 2009
Location: Perth
Distribution: Arch
Posts: 10,039
|
One other piece of advice I would give with the above is to probably look at the index in the arrays somehow relating to the found items,
ie when looking at 'Category', you could have something like:
Hence when you print or use this it will contain all items under 'Category'.
Also, if you wanted it to be more granular, you could look at adding a second index to the array. That way each item will have both a major and minor reference.
Example:
Code:
array['Category',1] = "Adventure"
array['Title',1] = "Robinson-Crusoe"
So here you can make the second index a variable and increase at each change in 'Category'
Just a thought
|
|
|
03-18-2011, 11:14 PM
|
#8
|
Member
Registered: Sep 2003
Location: 19th moon ................. ................Planet Covid ................Another Galaxy;............. ................Not Yours
Posts: 705
Rep: 
|
cgcamal,
Quote:
I've tested both kakaka's programs, prog.txt and prog2.txt but I'm not sure why they don't work, it looks they're printing list of files in ~usr/bin.
|
What you provided would not work on the command line at all with the shell I use, due to the single quotes.
The programs I provided are designed to be run like this:
Code:
awk -f prog.txt inputfile
awk -f prog2.txt inputfile
in which case they produce the output you wanted.
|
|
|
All times are GMT -5. The time now is 06:15 PM.
|
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
|
|