3 added 152 characters in body
source | link

You didn't mention what you want to replace it with so I'm just going to use ghi as a placeholder:

If that is the actual text then the easiest way is with sed. You can do it with awk but that will only send the modification to standard output whereas sed can change the file itself which is explained below:

sed '2s|xyz|ghi|1' file

To explain:

2s replaces instances on the second line
xyz is what you want to replace
ghi is what it's being replaced with
1 replaces the first instance of it on the specified line

The output:

col1,col2,col3,col4
abc,def,ghi,xyz
xyz,abc,def,xyz

Once you're sure that it does what you want, use the -i switch to change the file itself instead of sending it to standard output:

sed -i '2s|xyz|ghi|1' file

If there are other lines and columns where you might want to replace it, you'll need to modify 2s and 1 to specify the location(s) and instance(s). Obviously, you'd also replace ghi with whatever replacement you'd like.

If there are other lines in column 3 where you need to replace xyz, you can use the e switch. For example:

col1,col2,col3,col4
abc,def,xyz,xyz
xyz,abc,def,xyz
abc,def,xyz,abc

xyz appears in the fourth line of column 3 as well.

sed -e '2s|xyz|ghi|1' -e '4s|xyz|ghi|1' file

sed -i -e '2s|xyz|ghi|1' -e '4s|xyz|ghi|1' file

That would replace the first instance on the fourth line as well using -e to specify each stream of replacements. The -i switch changes the file instead of only sending it to standard input.

You didn't mention what you want to replace it with so I'm just going to use ghi as a placeholder:

If that is the actual text then the easiest way is with sed:

sed '2s|xyz|ghi|1' file

To explain:

2s replaces instances on the second line
xyz is what you want to replace
ghi is what it's being replaced with
1 replaces the first instance of it on the specified line

The output:

col1,col2,col3,col4
abc,def,ghi,xyz
xyz,abc,def,xyz

Once you're sure that it does what you want, use the -i switch to change the file itself instead of sending it to standard output:

sed -i '2s|xyz|ghi|1' file

If there are other lines and columns where you might want to replace it, you'll need to modify 2s and 1 to specify the location(s) and instance(s). Obviously, you'd also replace ghi with whatever replacement you'd like.

If there are other lines in column 3 where you need to replace xyz, you can use the e switch. For example:

col1,col2,col3,col4
abc,def,xyz,xyz
xyz,abc,def,xyz
abc,def,xyz,abc

xyz appears in the fourth line of column 3 as well.

sed -e '2s|xyz|ghi|1' -e '4s|xyz|ghi|1' file

sed -i -e '2s|xyz|ghi|1' -e '4s|xyz|ghi|1' file

That would replace the first instance on the fourth line as well using -e to specify each stream of replacements. The -i switch changes the file instead of only sending it to standard input.

You didn't mention what you want to replace it with so I'm just going to use ghi as a placeholder:

If that is the actual text then the easiest way is with sed. You can do it with awk but that will only send the modification to standard output whereas sed can change the file itself which is explained below:

sed '2s|xyz|ghi|1' file

To explain:

2s replaces instances on the second line
xyz is what you want to replace
ghi is what it's being replaced with
1 replaces the first instance of it on the specified line

The output:

col1,col2,col3,col4
abc,def,ghi,xyz
xyz,abc,def,xyz

Once you're sure that it does what you want, use the -i switch to change the file itself instead of sending it to standard output:

sed -i '2s|xyz|ghi|1' file

If there are other lines and columns where you might want to replace it, you'll need to modify 2s and 1 to specify the location(s) and instance(s). Obviously, you'd also replace ghi with whatever replacement you'd like.

If there are other lines in column 3 where you need to replace xyz, you can use the e switch. For example:

col1,col2,col3,col4
abc,def,xyz,xyz
xyz,abc,def,xyz
abc,def,xyz,abc

xyz appears in the fourth line of column 3 as well.

sed -e '2s|xyz|ghi|1' -e '4s|xyz|ghi|1' file

sed -i -e '2s|xyz|ghi|1' -e '4s|xyz|ghi|1' file

