How to load an array with desired lines with awk


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to load an array with desired lines with awk
# 8  
Old 03-15-2011
OK L is the output Line Number it is incremented (with L++) each time a Category line appears.

T tracks the output field 1=Category, 2=Titles, 3=Authors (set to 1 on Category line incremented on Title or Authors line), note this means the program depends on the input file having Category, Titles and Authors appearing in that order.

if ($0&&$0!~"^---") only add lines that are not blank ($0 is false for blank line) and do not start with "---".

The fields array F[] is a 2D array with first element as the output line number and 2nd element the field number so F[1,2] is output line#1 field#2 (ie Titles of 1st book).

Question mark operator works like this <logical_condition>?<true_value>:<false_value> . So if logical_condition is true the true_value is returned if it is false the false_value is returned. We use it like this X?X",":"" which will return "red," if X has value of "red" and "" if x has null value.
This User Gave Thanks to Chubler_XL For This Post:
# 9  
Old 03-15-2011
Quote:
Originally Posted by Chubler_XL
OK L is the output Line Number it is incremented (with L++) each time a Category line appears.

T tracks the output field 1=Category, 2=Titles, 3=Authors (set to 1 on Category line incremented on Title or Authors line), note this means the program depends on the input file having Category, Titles and Authors appearing in that order.

if ($0&&$0!~"^---") only add lines that are not blank ($0 is false for blank line) and do not start with "---".

The fields array F[] is a 2D array with first element as the output line number and 2nd element the field number so F[1,2] is output line#1 field#2 (ie Titles of 1st book).

Question mark operator works like this <logical_condition>?<true_value>:<false_value> . So if logical_condition is true the true_value is returned if it is false the false_value is returned. We use it like this X?X",":"" which will return "red," if X has value of "red" and "" if x has null value.
Chubler_XL, thanks for ALL, your help, solutions, and explanations!!!. I have learned trying to solve this for myself and a lot more with solutions like you've written.

I did have some doubts after the explanation, so I evaluated the code as shown below and now I'm clear enough

Code:
###################################################################################################
### Ouput after apply first sentence (/^(Category|Titles|Authors)/,/^$ 

Category--
--------------------------------------------------
Adventure

Titles--
--------------------------------------------------
Robinson-Crusoe
Saturday

Authors
--------------------------------------------------
Daniel-Defoe
Ian-McEwan

Category
--------------------------------------------------
Fantasy

Category
--------------------------------------------------
Literature
                                                                 
Titles
--------------------------------------------------
Ulysses    XXXXXXXXXXXX XXXX
###################################################################################################
---------------  --------------------      
-- L --|-- T --  ------ F[L,T] ------
---------------  --------------------
   1   |   1     F[1,1]=("")$1=Adventure
---------------  --------------------
   1   |   2     F[1,2]=("")$1=""Robinson-Crusoe=Robinson-Crusoe
---------------  --------------------
   1   |   2     F[1,2]=(F[1,2],)$1=(Robinson-Crusoe,)Saturday=Robinson-Crusoe,Saturday
---------------  --------------------
   1   |   3     F[1,3]=("")$1=""Daniel-Defoe=Daniel-Defoe
---------------  --------------------
   1   |   3     F[1,3]=(Daniel-Defoe,)$1=Daniel-Defoe,)Ian-McEwan=Daniel-Defoe,Ian-McEwan
---------------  --------------------
   2   |   1     F[2,1]=("")$1=""Fantasy=Fantasy
---------------  --------------------
   3   |   1     F[3,1]=("")$1=""Literature=Literature
---------------  --------------------
   3   |   2     F[3,2]=("")$1=""Ulysses=Ulysses
---------------  --------------------
###################################################################################################
Printout:
 Category | Titles |Authors   =>>    Category  |        Titles          |       Authors
  F[1,1]  | F[1,2] | F[1,3]   =>>   Adventure  |Robinson-Crusoe,Saturday|Daniel-Defoe,Ian-McEwan
  F[2,1]  | F[2,1] | F[2,1]   =>>    Fantasy   |                        |
  F[3,1]  | F[3,1] | F[3,1]   =>>   Literature |         Ulysses        |

