sed command to replace multiple column in one go


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting sed command to replace multiple column in one go
# 8  
Old 04-03-2017
NO..
Input file is in loop
one by one i m replacing each input :
Code:
input - 111111111111
output-MMMMMM111111

# 9  
Old 04-03-2017
Quote:
Originally Posted by Preeti Chandra
NO..
Input file is in loop
one by one i m replacing each input :
Code:
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
# 10  
Old 04-05-2017
Hi,

My question is how to replace multiple columns using sed command.
Code:
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.
Code:
So my expected output will look like -
1101MM0MMM0010010MM011110100MMMM1011010001001MM00000100000000010XX010000000

Your solution is simply replacing values by sed cmmd -
Code:
sed 's/1101110101001001010011110100000010110100010010000000100000000010XX010000000/1101MM0MMM0010010MM011110100MMMM1011010001001MM00000100000000010XX010000000/g'
this is ok when there is only one input

Code:
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....
# 11  
Old 04-05-2017
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?
These 2 Users Gave Thanks to RudiC For This Post:
# 12  
Old 04-05-2017
Hi RudiC,


My input file -
Code:
000000011111111000000000111111110000000011111111000000001111111100000000111
000000111101101010010000001101011110100011110010101100101001101000110011110
100110101111100101001010111001010001000011000001000000010000110111010010100
110001101100111110110111100010101001101011010011111100010010000010110101000
101110111101100111111111101011100110111001100111001001110011000010010110000
110001001011111111100000001111011000011000001001010010010110010011100111011
110011111101010001010111011101000110001001111110010111111110010110001100001
100111101100001000010011011011110000001100011011101101101000101100011110010
010111001000110000101101010101100100011000001101100101011111001000001010000
000001111000010100111010010101010100110000010010111011011000011101000000010

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

Expected Output -
Code:
M0000001111111MMMM00000001111111100000000111111110000000011111MMMMMMMMMMMMM
M0000011110110MMMM01000000110101111010001111001010110010100110MMMMMMMMMMMMM
M0011010111110MMMM00101011100101000100001100000100000001000011MMMMMMMMMMMMM
M1000110110011MMMM11011110001010100110101101001111110001001000MMMMMMMMMMMMM
M0111011110110MMMM11111110101110011011100110011100100111001100MMMMMMMMMMMMM
M1000100101111MMMM10000000111101100001100000100101001001011001MMMMMMMMMMMMM
M1001111110101MMMM01011101110100011000100111111001011111111001MMMMMMMMMMMMM
M0011110110000MMMM01001101101111000000110001101110110110100010MMMMMMMMMMMMM
M1011100100011MMMM10110101010110010001100000110110010101111100MMMMMMMMMMMMM
M0000111100001MMMM11101001010101010011000001001011101101100001MMMMMMMMMMMMM

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

2nd line is second input pattern
Code:
000000111101101010010000001101011110100011110010101100101001101000110011110

and so on....
# 13  
Old 04-05-2017
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.
# 14  
Old 04-05-2017
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:
Code:
cat positions
1,15,16,17,18,63,64,65,66,67,68,69,70,71,72,73,74,75

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

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Extract a column and multiple by 1000 and replace it on same file

Hi All, I need to extract a position in the file and multiple the value by 1000 and the replace it . Original 0010001200084701217637306521200000000000010010000000 ---> 000847 * 1000 0010012700086001213437404323000000000000001001000000 ---> 000860 * 1000... (2 Replies)
Discussion started by: arunkumar_mca
2 Replies

2. Shell Programming and Scripting

sed command to replace one value which occurs multiple times

Hi, My Input File : "MN.1.2.1.2.14.1.1" := "MN_13_TM_4" ( 000000110110100100110001111110110110101110101001100111110100011010110111001 ) "MOS.1.2.1.2.13.6.2" := "MOS_13_TM_4" ( 000000110110100100110001111110110110101110101001100111110100011010110111001 ) Like above template,I have... (4 Replies)
Discussion started by: Preeti Chandra
4 Replies

3. Shell Programming and Scripting

How to search and replace string in column in file with command sed?

how to search and replace string in column in file with command sed or other search "INC0000003.in" and replace column 4 = "W" $ cat file.txt INC0000001.in|20150120|Y|N|N INC0000002.in|20150120|Y|N|N INC0000003.in|20150120|Y|N|N INC0000004.in|20150120|Y|N|Noutput... (4 Replies)
Discussion started by: ppmanja3
4 Replies

4. Shell Programming and Scripting

Search and replace multiple patterns in a particular column only - efficient script

Hi Bigshots, I have a pattern file with two columns. I have another data file. If column 1 in the pattern file appears as the 4th column in the data file, I need to replace it (4th column of data file) with column 2 of the pattern file. If the pattern is found in any other column, it should not... (6 Replies)
Discussion started by: ss112233
6 Replies

5. Shell Programming and Scripting

Awk: Multiple Replace In Column From Two Different Files

Master_1.txt 2372,MTS,AP 919821,Airtel,DL 0819,MTS,MUM 919849788001,Airtel,AP 1430,Aircel MP,20 405899143999999,MTS,KRL USSDLIKE,MTS,DEL Master_2.txt 919136,DL 9664,RAJ 919143,KOL 9888,PUN Input File: (4 Replies)
Discussion started by: siramitsharma
4 Replies

6. Shell Programming and Scripting

Sed/awk/perl command to replace pattern in multiple lines

Hi I know sed and awk has options to give range of line numbers, but I need to replace pattern in specific lines Something like sed -e '1s,14s,26s/pattern/new pattern/' file name Can somebody help me in this.... I am fine with see/awk/perl Thank you in advance (9 Replies)
Discussion started by: dani777
9 Replies

7. Shell Programming and Scripting

sed multiple replace

Hello! I'm using sed to perform a lots of replaces in one text file. I call it this way: sed -f commands.txt in.txt > out.txt commands.txt has about 1000 lines, each one is some variation of: s/from/to/gI And in.txt has about 300 000 lines. So the problem is that operation takes about... (7 Replies)
Discussion started by: backdrift
7 Replies

8. Shell Programming and Scripting

Loop with sed command to replace line with sed command in it

Okay, title is kind of confusion, but basically, I have a lot of scripts on a server that I need to replace a ps command, however, the new ps command I'm trying to replace the current one with pipes to sed at one point. So now I am attempting to create another script that replaces that line. ... (1 Reply)
Discussion started by: cbo0485
1 Replies

9. Shell Programming and Scripting

awk/sed column replace using column header - help

$ cat log.txt Name Age Sex Lcation nfld alias xsd CC 25 M XYZ asx KK Y BB 21 F XAS awe SS N SD 21 M AQW rty SD A How can I replace the column with header "Lcation" with the column with header "alias" and delete the "alias" column? so that the final output will become: Name Age Sex... (10 Replies)
Discussion started by: jkl_jkl
10 Replies

10. Shell Programming and Scripting

using sed command to replace multiple lines

the file contains the follwoing lines /* * Copyright (C) 1995-1996 by XXX Corporation. This program * contains proprietary and confidential information. All rights reserved * except as may be permitted by prior written consent. * * $Id: xxx_err.h,v 1.10 2001/07/26 18:48:34 zzzz $ ... (1 Reply)
Discussion started by: radha.kalivar
1 Replies
Login or Register to Ask a Question