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
# 15  
Old 04-05-2017
NOT WORKING ...

Error Message -

Code:
/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.

# 16  
Old 04-05-2017
Quote:
Originally Posted by Preeti Chandra
NOT WORKING ...
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?
# 17  
Old 04-06-2017
Output in bash shell-
Code:
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?
# 18  
Old 04-06-2017
@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:
Code:
sed: -: No such file or directory


--
Another option:
Code:
sed 's/./M/; s/\(.\{14\}\)..../\1MMMM/; s/\(.\{62\}\).\{13\}/\1MMMMMMMMMMMMM/' file

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


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


Last edited by Scrutinizer; 04-06-2017 at 02:05 AM..
# 19  
Old 04-06-2017
@RudiC Thanks you for the solution.
can u please tell me how to change this command for tcsh shell.
And brief explanation of -
Code:
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.
# 20  
Old 04-06-2017
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!):
Code:
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)
Code:
mkfifo TMPIPE
sed -E "s#([^,]*)(,|$)#s/./M/\1\\
#g" positions | tee TMPIPE | sed -fTMPIPE file
rm TMPIPE

# 21  
Old 04-06-2017
Quote:
Originally Posted by RudiC
[..] @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)
Code:
mkfifo TMPIPE
sed -E "s#([^,]*)(,|$)#s/./M/\1\\
#g" positions | tee TMPIPE | sed -fTMPIPE file
rm TMPIPE

Correct, and also process substitution could be used, but just wanted to point out that the stdin placeholder - does not work with BSD sed or other regular sed version. It is also not part of POSIX so it is probably a GNU extension.
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