Bulk changes using sed


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Bulk changes using sed
# 1  
Old 09-04-2014
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

Code:
-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
# 2  
Old 09-04-2014
Code:
| sed 's/10\.0\.108\.*/10.0.108.11/'

# 3  
Old 09-04-2014
try

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

# 4  
Old 09-04-2014
but how do i make sure that only field 24 is changed?
# 5  
Old 09-04-2014
Hello kekanap,

Following may help you(Not tested).

Code:
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.

Code:
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

Last edited by RavinderSingh13; 09-04-2014 at 12:17 PM.. Reason: Added OFS and FS details
# 6  
Old 09-04-2014
Mind the details:
Code:
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:
Code:
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) <=

Last edited by Scrutinizer; 09-04-2014 at 12:18 PM..
These 2 Users Gave Thanks to Scrutinizer For This Post:
# 7  
Old 09-04-2014
Code:
awk -F, '{sub(/^10\.0\.108\..*/,"10.0.108.11",$24)}1' pax1.csv

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Mailx bulk operations

Greeting. How to download messages in bulk in mailx? e.g. how to download all the message received on mar-23-2017 and save as a file or download individual mailx message that received on mar-23-2017 as individual file and as a loop? We are on HP-UX centstdb B.11.31 U ia64 3294693583... (1 Reply)
Discussion started by: moneric
1 Replies

2. Shell Programming and Scripting

Parsing Bulk Data

Hi All, :D Actullay I am looking for a smart way :b: to parse files in a directory whose count is around 2000000 :eek: in a single day. Find is working with me but taking a lot of times :confused:, sometimes even a day which is not helping me.:wall: So anyone can help me know a smart... (5 Replies)
Discussion started by: jojo123
5 Replies

3. Shell Programming and Scripting

Renaming files in bulk.

Hi, I have a bunch of files which are named something like: Company Name~1234~X1234~X1-123.pdf I need to get them renamed something like: Company Name~1234(X1234)X1-123.pdf Once I have the X1234 inside () I have a piece of software which can use the X1234 bit. I will be receiving... (7 Replies)
Discussion started by: jcborland
7 Replies

4. Shell Programming and Scripting

Bulk Find and Replace

Hi Friends, I have a directory with a ton of .html files, like this ls -m1 dir 1.html 2.html 3.html 4.html Somewhere in the files, there is a pattern like this 1.html http://unix.com/cgi-bin/task?taskid=12010&task.out 2.html http://unix.com/cgi-bin/task?taskid=11110&task.out... (1 Reply)
Discussion started by: jacobs.smith
1 Replies

5. Shell Programming and Scripting

Bulk rename

hi, my directory has the following files I want to rename or mv them as file 1 corresponds to new_1.bed, file2 to new_2.bed and so on. How do I do it using awk or bash? TIA (4 Replies)
Discussion started by: jacobs.smith
4 Replies

6. Shell Programming and Scripting

Change part of filenames in a bulk way

Hallo! I have generated lots of data file which all having this format: sp*t1overt2*.txt Now I want to change them in this way: sp*t2overt1*.txt The rest of the file names stay unchanged. I know this is kind of routine action in sed or awk, but dont know how! I tried this command: ... (6 Replies)
Discussion started by: forgi
6 Replies

7. AIX

Bulk Fixes Selection

Under smit, one has to manually select each fix with F7. there 9000 fixes left to be marked. How Can I manually install/Mark all of these without SMIT. ---------- Post updated at 02:29 PM ---------- Previous update was at 01:15 PM ---------- From the command line instfix -T -d... (4 Replies)
Discussion started by: mrmurdock
4 Replies

8. Shell Programming and Scripting

Need help renaming bulk file extentions

Hello, I am trying to rename bulk files however i dont think the rename/mv command is giong to help me here. here is a quick snapshot of the files I need to rename: 75008040 -rw-r----- 1 root root 8716 May 8 05:00 10.9.144.2 75008041 -rw-r----- 1 root root 11700 May 8 05:00 10.9.160.2... (10 Replies)
Discussion started by: jallan
10 Replies

9. Shell Programming and Scripting

outcomment bulk

Hello chiefs, So i wanna outcomment several lines, but i dont want to start each line with #-sign. Once i saw it in use, but dont remember the syntax. It should work with sh or ksh. regards congo (3 Replies)
Discussion started by: congo
3 Replies

10. BSD

bulk pkg_delete (FreeBSD)

I have been toying with this all night now and cannot seem to get it to work, so I came to you for some aid. I recently discovered the wonders of the AWK programming language and how it can be useful of UNIX system administratioin, especially directly from and interacitive interpreter (ala sh). ... (0 Replies)
Discussion started by: erebus47
0 Replies
Login or Register to Ask a Question