Help tabulating file putting repeated strings as headers

Tags
awk, parse, shell scripts, tabulate

Login to Reply

 
Thread Tools Search this Thread
# 8  
Old 04-15-2018
Update to rdrtx1's solution (in red) to cover the duplicate field(s) qualification.

Code:
awk '
BEGIN {lines=0; column_count=0}
$2 !~ /=/ || NF != 3 {next}
! column[$1]++ {columns[column_count++]=$1}
$1 ~ /^STAGE*$/ || $1 SUBSEP lines in column_data{lines++}
{column_data[$1, lines]=$3}
END {
   for (i=0; i<column_count; i++) if (columns[i]) printf columns[i] ((i<column_count-1) ? "|" : "\n")
   for (i=1; i <= lines; i++) {
      for (j=0; j < column_count; j++) {
         if (columns[j]) printf column_data[columns[j], i] ((j<column_count-1) ? "|" : "\n")
      }
   }
}
' infile

This User Gave Thanks to Chubler_XL For This Post:
Ophiuchus (04-16-2018)
# 9  
Old 04-16-2018
Quote:
Originally Posted by Chubler_XL
Update to rdrtx1's solution (in red) to cover the duplicate field(s) qualification.
Excellent rdrtx1 for yout solution and Chubler_XL for the modification. With both solution it seems to work pretty nice even when in real file the strings have leading spaces.

Chubler_XL,

I see this solution has several arrays. Is there a tool to test and see what has stored each part of an awk program, the content of the arrays, etc?

Something similar to IRB(interactive Ruby) which we can test small parts of the ruby program. This in order to understand better the logic and how it works.

Thanks for help.
# 10  
Old 04-16-2018
I'm not aware of any tool like that. You could simply print out the contents of the arrays in the END block like this:

Code:
awk '
BEGIN {lines=0; column_count=0}
$2 !~ /=/ || NF != 3 {next}
! column[$1]++ {columns[column_count++]=$1}
$1 ~ /^STAGE*$/ || $1 SUBSEP lines in column_data{lines++}
{column_data[$1, lines]=$3}
END {
   for (i in column) print "column[" i "]="column[i]
   for (i in columns) print "columns[" i "]="columns[i]
   for (i in column_data) {
     k=i
     gsub(SUBSEP,",",k)
     print "column_data[" k "]="column_data[i]
   }
}
' infile


result:
Code:
column[ADDR]=5
column[ISGALW]=5
column[RRUL]=5
column[TYPE]=4
...
columns[0]=STAGE
columns[1]=ID
columns[2]=NAME
columns[3]=TYPE
columns[4]=DFRUL
columns[5]=ADDR
...
column_data[NAME,6]=PPROOA
column_data[SPRR,1]=TRUE
column_data[ID,1]=0
column_data[SPRR,3]=FALSE
column_data[ID,2]=2
column_data[SPRR,4]=FALSE
...

You can see that:
column[] is index by the column name and contains a count of the number of rows the column appears in (less 1)
columns[] is indexed by the column order and contains the column name.
column_data[] is a 2D array with index of <column name>,<row> and contains value of the data for that cell.

column[] is only used to detect the first occurrence of a column and add it to columns[]

Last edited by Chubler_XL; 04-16-2018 at 08:53 PM..
This User Gave Thanks to Chubler_XL For This Post:
Ophiuchus (04-17-2018)
# 11  
Old 04-17-2018
Quote:
Originally Posted by Chubler_XL
I'm not aware of any tool like that. You could simply print out the contents of the arrays in the END block like this:

You can see that:
column[] is index by the column name and contains a count of the number of rows the column appears in (less 1)
columns[] is indexed by the column order and contains the column name.
column_data[] is a 2D array with index of <column name>,<row> and contains value of the data for that cell.

column[] is only used to detect the first occurrence of a column and add it to columns[]
Excellent ChublerXL. Thanks for the lesson, specially that comes from someone that knows a lot. I'll play a little bit with this example of arrays.

Thanks to all.
Login to Reply

|
Thread Tools Search this Thread
Search this Thread:
Advanced Search

Similar Threads More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Need help in sorting the file and putting to a different folder Shanmugapriya D Shell Programming and Scripting 4 10-29-2018 09:08 AM
Putting strings into positioning array in loop annacreek Shell Programming and Scripting 7 08-30-2018 07:57 AM
Use strings from nth field from one file to match strings in entire line in another file, awk jvoot UNIX for Beginners Questions & Answers 1 01-16-2018 09:31 AM
How to pass strings from a list of strings from another file and create multiple files? nubie2linux UNIX for Beginners Questions & Answers 2 04-10-2017 09:37 AM
Joining ends of strings in certain order with repeated ID's verse123 UNIX for Dummies Questions & Answers 8 03-25-2014 01:29 PM
Find repeated word and take sum of the second field to it ,for all the repeated words in awk 100bees Shell Programming and Scripting 11 07-04-2013 02:41 AM
Delete lines in file containing duplicate strings, keeping longer strings raidzero Shell Programming and Scripting 11 09-20-2011 12:00 PM
Putting together all values from different files in one file shoaibjameel123 Shell Programming and Scripting 2 08-16-2011 01:42 PM
delete repeated strings (tags) in a line and concatenate corresponding words mjomba Shell Programming and Scripting 2 11-08-2010 05:15 AM
Merging of files with different headers to make combined headers file marut_ashu Shell Programming and Scripting 1 08-07-2009 05:10 AM
Deleting repeated strings in column 2 cgkmal Shell Programming and Scripting 5 05-26-2009 04:36 AM
Putting echoed text into a new file likelylad UNIX for Dummies Questions & Answers 7 01-27-2009 06:55 PM
Remove text between headers while leaving headers intact Trones Shell Programming and Scripting 3 12-27-2008 08:06 PM
putting a timestamp in a file rocker40 UNIX for Dummies Questions & Answers 2 10-22-2003 12:56 PM
putting restrictions when copying file thoffpauir UNIX for Dummies Questions & Answers 3 04-12-2002 12:08 PM
All times are GMT -4. The time now is 12:31 AM.

Unix & Linux Forums Content Copyright 1993-2018. All Rights Reserved.
UNIX.COM Login
Username:
Password:  
Show Password





Not a Forum Member?
Forgot Password?