Jose Campos
Jose Campos

Reputation: 23

find pattern in a column and replace it with awk

I was trying to do the following.

I was trying to put column 1 when I found a pattern that would be "undefined", for example:

I have my file.txt file that contains

192.168.1.1, Paul

192.168.2.2, undefined

Here it is "undefined", then replace it with the first column in the row where it is "undefined". (192.168.2.2)

192.168.1.1, Paul

192.168.2.2, 192.168.2.2

Can somebody help me?

Upvotes: 0

Views: 856

Answers (3)

Kent
Kent

Reputation: 195229

This line works for your example, no matter the separator is , or ,(space)

awk -F, '1+sub(/undefined$/,$1)' file

Upvotes: 0

oliv
oliv

Reputation: 13259

You can use sed:

sed 's/\(.*\), undefined$/\1, \1/' file.txt

or

sed -r 's/(.*), undefined$/\1, \1/' file.txt

Upvotes: 0

Rahul Verma
Rahul Verma

Reputation: 3089

using awk

$ awk -F", " '$2~/undefined/{ print $1 FS $1; next}1' file
192.168.1.1, Paul

192.168.2.2, 192.168.2.2

Upvotes: 1

Related Questions