sed command to replace multiple column in one go

Hi,

I want to replace the value in more than one column.
For one column ,following command is working -

sed 's/./$value_to_replace/$column number' file_name
e.g. suppose this is input 1111000000
command - sed 's/./M/5'
output= 1111M000000

For two column also command is like -

 cat file_name | sed 's/./M/5 | sed 's/./M/6' 

But if I want to replace n value how to do that?

Thanks...

HI,

sed 's/pattern_to_be_replaced/New_pattern/g' file_name

Regard's
Venkat

Hello Preeti Chandra,

There are 2 things here.
I- Please DO NOT use cat Input_file | sed ...... as sed ....... Input_file , it can read Input_file by itself.
II- To substitute all the occurrences of a string you should use sed 's/old_text/new_text/g with single sed only.

Just try it out and let us know how it goes then.

Thanks,
R. Singh

My question is entirely different.

Input-sequence of binary number(it a file contains multiple values)
from this sequence I want to change some particular column for each input

I have to change some value in the input after that this substitute command .
But above two commands are working fine but I want to change 10 columns in one input ,how to do that??

Hello Preeti,

I think whatever was asked in your post I had tried to answer it, Without letting us know what is sample Input_file and expected output with all conditions by which you want to get the expected result how we could help you? Kindly do help us by providing all details to help you.

Thanks,
R. Singh

Input file -

