Bulk changes using sed

Hallo Team,

I would like to change everything on field 24 that starts with

10.0.108.* to be 10.0.108.11

-bash-3.2$ cat pax1.csv|cut -f24 -d","|sort -u
10.0.108.11
10.0.108.11100
10.0.108.11102
10.0.108.11104
10.0.108.11105
10.0.108.11106
10.0.108.11108
10.0.108.11110
10.0.108.11111
10.0.108.11112
10.0.108.113
10.0.108.11:5060
10.0.108.116
10.0.108.117
10.0.108.119
10.0.108.1197
10.0.108.1198
10.0.108.1199
10.0.108.11.g
10.0.108.20:5060
10.0.109.10
10.0.109.10:5060
10.0.109.17:5060
10.0.28.108
10.0.4.227
172.29.4.248
172.29.9.32
172.31.6.189
172.31.6.196
172.31.6.239
172.31.6.27
172.31.7.154
172.31.7.22
172.31.7.27
196.35.130.3
196.35.130.3:5060
196.35.130.5
-bash-3.2$

at the moment i am changing them one by one and it is time consuming. Can you please suggest a better faster way for me?

Regards,

Pax

| sed 's/10\.0\.108\.*/10.0.108.11/'

try

sed 's/10.0.108.*/10.0.108.11/g' file

but how do i make sure that only field 24 is changed?

Hello kekanap,

Following may help you(Not tested).

awk '{for(i=1;i<=NF;i++){{if(i==24){match($i,/.*\.+/);v=substr($i,RSTART,RLENGTH);$i=v"11"}}}} 1' filename

EDIT: I have assumed field seprator is space so if that is , etc then we can add FS=, and OFS=,
in code like as follows.

awk '{for(i=1;i<=NF;i++){{if(i==24){match($i,/.*\.+/);v=substr($i,RSTART,RLENGTH);$i=v"11"}}}} 1' FS="," OFS="," filename

Thanks,
R. Singh

Mind the details:

sed 's/^10\.0\.108\..*/10.0.108.11/' file

There needs to be an ^ anchor at the start , otherwise it may match 110.0.108. for example. The anchor at the back can be provided by the third literal dot (although 108 is already three number so it will probably go alright in this case)

To change in the original file in field 24, you need something like:

sed 's/^\(\([^,]*,\)\{23\}\)10\.0\.108\.[^,]*/\110.0.108.11/' file

In both cases there is no need for the g flag

Or probably better to use an an awk solution ...

=> (In which case there needs to be an FS=, and and OFS=, variable) <=

2 Likes
awk -F, '{sub(/^10\.0\.108\..*/,"10.0.108.11",$24)}1' pax1.csv