That would replace the first instance on the fourth line as well using -e to specify each stream of replacements. The -i switch changes the file instead of only sending it to standard input.

2 added 654 characters in body
source | link

You didn't mention what you want to replace it with so I'm just going to use ghi as a placeholder:

If that is the actual text then the easiest way is with sed:

sed '2s|xyz|ghi|1' file

To explain:

2s replaces instances on the second line
xyz is what you want to replace
ghi is what it's being replaced with
1 replaces the first instance of it on the specified line

The output:

col1,col2,col3,col4
abc,def,ghi,xyz
xyz,abc,def,xyz

Once you're sure that it does what you want, use the -i switch to change the file itself instead of sending it to standard output:

sed -i '2s|xyz|ghi|1' file

If there are other lines and columns where you might want to replace it, you'll need to modify 2s and 1 to specify the location(s) and instance(s). Obviously, you'd also replace ghi with whatever replacement you'd like.

If there are other lines in column 3 where you need to replace xyz, you can use the e switch. For example:

col1,col2,col3,col4
abc,def,xyz,xyz
xyz,abc,def,xyz
abc,def,xyz,abc

xyz appears in the fourth line of column 3 as well.

sed -e '2s|xyz|ghi|1' -e '4s|xyz|ghi|1' file

sed -i -e '2s|xyz|ghi|1' -e '4s|xyz|ghi|1' file

That would replace the first instance on the fourth line as well using -e to specify each stream of replacements. The -i switch changes the file instead of only sending it to standard input.

You didn't mention what you want to replace it with so I'm just going to use ghi as a placeholder:

If that is the actual text then the easiest way is with sed:

sed '2s|xyz|ghi|1' file

To explain:

2s replaces instances on the second line
xyz is what you want to replace
ghi is what it's being replaced with
1 replaces the first instance of it on the specified line

Once you're sure that it does what you want, use the -i switch to change the file itself instead of sending it to standard output:

sed -i '2s|xyz|ghi|1' file

If there are other lines and columns where you might want to replace it, you'll need to modify 2s and 1 to specify the location(s) and instance(s). Obviously, you'd also replace ghi with whatever replacement you'd like.

You didn't mention what you want to replace it with so I'm just going to use ghi as a placeholder:

If that is the actual text then the easiest way is with sed:

sed '2s|xyz|ghi|1' file

To explain:

2s replaces instances on the second line
xyz is what you want to replace
ghi is what it's being replaced with
1 replaces the first instance of it on the specified line

The output:

col1,col2,col3,col4
abc,def,ghi,xyz
xyz,abc,def,xyz

Once you're sure that it does what you want, use the -i switch to change the file itself instead of sending it to standard output:

sed -i '2s|xyz|ghi|1' file

If there are other lines and columns where you might want to replace it, you'll need to modify 2s and 1 to specify the location(s) and instance(s). Obviously, you'd also replace ghi with whatever replacement you'd like.

If there are other lines in column 3 where you need to replace xyz, you can use the e switch. For example:

col1,col2,col3,col4
abc,def,xyz,xyz
xyz,abc,def,xyz
abc,def,xyz,abc

xyz appears in the fourth line of column 3 as well.

sed -e '2s|xyz|ghi|1' -e '4s|xyz|ghi|1' file

sed -i -e '2s|xyz|ghi|1' -e '4s|xyz|ghi|1' file

That would replace the first instance on the fourth line as well using -e to specify each stream of replacements. The -i switch changes the file instead of only sending it to standard input.

1
source | link

You didn't mention what you want to replace it with so I'm just going to use ghi as a placeholder:

If that is the actual text then the easiest way is with sed:

sed '2s|xyz|ghi|1' file

To explain:

2s replaces instances on the second line
xyz is what you want to replace
ghi is what it's being replaced with
1 replaces the first instance of it on the specified line

Once you're sure that it does what you want, use the -i switch to change the file itself instead of sending it to standard output:

sed -i '2s|xyz|ghi|1' file

If there are other lines and columns where you might want to replace it, you'll need to modify 2s and 1 to specify the location(s) and instance(s). Obviously, you'd also replace ghi with whatever replacement you'd like.