000000011111111000000000111111110000000011111111000000001111111100000000111
000000111101101010010000001101011110100011110010101100101001101000110011110
100110101111100101001010111001010001000011000001000000010000110111010010100
110001101100111110110111100010101001101011010011111100010010000010110101000
101110111101100111111111101011100110111001100111001001110011000010010110000
110001001011111111100000001111011000011000001001010010010110010011100111011
110011111101010001010111011101000110001001111110010111111110010110001100001
100111101100001000010011011011110000001100011011101101101000101100011110010
010111001000110000101101010101100100011000001101100101011111001000001010000
000001111000010100111010010101010100110000010010111011011000011101000000010
110110000011000111010000101111011000100000010100001100100111001000111010011
000100011011100011100111100011001000011100001100010011000111000111101010010
100010100001111101100001010101010011000001001100001000111010011111110111110
000110001111100000100001110010110110111001111110101000010001000001111011010
001101011001001101000010010100011100001000000101000010110000000000111010000
010101111000001111111101111000011000111000011000001110000001111110001101100
100000011010111100111101001100100100111000000001011100010111001010011000100
011111100110101011101001111111010011001001000010111000101110111101011010011
111001001101001101010000000101000011001001011110010111100100111000000001101
111110011010111100111010111000010001111011101110101010101111000000111111000
100010010011010010100100011110000011010001001000010010000100011000111101110
001010000001101101010000100010111110100001101111111011111110101101000110001
001101101100100110011011100001110111011001010100010101000101111110100001111
111111001001000000010011111000011010011001011001100111011101110001011101110
111000101000000011101110101001111100000000010000100001000110001011000010110
100000101101100010001000011000111000010000001011101111100101100001100000000
001111110000101000010110110001010000101000001000110010001110001011001111011
010010100001000111011011101001010110000001111010111101110101100110100001111
111001111001110010101110011100111011101001010110000101100001001100101000001
110001111001000111011101101010001011000001000001000000010011100001100110001
111100111110000000011100010101110001010011001110000011100000000111001100110
010101100000001111000111111010011010100001000000001111101101010111001010111
111000000101110101011100001101111001001011000010010000100111010110001110100
110000111110101000011001001001011011111001011110100111101001100111000010010
100101000000101000010010011000000011000001000011000000110011011010010000110
000001001010000100000001000001000000011000010001100100011001101110101010100
110011011101010111011000110111101000010100011100110111001110011101110110110
001011011001001010001100010101011010000001000001011010010110111111011000000
100111010100100010010010001101111100101000000100011001000101000100001110110
101000101110110010010001011011000000110100000010011101001011000110100000001
110011100001011001010110011000101011111001000101010001010101101100001011000
001101111111010100010100010010101000001100000001000000010010010011010001101
111101001000001001110100111011110011001001011101100111011010100011011110010
101001000010101111001100001111111100001000001100100011001010101101100011100
010110110011000111001100100000101010001001010111011001000011100000001001011
101010110100001111110000111101011100101000010000111100110111000000100011001
001011001010100110101000001100101010010001000000101000000010111001010001100
000000111010001011100010011101100011101001000001010000010111010110011100110
011010111000000111101010100000100100010000001100001010011111100000110010011
011100111010110101100100100010010011000001010100001101000000010100110101100
110011101110010010100010111001111010011001011101111011011010001010100101111
001000101101111011110110001011010111100001000011111001000010000010010001100
011101101100110100101101111111001001101011011110101100011001010100101110010
000111011001010010101101101001110011010001010110110101101110001100000110111
100011011101110001011111000000010100011000011000000011110101111000010111001
001101011101001100100000000101000010011001001010000000001001011110010100011
000001010011011010111010110001110010010001000100011011101011000101110010111
011101100100110011110011011001100000111000001100100011001000001011101000011
001010000001000101101111110110100000101100011101001000001101110100001011010
011101010010101111010001000101010000101000000100101100010100000000011010000
101010111000010010110001011011110100011000011010101011001011011101011111110
010000011110101000001001101100110100000000010111111110000010110010111100110
010101001101100011111101011111000001111011110011100100111011101001011101100
010010111100100001100010100000000111101110011111110111011000100100000010011
011101101001101100010000011000101100011000000011010000110100111110011111110
111011011010111110111000110101011111111001001000001111011001111110010101101
101110010010010101111010111100101011010001011100110111001101111110110011100
101111111001011001010010010001100111000001001100101011001011011001111110100
101101000100100011011011110011111001010011110100010101000100100101101010011
000010000010110000101011011000010010001001001000001100100101010010010110100
010011101111111111001010011010010110101001110110111101101101000100011100000
001011110011101010000001101100001000111000000100001110001111111001100101010
101101110011111111110101110111010110110011100110000001100011110001011001000
010110010111011111011000110100000100001000001000110010001100110110001001010
001001001111100101010101100101000110110001110110101101101011001101011101110
011101000100100101111011001001010011000001000010000000100011000001100111110
110010100000010101101001101101001011100001000010011000001101101100010111100
101100101101110100011001100110110111011001011011000110110000101100110101011
100111111101011111100101100111111001110011001111101110010010001011101010011
011010100110000110001111001101101110111011110000111110011100100001110110011
111110000011000000111011011011001000101100011001100110011010110111111010100
111101110101110100101010100110001000001000011110010111100111010010111001001
101011000110000111001100000111001000100100010010000100100011011110100010100
111001111100000000111110010101111110011001111010001110001110000011110100000
001110010000111110001110001011000100110000001011100010111010101011010101100
000111101110000101011110110001011100111000000110000100101011011001001001100
010101100000110000100001011111000110100011111111100111111001000000101001110
000011111010001000101010100111000000100000000101100001011010100011001000110
000111110010001101101101111000101011100001010101011111011100011110100000100
010000100101111100011000100001010000110000001011110010111100000101000100100
011001001001011010100101011000011010010001000000100100001000001111101111011
001011110101011011111001011011000100111000001100001010101001111101100000100
010010010100010110001001110100000011110001010110111011011011000100011010110
011101000011001011011001010000111111100001000000110000001111101011010000010
010010100101001011001101001010100000011100011101100111011000011000010100000
010011000011011001000011111111001100001000011111010111110110110110100111100
000001001110111010110011000001101111000001001001010010010110111100000100001
110111100011110100010011010100111011101001001001100010011000010011100111011
001110110111111001110000011110010101100001011111110111111101111101000001010
101100110000011000011100000100100100100000001010010010100110101100111001010
111011000111011000100110001010000010100001000100000010110110100001101111110
011100001100010001011101100000111000000000001111110011111110101101110100010
010000010111101000110000001010101110000001110101011000011101011000010110000
100011001010011101111011010110000110011001101101111011011100111100010100110
001010111100011100101101001110100001000011100011001000110001010101101010000
011111001000101110101111011100000001001011010111000101110010010111100011100
000001101010101010011010100010111110001001111101110111011111111100111011111
000100000100000001111111000011110011110001010111101101111011101011010101100
110101011001010001010001001001010001100011000101100001011001101001110010000
111001011110010010100110011001100100101000010001101001010111110111111011100
000001011011111000011100110001111100011000010100110101001101100101111011000
010100110010011000000001000010001011111001000100010001000100011010111011010
110000110011001110011110110110111100011000011001001110010000110110010101001
111111101011000110100111000100001001111011011111010111110110111010101100101
000101101011010001100000101110001011001001001000101010001001110100010010101
000001100011010001010110001000100000001000010100110101001110110100011000010
100001111000011101001001001001100110000001101001011111010000011101111100000
001000010010101100010101010101101001111011000000011010111101000000111101110
011011111000110111110010101010011110101011100001000000010001110001010100100

