Sponsored Content
Top Forums Shell Programming and Scripting Word boundary with awk in ksh Post 302810695 by Don Cragun on Wednesday 22nd of May 2013 11:22:45 AM
Old 05-22-2013
You could try something like the following:
Code:
#!/usr/xpg4/bin/sh
/usr/xpg4/bin/awk '
# repDigits(1, split1, n1, m) or
# repDigits(2, split2, n2, m) reconstituutes the field specified by the 1st
# argument with data from the array specifeid by the 2nd argument containing
# the # of elements specified by the 3rd argument.  The digits in the 1st 4
# elements of splitN will be replaced by the same number of alphabetic
# characters:  if both fields are being replaced, digits are replaced with "f";
# if only field 1 is being replaced, digits are replaced with "g"; and if only
# field 2 is being replaced, digits are replaced with "h".
function repDigits(field, fieldData, fieldCount, repIndex,      i) {
        $field = sprintf("%.*s.%.*s.%.*s.%.*s",
                        length(fieldData[1]), rep[repIndex],
                        length(fieldData[2]), rep[repIndex],
                        length(fieldData[3]), rep[repIndex],
                        length(fieldData[4]), rep[repIndex])
        for(i = 5; i <= fieldCount; i++)
                $field = $field "." fieldData[i]
}
BEGIN { # Initialize replaceemnt strings for digits in IP addresses
        rep[1] = "gggggggggg"   # 1st field only
        rep[2] = "hhhhhhhhhh"   # 2nd field only
        rep[3] = "ffffffffff"   # both fields
}
NF == 7 {
        # Split the first two fields on this line into the arrays split1[] and
        # split2[] using a period as the field separator and then verify that
        # each array has at least four elements and that the 1st four elements
        # of each is numeric.  The variable m is then set as follows:
        #    0=>neither field meets the criteria
        #    1=>the 1st field meets the criteria, but the 2nd argument does not
        #    2=>the 2nd field meets the criteria, but the 1st argument does not
        #    3=>both arguments meet the criteria
        m = 0
        n1 = split($1, split1, /[.]/)
        if(n1 >= 4 &&   split1[1] ~ /^[0-9]+$/ &&
                        split1[2] ~ /^[0-9]+$/ &&
                        split1[3] ~ /^[0-9]+$/ &&
                        split1[4] ~ /^[0-9]+$/) m = 1
        n2 = split($2, split2, /[.]/)
        if(n2 >= 4 &&   split2[1] ~ /^[0-9]+$/ &&
                        split2[2] ~ /^[0-9]+$/ &&
                        split2[3] ~ /^[0-9]+$/ &&
                        split2[4] ~ /^[0-9]+$/) m += 2
        if(m % 2) repDigits(1, split1, n1, m)
        if(m >= 2) repDigits(2, split2, n2, m)
}
1       # print the (possibly updated) line' input

Note that on Solaris systems, /usr/xpg[46]/bin/sh are Korn shells, but there is nothing in this script that requires Korn shell extensions Any shell that accepts basic Bourne shell syntax will be fine for this script.
 

8 More Discussions You Might Find Interesting

1. Programming

Aligning for boundary conditions

Hi, I have tcp/ip client server programs which will communicate through reqest,reply c-structures. As the sizeof(struct) may give different value between client and server programs, how do i align properly for boundary conditions. Could anybody please give some suggestion. Thanks in... (3 Replies)
Discussion started by: axes
3 Replies

2. Solaris

Partition 0 not aligned on cylinder boundary: "

hi Guys .. user want mirror disk c3t9d0 (running ) to c2t9d0 (fresh hdd). when i tried to bash : prtvtoc /dev/rdsk/c3t9d0s2 | fmthard -s- /dev/rdsk/c2t9d0s2 it showing following error Partition 0 not aligned on cylinder boundary: " 0 4 222 ..... unable to mirror .... plz... (1 Reply)
Discussion started by: coolboys
1 Replies

3. UNIX for Dummies Questions & Answers

How do I count how many times a specific word appear in a file (ksh)?

Hi Please can you help how do I count the number of specific characters or words that appear in a file? (8 Replies)
Discussion started by: fretagi
8 Replies

4. Red Hat

Warning: extended partition does not start at a cylinder boundary.

Can you please help me to remove this error. Disk /dev/sda: 64.4 GB, 64424509440 bytes 255 heads, 63 sectors/track, 7832 cylinders Units = cylinders of 16065 * 512 = 8225280 bytes Sector size (logical/physical): 512 bytes / 512 bytes I/O size (minimum/optimal): 512 bytes / 512 bytes Disk... (4 Replies)
Discussion started by: karthik9358
4 Replies

5. UNIX for Dummies Questions & Answers

Spooling data from the database in .csv file with boundary

Hi Guys, Another questions to the genius over here. I have spool the dataf from the database into a .csv file. But can it be possible to have all the rows and column with the boundaries..for example the .csv file which i have is as below: 20140327 BU 9A 3 20140327 SPACE 9A 3 20140327... (8 Replies)
Discussion started by: Pramod_009
8 Replies

6. Shell Programming and Scripting

How do i replace a word ending with "key" using awk excpet for one word?

echo {mbr_key,grp_key,dep_key,abc,xyz,aaa,ccc} | awk 'gsub(/^|abc,|$/,"") {print}' Required output {grp_key,xyz,aaa,ccc} (5 Replies)
Discussion started by: 100bees
5 Replies

7. Shell Programming and Scripting

Help with defining a consition within a circular boundary

Hi Help, I am trying to create something like --- Suppose, I have grid origin at X=600000.0 & Y=90000.0. For any values of X, Y values lying within a circular periphery defined by circle of radius R=500m;X=599500.0 & 600500.0 ;Y=90500.0 & 89500.0should have a default=0or else it should... (4 Replies)
Discussion started by: Indra2011
4 Replies

8. UNIX for Beginners Questions & Answers

How to search for a word in column header that fully matches the word not partially in awk?

I have a multicolumn text file with header in the first row like this The headers are stored in an array called . which contains I want to search for each elements of this array from that multicolumn text file. And I am using this awk approach for ii in ${hdr} do gawk -vcol="$ii" -F... (1 Reply)
Discussion started by: Atta
1 Replies
MB_EREGI_REPLACE(3)							 1						       MB_EREGI_REPLACE(3)

mb_eregi_replace - Replace regular expression with multibyte support ignoring case

SYNOPSIS
string mb_eregi_replace (string $pattern, string $replace, string $string, [string $option = "msri"]) DESCRIPTION
Scans $string for matches to $pattern, then replaces the matched text with $replacement. PARAMETERS
o $pattern - The regular expression pattern. Multibyte characters may be used. The case will be ignored. o $replace - The replacement text. o $string - The searched string. o $option -$option has the same meaning as in mb_ereg_replace(3). RETURN VALUES
The resultant string or FALSE on error. NOTES
Note The internal encoding or the character encoding specified by mb_regex_encoding(3) will be used as the character encoding for this function. Warning Never use the e modifier when working on untrusted input. No automatic escaping will happen (as known from preg_replace(3)). Not taking care of this will most likely create remote code execution vulnerabilities in your application. SEE ALSO
mb_regex_encoding(3), mb_ereg_replace(3). PHP Documentation Group MB_EREGI_REPLACE(3)
All times are GMT -4. The time now is 06:51 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy