![]() |
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.
|
|
google unix.com
|
|||||||
| Forums | Register | Forum Rules | Links | Albums | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
| Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here. |
More UNIX and Linux Forum Topics You Might Find Helpful
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| awk - comparing files | dbrundrett | Shell Programming and Scripting | 6 | 01-18-2009 10:51 PM |
| Comparing two files | superstar003 | Forum Support Area for Unregistered Users & Account Problems | 1 | 05-08-2008 03:34 AM |
| Comparing two files.. | padarthy | Shell Programming and Scripting | 1 | 08-29-2007 08:01 AM |
| Comparing two files... | paqman | Shell Programming and Scripting | 12 | 08-08-2007 03:45 AM |
| comparing shadow files with real files | terrym | UNIX for Advanced & Expert Users | 4 | 02-09-2007 02:38 AM |
![]() |
|
|
LinkBack | Thread Tools | Search this Thread |
Rating:
|
Display Modes |
|
||||
|
Hi,
I have two files in this format. The files contain the statistics of tables as seen below. The other file is also in this format. I need to compare both the files and if there is a mismatch i need to display the contents within the break lines from both the files for that corresponding table. Table Name: AAA Row Count:96 SUM(F1): 3739 MAX(F1):77 MIN(F1): 0 AVG(F1): 38.9479167 LENGTH(LINE): 2260 ------------------------------------------------------------------------------------------------------------------------------ Table Name: AQ$_FT_Q_BECMD_G Row Count: 0 SUM(SUBSCRIBER#):MAX(SUBSCRIBER#): MIN(SUBSCRIBER#): AVG(SUBSCRIBER#):LENGTH(NAME):SUM(ADDRESS#): MAX(ADDRESS#):MIN(ADDRESS#):AVG(ADDRESS#): ------------------------------------------------------------------------------------------------------------------------------ Table Name: AQ$_FT_Q_BECMD_H Row Count: 0 SUM(SUBSCRIBER#):MAX(SUBSCRIBER#): MIN(SUBSCRIBER#): AVG(SUBSCRIBER#):LENGTH(NAME):SUM(ADDRESS#): MAX(ADDRESS#):MIN(ADDRESS#):AVG(ADDRESS#): LENGTH(TRANSACTION_ID): SUM(DEQUEUE_USER): MAX(DEQUEUE_USER): LENGTH(DEQUEUE_USER): 50 ------------------------------------------------------------------------------------------------------------------------------ Can anyone help me on this? Last edited by ragavhere; 05-12-2008 at 12:27 AM.. |
|
|||||
|
Code:
awk '
/^Table Name/ { # Select lines starting with 'Table Name'
table = $3; # Memorize table name into variable
tables[table]++ # and array
} #
! NF || /^-+/ { # Select empty and delimiter lines
next # Proceed next line (skip selected lines)
} #
NR == FNR { # Select lines from first input file
stats1[table] = stats1[table] $0 ORS; # Memorize stats's table
next # Proceed next line
}
{ # Lines comes from second input file
stats2[table] = stats2[table] $0 ORS; # Memorize stats's table
next # Proceed next line
} #
END { # All files have been read
for (t in tables) { # For all memorized tables
if (stats1[t] != stats2[t]) { # If stats mismatch
out = "==========================================" ORS;
out = out stats1[t] ORS stats2[t]; #
print out # Output sep line and stats
} #
} #
} #
' stats1.dat stats2.dat
|
|
||||
|
Hi Jean-Pierre,
Does stats1 and stats2 refer to the two input files? Will the code work if i give the path name like /home/frk/ragav/stats1 and /home/frk/ragav/stats2 instead of the file names? Then how should the code be modified? If i assign the path like /home/frk/ragav/stats1 to a variable how can i call the path in the code? When i assigned the file name to a variable like a=stats1.txt b=stats2.txt and changed the code to nawk ' /^Table Name/ { table = $3 ; tables[table]++ } ! NF || /^-+/ { next } NR == FNR { $e[table] = $e[table] $0 ORS ; next } { $f[table] = $f[table] $0 ORS ; next } END { for (t in tables) { if ($e[t] != $f[t]) { out = "-----------------------------------------------------------------" ORS out = out $e[t] ORS $f[t] print out } } } ' $e $f >> result.out i am getting this error. nawk: illegal field $() input record number 1, file startendcut1.txt source line number 4 Can you please help on the above two ways of modifying the code? Last edited by ragavhere; 04-26-2008 at 05:31 AM.. Reason: Error reason included |
|
|||||
|
In my script, stats1 and stats2 inside awk code are arrays.
stats1.dat and stats2.dat are the input files. The inputfiles can be specified with the full path. Don't use $e and $f as arrays in your awk code, use fixed names (stats1 and stats2 or what you want like first_array and second_array...) Code:
a=stats1.txt
b=stats2.txt
nawk '
/^Table Name/ { table = $3 ; tables[table]++ }
! NF || /^-+/ { next }
NR == FNR { stats1[table] = stats1[table] $0 ORS ; next }
{stats2[table] = stats2[table] $0 ORS ; next }
END {
for (t in tables) {
if (stats1[t] != stats2[t]) {
out = "-----------------------------------------------------------------" ORS
out = out stats1[t] ORS stats2[t]
print out
}
}
}
' $e $f >> result.out
|
| Sponsored Links | ||
|
|
![]() |
| Bookmarks |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|