LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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


Reply
  Search this Thread
Old 03-15-2011, 02:27 AM   #1
cgcamal
Member
 
Registered: Nov 2008
Location: Tegucigalpa
Posts: 78

Rep: Reputation: 16
Question 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.
 
Old 03-15-2011, 03:27 PM   #2
rigor
Member
 
Registered: Sep 2003
Location: 19th moon ................. ................Planet Covid ................Another Galaxy;............. ................Not Yours
Posts: 705

Rep: Reputation: Disabled
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.
Attached Files
File Type: txt prog.txt (641 Bytes, 20 views)
File Type: txt prog2.txt (722 Bytes, 25 views)
 
Old 03-15-2011, 09:05 PM   #3
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Arch
Posts: 10,039

Rep: Reputation: 3203Reputation: 3203Reputation: 3203Reputation: 3203Reputation: 3203Reputation: 3203Reputation: 3203Reputation: 3203Reputation: 3203Reputation: 3203Reputation: 3203
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
 
Old 03-16-2011, 03:51 AM   #4
cgcamal
Member
 
Registered: Nov 2008
Location: Tegucigalpa
Posts: 78

Original Poster
Rep: Reputation: 16
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.
 
Old 03-16-2011, 06:36 AM   #5
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Arch
Posts: 10,039

Rep: Reputation: 3203Reputation: 3203Reputation: 3203Reputation: 3203Reputation: 3203Reputation: 3203Reputation: 3203Reputation: 3203Reputation: 3203Reputation: 3203Reputation: 3203
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.
 
Old 03-16-2011, 02:23 PM   #6
cgcamal
Member
 
Registered: Nov 2008
Location: Tegucigalpa
Posts: 78

Original Poster
Rep: Reputation: 16
Quote:
Originally Posted by grail View Post
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.
 
Old 03-16-2011, 11:02 PM   #7
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Arch
Posts: 10,039

Rep: Reputation: 3203Reputation: 3203Reputation: 3203Reputation: 3203Reputation: 3203Reputation: 3203Reputation: 3203Reputation: 3203Reputation: 3203Reputation: 3203Reputation: 3203
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:
Code:
array['Category']
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
 
Old 03-18-2011, 11:14 PM   #8
rigor
Member
 
Registered: Sep 2003
Location: 19th moon ................. ................Planet Covid ................Another Galaxy;............. ................Not Yours
Posts: 705

Rep: Reputation: Disabled
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.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
Help needed for using awk to parse a file to make array for bash script tallmtt Programming 12 04-14-2012 01:16 PM
[SOLVED] Shell script to convert values on successive lines into consecutive ranges? kmkocot Programming 5 07-09-2010 10:59 AM
[SOLVED] Problems to exclude lines and bad filter within AWK script cgcamal Programming 7 04-30-2010 12:38 AM
need help with awk-script (compare two lines) Mauline Programming 2 11-27-2008 04:12 AM
awk/gawk/sed - read lines from file1, comment out or delete matching lines in file2 rascal84 Linux - General 1 05-24-2006 09:19 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

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