output - for one input likewise i want for all values present in the input file

011011111MMMMMMMMMM10010101010011110101011100001000000010001110001010100100

Hello Preeti,

Not much clear, suspecting that you need to substitute string 011011111 to 011011111MMMMMMMMMM then following may help you in same.

sed 's/011011111/011011111MMMMMMMMMM/g'  Input_file

Thanks,
R. Singh

NO..
Input file is in loop
one by one i m replacing each input :

input - 111111111111
output-MMMMMM111111

Hello Preeti,

Without knowing your complete requirements, I believe it is not at all possible for us to help you. You showed us a simple Input_file and wanted to substitute a string in it which I had provided that solution. Now you mentioned that Input_file is in loop, so may I ask why Input_file is in loop when sed could read the Input_file by him itself?
It's better to re-think and write your problem and decide what you want to ask to us(your confusions, issues etc) with meaningful samples of Input_file and expected output file.

Thanks,
R. Singh

Hi,

My question is how to replace multiple columns using sed command.

For e.g. To Change one column I am using below command :

/home/chandrap> cat c
1101110101001001010011110100000010110100010010000000100000000010XX010000000
/home/chandrap> sed 's/./M/5' c
1101M10101001001010011110100000010110100010010000000100000000010XX010000000

this sed replaces 5th bit of my input by M.

In similar manner I want to change value at multiple places, I have one file(binary numbers)which I already shared in my previous post.

So my expected output will look like -
1101MM0MMM0010010MM011110100MMMM1011010001001MM00000100000000010XX010000000

Your solution is simply replacing values by sed cmmd -

sed 's/1101110101001001010011110100000010110100010010000000100000000010XX010000000/1101MM0MMM0010010MM011110100MMMM1011010001001MM00000100000000010XX010000000/g'
this is ok when there is only one input 
sed 's/First_value/Second_value'

But in my case I have to drive the second value from the first value after altering some of the input bit .

I hope now you clear with my question....

ABSOLUTELY NOT!

Please supply facts and data. Show decent, meaningful (not overwhelming) sample input, the related output, and the hard logics connecting the two.
What does "change value at multiple places" mean? Where in the input lines should those "M"s appear? Any rules, patterns? Line numbers? Char positions? All lines according to the same?

2 Likes

Hi RudiC,

My input file -

000000011111111000000000111111110000000011111111000000001111111100000000111
000000111101101010010000001101011110100011110010101100101001101000110011110
100110101111100101001010111001010001000011000001000000010000110111010010100
110001101100111110110111100010101001101011010011111100010010000010110101000
101110111101100111111111101011100110111001100111001001110011000010010110000
110001001011111111100000001111011000011000001001010010010110010011100111011
110011111101010001010111011101000110001001111110010111111110010110001100001
100111101100001000010011011011110000001100011011101101101000101100011110010
010111001000110000101101010101100100011000001101100101011111001000001010000
000001111000010100111010010101010100110000010010111011011000011101000000010

Condition -

Replace 1,15,16,17,18,63 to 75 bit of each input by M in one go.

Expected Output -

M0000001111111MMMM00000001111111100000000111111110000000011111MMMMMMMMMMMMM
M0000011110110MMMM01000000110101111010001111001010110010100110MMMMMMMMMMMMM
M0011010111110MMMM00101011100101000100001100000100000001000011MMMMMMMMMMMMM
M1000110110011MMMM11011110001010100110101101001111110001001000MMMMMMMMMMMMM
M0111011110110MMMM11111110101110011011100110011100100111001100MMMMMMMMMMMMM
M1000100101111MMMM10000000111101100001100000100101001001011001MMMMMMMMMMMMM
M1001111110101MMMM01011101110100011000100111111001011111111001MMMMMMMMMMMMM
M0011110110000MMMM01001101101111000000110001101110110110100010MMMMMMMMMMMMM
M1011100100011MMMM10110101010110010001100000110110010101111100MMMMMMMMMMMMM
M0000111100001MMMM11101001010101010011000001001011101101100001MMMMMMMMMMMMM
For example - 01010101 is replace M1M1M1M1
Here alternate bits are changed by M.

---------- Post updated at 05:47 AM ---------- Previous update was at 05:45 AM ----------

each input means -
1st line of input file is first input pattern

000000011111111000000000111111110000000011111111000000001111111100000000111

