Sponsored Content
Top Forums Shell Programming and Scripting Word boundary with awk in ksh Post 302810289 by sanzee007 on Tuesday 21st of May 2013 02:21:05 PM
Old 05-21-2013
Hi,
Say, I have a file containing 'netstat -an' output like below -
Code:
TCP: IPv4
   Local Address        Remote Address    Swind Send-Q Rwind Recv-Q    State
-------------------- -------------------- ----- ------ ----- ------ -----------
10.108.231.140.33099 10.108.231.140.33122 49152      0 49152      0 ESTABLISHED
127.0.0.1.33123            *.*                0      0 49152      0 LISTEN
      *.*                  *.*                0      0 49152      0 IDLE
10.108.231.140.33124 10.108.231.140.33099 49152      0 49152      0 ESTABLISHED
10.108.231.140.33099 10.108.231.140.33124 49152      0 49152      0 ESTABLISHED
...

I want to replace only the IP address 10.108.231.140 to some letter/character. If you see this pattern 10.108.231.140.33099 which represents some_ip.port_no. I want to replace only the IP address leaving the port no unchanged.

Below sample code replaces all ip with port no pattern without considering the exact IP address pattern.
Code:
for str in ${ip_arr[@]}
do
    #here str contains the exact ip, say 10.108.231.140
    /usr/xpg4/bin/awk '{ for (i=1; i<=NF; i++) if ($i ~ /'str'/) gsub(/[[:digit:]]/, "S", $i) }1' netstat-an.out
done

So can you please tell me how to get output like below-
Code:
TCP: IPv4
   Local Address        Remote Address    Swind Send-Q Rwind Recv-Q    State
-------------------- -------------------- ----- ------ ----- ------ -----------
ff.fff.fff.fff.33099 ff.fff.fff.fff.33122 49152 0 49152 0 ESTABLISHED
ggg.g.g.g.33123 *.* 0 0 49152 0 LISTEN
      *.*                  *.*                0      0 49152      0 IDLE
ff.fff.fff.fff.33124 ff.fff.fff.fff.33099 49152 0 49152 0 ESTABLISHED
ff.fff.fff.fff.33099 ff.fff.fff.fff.33124 49152 0 49152 0 ESTABLISHED
...

As you see, only the IP address is replaced with a Letter. I want this as the desired result.
This I am doing on Solaris using ksh and awk. So I can't use perl for this, because I want this in korn script only.
thanks for your valuable replies.
 

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
apache_mod_perl-108~358::mod_perl-2.0.7::docs::api::APR:UsereContributed Perl Doapache_mod_perl-108~358::mod_perl-2.0.7::docs::api::APR::Base64(3)

NAME
APR::Base64 - Perl API for APR base64 encoding/decoding functionality Synopsis use APR::Base64 (); my $clear = "foo" my $encoded = APR::Base64::encode($clear); my $decoded = APR::Base64::decode($encoded); my $len_enc = APR::Base64::encode_len(length $clear); Description "APR::Base64" provides the access to APR's base64 encoding and decoding API. API
"APR::Base64" provides the following functions and/or methods: "decode" Decode a base64 encoded string $decoded = decode($encoded); arg1: $encoded ( string ) The encoded string. ret: $decoded ( string ) The decoded string. since: 2.0.00 "encode" Encode a string to base64 $encoded = encode($clear); arg1: $clear ( string ) The unencoded string. ret: $encoded ( string ) The encoded string. since: 2.0.00 "encode_len" Given the length of an unencoded string, get the length of the encoded string. $len_enc = encode_len($len_clear); arg1: $len_clear ( integer ) the length of an unencoded string. ret: $len_enc ( integer ) the length of the string after it is encoded since: 2.0.00 See Also mod_perl 2.0 documentation. Copyright mod_perl 2.0 and its core modules are copyrighted under The Apache Software License, Version 2.0. Authors The mod_perl development team and numerous contributors. perl v5.16.2 2011-02-07 apache_mod_perl-108~358::mod_perl-2.0.7::docs::api::APR::Base64(3)
All times are GMT -4. The time now is 04:48 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy