drewrockshard
drewrockshard

Reputation: 2071

Replace first occurrence in row

I'm trying to figure out how to replace the first occurrence of a match (search/replace) via sed. I want it line by line (which I think is the default with sed).

Here's my dataset:

(4320, 38, '101.1.168.192.in-addr.arpa', 'PTR', 'test1.example.com', 3600, NULL, 1377614683),
(4321, 38, '102.1.168.192.in-addr.arpa', 'PTR', 'test2.example.com', 3600, NULL, 1377614683),
(4322, 38, '103.1.168.192.in-addr.arpa', 'PTR', 'test3.example.com', 3600, NULL, 1377614683),
(4323, 38, '104.1.168.192.in-addr.arpa', 'PTR', 'test4.example.com', 3600, NULL, 1377614683),

This is just a snip – it's a huge list. I'd like to take out the first number, for each line, specifically find ([0-9], with (,. This is so that I can have this SQL statement assign and auto_increment value by default, since I'll be taking this dataset and importing it into a table (that could have conflicts with the auto_increment values).

Here's what I would like it to look like when done:

( , 38, '101.1.168.192.in-addr.arpa', 'PTR', 'test1.example.com', 3600, NULL, 1377614683),
( , 38, '102.1.168.192.in-addr.arpa', 'PTR', 'test2.example.com', 3600, NULL, 1377614683),
( , 38, '103.1.168.192.in-addr.arpa', 'PTR', 'test3.example.com', 3600, NULL, 1377614683),
( , 38, '104.1.168.192.in-addr.arpa', 'PTR', 'test4.example.com', 3600, NULL, 1377614683)

I believe that would work. I've tried some sed commands, but they all seem to remove the whole match until the end of the line (since I am matching what I am matching, it basically removes each [0-9].*,).

Upvotes: 0

Views: 103

Answers (1)

Avinash Raj
Avinash Raj

Reputation: 174706

You could try the below GNU sed command,

$ sed -r 's/^\([0-9]+/( /' file
( , 38, '101.1.168.192.in-addr.arpa', 'PTR', 'test1.example.com', 3600, NULL, 1377614683),
( , 38, '102.1.168.192.in-addr.arpa', 'PTR', 'test2.example.com', 3600, NULL, 1377614683),
( , 38, '103.1.168.192.in-addr.arpa', 'PTR', 'test3.example.com', 3600, NULL, 1377614683),
( , 38, '104.1.168.192.in-addr.arpa', 'PTR', 'test4.example.com', 3600, NULL, 1377614683),

Upvotes: 3

Related Questions