2nd line is second input pattern

000000111101101010010000001101011110100011110010101100101001101000110011110

and so on....

OK, that's better. I'm not sure (yet) this is doable in sed . How are those numbers supplied? Manually entered, in a variable, in a file?

For your second example - that's fuzzy again. Will changes start at 1. or 2. bit? It's about data processing, not data guessing.

I don't think you can do it in one single sed command. But I'd like to learn if someone comes up with sth. better...
Given that the numbers are in a file called positions :

cat positions
1,15,16,17,18,63,64,65,66,67,68,69,70,71,72,73,74,75

, try

tr ',' $'\n' < positions | sed 's#^#s/./M/#' | sed -f- file
M0000001111111MMMM00000011111111000000001111111100000000111111MMMMMMMMMMMMM
M0000011110110MMMM01000000110101111010001111001010110010100110MMMMMMMMMMMMM
M0011010111110MMMM00101011100101000100001100000100000001000011MMMMMMMMMMMMM
M1000110110011MMMM11011110001010100110101101001111110001001000MMMMMMMMMMMMM
M0111011110110MMMM11111110101110011011100110011100100111001100MMMMMMMMMMMMM
M1000100101111MMMM10000000111101100001100000100101001001011001MMMMMMMMMMMMM
M1001111110101MMMM01011101110100011000100111111001011111111001MMMMMMMMMMMMM
M0011110110000MMMM01001101101111000000110001101110110110100010MMMMMMMMMMMMM
M1011100100011MMMM10110101010110010001100000110110010101111100MMMMMMMMMMMMM
M0000111100001MMMM11101001010101010011000001001011101101100001MMMMMMMMMMMMM

NOT WORKING ...

Error Message -

/home/chandrap> cat positions 
1,15,16,17,18,63,64,65,66,67,68,69,70,71,72,73,74,75
/home/chandrap> tr ',' $\'\n' < positions | sed 's#^#s/./M/#' | sed -f list 
Illegal variable name.
/home/chandrap> tr ',' $\'\n' < positions | sed 's#^#s/./M/#' | sed -f- list 
Illegal variable name.

NOT TRUE.

That proposal was successfully tested on a linux host with bash shell, and, with a small adaption, on a FreeBSD host with bash .

So a reasonable statement from your side might be: "Not working on my system (without adaption)." (BTW, we don't know anything about your system...)

Did you bother to find out WHAT causes the error? WHERE it occurs?

Output in bash shell-

bash-4.1$ cat positions
1,15,16,17,18,63,64,65,66,67,68,69,70,71,72,73,74,75
bash-4.1$ tr ',' $\'\n' < positions | sed 's#^#s/./M/#' | sed -f list
> 

My script is in tcsh shell.But i tried in bash shell but it is not working here also.
Just check once.Is this correct or not?

@Preeti: This backslash should not be there $\'\n' , plus it said sed -f- , not sed -f

--
@RudiC, that seems to work with GNU sed, but BSD sed gave:

sed: -: No such file or directory

--
Another option:

sed 's/./M/; s/\(.\{14\}\)..../\1MMMM/; s/\(.\{62\}\).\{13\}/\1MMMMMMMMMMMMM/' file

or

sed 's/./M/; s/\(.\{14\}\)..../\1MMMM/; s/.\{13\}$/MMMMMMMMMMMMM/' file

Using GNU awk 4:

awk4 '{for(i=1; i<=NF; i+=2) gsub(/./,"M",$i)}1' OFS= FIELDWIDTHS="1 13 4 44 13" file

@RudiC Thanks you for the solution.
can u please tell me how to change this command for tcsh shell.
And brief explanation of -

tr ',' $'\n' < positions | sed 's#^#s/./M/#' | sed -f- list

@Scrutinizer Thanks for modifying the command.
Now it is working in bash shell as expected.

I can't comment on tcsh . But - there's a tr too many, and its removal may eliminate the shell problem as well. Try (assuming your sed allows for the exetended regexes -r ; check man page!):

sed -r 's#([^,]*)(,|$)#s/./M/\1\n#g' positions | sed -f- file

@Scrutinizer: BSD sed doesn't accept stdin for the script file. That's why I said "small adaption"; but the principle works. Try on BSD (note the literal <newline> char in the first sed script)

mkfifo TMPIPE
sed -E "s#([^,]*)(,|$)#s/./M/\1\\
#g" positions | tee TMPIPE | sed -fTMPIPE file
rm TMPIPE