![]() |
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 |
| [sh] String comparison operators | userix | Shell Programming and Scripting | 1 | 05-16-2008 04:09 AM |
| string comparison | Jatsui | Shell Programming and Scripting | 5 | 02-04-2008 04:28 PM |
| string comparison | fedora | Shell Programming and Scripting | 2 | 01-03-2007 03:20 PM |
| Help with if loop (string comparison) | psynaps3 | Shell Programming and Scripting | 4 | 07-07-2006 02:36 AM |
| String Comparison | abey | High Level Programming | 1 | 10-19-2005 12:08 PM |
![]() |
|
|
LinkBack | Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
||||
|
String Comparison between two files using awk
I have two files with field seperator as "~".
File A: 12~13~14~15 File B: 22~22~32~11 i want to calculate the difference between two files and than calculate the percentage difference and output it to a new file. How do i do this using awk. Also please suggest GOOD awk tutorials. Thank you |
|
||||
|
Quote:
Can you pls explain the above code to me, I tried to understand it, but couldn't figure it out, your reply would be highly appreciated. Thanks. Cheers, Patras |
|
|||||
|
Sure thing...
fileA.txt contains: Code:
12~13~14~15 16~83~10~44 75~84~96~56 Code:
22~22~32~11 73~85~52~43 74~51~14~68 Code:
paste file[AB].txt | sed 's/ /~/g' | \
awk '
BEGIN { FS="~" }
{
divs=NF/2
for ( i = 1; i <= divs; i++ ) {
f1=i
f2=i+divs
diff=$f1-$f2
pct=diff/$f1
printf("%.0f/%.2f ",diff,pct)
}
printf("\n","")
}'
Code:
paste file[AB].txt Code:
12~13~14~15 22~22~32~11 16~83~10~44 73~85~52~43 75~84~96~56 74~51~14~68 Code:
sed 's/ /~/g' Code:
12~13~14~15~22~22~32~11 16~83~10~44~73~85~52~43 75~84~96~56~74~51~14~68 Set the field separator to ~ Code:
awk '
BEGIN { FS="~" }
record pair in the files contains an equal number of fields. Therefore, we want to split the whole record, e.g. "12~13~14~15~22~22~32~11" into two parts. So we set divs equal to half the number of fields: Code:
{
divs=NF/2
of fields 1 and 5, 2 and 6, 3 and 7, and 4 and 8. The "divs=NF/2" method just lets us determine at run time how many fields we are dealing with: Code:
Field #: 1 2 3 4 5 6 7 8 Record: 12~13~14~15~22~22~32~11 record pairs. The number of record pairs is determined dynamically for each line: Code:
for ( i = 1; i <= divs; i++ ) {
It gets incremented after each comparison: Code:
f1=i then we want f2 to be field 5. So f2=i+divs --> f2=1+4. Code:
f2=i+divs subtracting the value of f2 from the value of f1; f1 and f2 are pointers (i.e. field numbers). So $f1 is $1 and $f2 is $5, then on the next iteration $f1 is $2 and $f2 is $6, and so on: Code:
diff=$f1-$f2 Code:
pct=diff/$f1 just chose those formats for looks). Code:
printf("%.0f/%.2f ",diff,pct)
Code:
}
printf("\n","")
Code:
}' |
| Sponsored Links | ||
|
|
![]() |
| Bookmarks |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|