Thanks so much again.

Regards
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Need help in solving to obtain desired print output using awk or perl or any commands, Please help!!

I have an file which have data in lines as follows ad, findline=24,an=54,ab=34,av=64,ab=7989,ab65=34,aj=323,ay=34,au=545,ad=5545 ab,abc,an10=23,an2=24,an31=32,findline=00,an33=23,an32=26,an40=45,ac23=5,ac=87,al=76,ad=26... (3 Replies)
Discussion started by: deepKrish
3 Replies

2. Shell Programming and Scripting

awk split command to get the desired result

Dear all, I am using the awk 'split' command to get the particular value. FILE=InputFile_009_0.txt Temp=$(echo $FILE | awk '{split($FILE, a, "e_"); print a}') I would like to have the Temp take the value as : _009_0 ... (4 Replies)
Discussion started by: emily
4 Replies

3. Shell Programming and Scripting

awk pattern match not printing desired columns

Hi all, I'm trying to match the following two files with the code below: awk -F, 'NR==FNR {a=$0; next} ($12,$4) in a {print $12,$1,a}' OFS="," file4.csv file3.csv but the code does not print the entire row from file4 in addition to column 12 and 1 of file3. file4: o,c,q,co,ov,b... (1 Reply)
Discussion started by: bkane3
1 Replies

4. Shell Programming and Scripting

AWK command to cut the desired header columns

Hi Friends, I have a file1 i want to retrieve only the fields which have DEP,CITY,TRANS as headers in other file. Output: I want to give the input as DEP,CITY,TRANS column names to get the output. i used cut command .. but if i have 300 fileds it is more difficult to... (4 Replies)
Discussion started by: i150371485
4 Replies

5. Shell Programming and Scripting

awk syntax mistake doubles desired output

I am trying to add a line to a BASH shell script to print out a large variable length table on a web page. I am very new to this obviously, but I tried this with awk and it prints out every line twice. What I am doing wrong? echo "1^2^3%4^5^6%7^8^9%" | awk 'BEGIN { RS="%"; FS="^"; } {for (i =... (6 Replies)
Discussion started by: awknewb123
6 Replies

6. Shell Programming and Scripting

Put lines of a file in an array with awk

Hello, Is there any way in awk to put every line of a file in an array and so we can like this print the line we want. For example, if we have this file aaa eee bbb fff ccc ggg ddd hhh So we can print to the output the 3rd line only ccc ggg If it is possible, please put the... (7 Replies)
Discussion started by: rany1
7 Replies

7. Shell Programming and Scripting

Need to parse file "x" lines at a time ... awk array?

I have files that store multiple data points for the same device "vertically" and include multiple devices. It repeats a consistant pattern of lines where for each line: Column 1 is a common number for the entire file and all devices in that file Column 2 is a unique device number Column 3 is... (7 Replies)
Discussion started by: STN
7 Replies

8. Solaris

How to grep (say)last-3 and next-3 lines of Desired Pattern

Hi All, OS-Type=Sun-OS 5.8 Sparc9 Processor Can I grep the previous 4 lines and next 4 lines of a matched pattern(context grep)? For example here we need to monitor logs of live traffic.The data obtained from "tail -f LiveTrafficData.log" looks something like this:-... (3 Replies)
Discussion started by: Sujan Banerjee
3 Replies

9. Shell Programming and Scripting

Need help in wrting Load Script for a Load-Resume type of load.

hi all need your help. I am wrting a script that will load data into the table. then on another load will append the data into the existing table. Regards Ankit (1 Reply)
Discussion started by: ankitgupta
1 Replies

10. UNIX for Advanced & Expert Users

Percent complete error while scanning RAID array during 5.0.6 load

Percent complete SCO 5.0.6 / No longer an issue (0 Replies)
Discussion started by: Henrys
0 Replies
Login or Register to Ask a Question