sed Command to replace particular value.


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting sed Command to replace particular value.
# 1  
Old 04-25-2017
sed Command to replace particular value.

Hi ,

My input file contain :
Code:
list = 3 14 15 10 9 11 12 18 19 20 21 22 23 24 25 26 6 1 2 3 4 5 7 8 16 17 27 28 30 29

Expected output :
Code:
list = 0 0 0 0 0 0 0 18 0 20 0 0 0 0 0 0 6 0 0 3 4 0 0 0 0 0 0 0 0 0

I want to keep the 8,10,16,17,22 value from the list and put 0 on rest of the positions.

command using :
Code:
set list=`3 14 15 10 9 11 12 18 19 20 21 22 23 24 25 26 6 1 2 3 4 5 7 8 16 17 27 28 30 29'
set new_list='0 0 0 0 0 0 0 18 0 20 0 0 0 0 0 0 6 0 0 3 4 0 0 0 0 0 0 0 0 0'
sed -i 's/'$list'/'$exp_list'/g'

but it is not working...

Thanks in advance....
# 2  
Old 04-25-2017
Hi,
If you want to keep the 8,10,16,17,22 positions value, expect output will:
Code:
list = 0 0 0 0 0 0 0 18 0 20 0 0 0 0 0 26 6 0 0 0 0 5 0 0 0 0 0 0 0 0

Why do you do these with sed ?
It will most easy to do with awk.

Regards.
# 3  
Old 04-25-2017
Quote:
Originally Posted by Preeti Chandra
Hi ,

My input file contain :
Code:
list = 3 14 15 10 9 11 12 18 19 20 21 22 23 24 25 26 6 1 2 3 4 5 7 8 16 17 27 28 30 29

Expected output :
Code:
list = 0 0 0 0 0 0 0 18 0 20 0 0 0 0 0 0 6 0 0 3 4 0 0 0 0 0 0 0 0 0

I want to keep the 8,10,16,17,22 value from the list and put 0 on rest of the positions.

command using :
Code:
set list=`3 14 15 10 9 11 12 18 19 20 21 22 23 24 25 26 6 1 2 3 4 5 7 8 16 17 27 28 30 29'
set new_list='0 0 0 0 0 0 0 18 0 20 0 0 0 0 0 0 6 0 0 3 4 0 0 0 0 0 0 0 0 0'
sed -i 's/'$list'/'$exp_list'/g'

but it is not working...

Thanks in advance....
Note that you can't use a backquote in your assignment to list; the opening quote type much either be a double-quote or a single-quote and must be the same type of quote as the ending quote.

And, you have to use the correct quoting in your sed command so the substitute command passed to it will be a single quoted string. And, you have to tell sed what file you want it to edit:
Code:
set list='3 14 15 10 9 11 12 18 19 20 21 22 23 24 25 26 6 1 2 3 4 5 7 8 16 17 27 28 30 29'
set new_list='0 0 0 0 0 0 0 18 0 20 0 0 0 0 0 0 6 0 0 3 4 0 0 0 0 0 0 0 0 0'
sed -i "s/$list/$exp_list/" pathname

Where pathname is the pathname of the file you want to edit.

But, note that this preserves the values of fields 8, 10, 17, 20, and 21; not 8, 10, 16, 17, and 22???
# 4  
Old 04-26-2017
Just for the fun of it - can't be done with sed alone; but, with list as defined above, 8,10,16,17,22 in a file called POS, and a recent bash, try

Code:
echo $list | sed 's/[0-9]\+/0/g' | sed -f<(echo $list | cut -d" " -f$(< POS) | tr ' ' '\n' | paste - <(tr ',' '\n' < POS) | tac | sed 's/^/s\/0\//; s/\t/\//')
0 0 0 0 0 0 0 18 0 20 0 0 0 0 0 26 6 0 0 0 0 5 0 0 0 0 0 0 0 0


Last edited by RudiC; 04-26-2017 at 07:07 PM..
# 5  
Old 04-26-2017
Code:
awk '{for (i=3; i<=NF; i++) $i=(keep ~ ("," (i-2) ",")) ? $i : 0; print}' keep=,8,10,16,17,22, infile

# 6  
Old 04-27-2017
input file contain :
Code:
list=1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1

expected output :
keep values at 8,10,16,17,22 and put 0 at all other places in the same file.
Code:
list=0 0 0 0 0 0 0 0 1 0 1 0 0 0 0 0 1 1 0 0 0 1 0 0 0 0 0 0 0 0

what command we can use for this changes?

previous post gives separate script command,but i want to change in the same input file.
# 7  
Old 04-29-2017
Hi,
with perl:
Code:
perl -pe 'BEGIN{%idx=map { $_ => 1 } (8,10,16,17,22)};print;@x=split / /,(split /=/)[1];$i=0;s/([0-9]+)/$idx{$i++} == 1 ? "$x[$i-1]":"0"/ge' file

And if ok, you can just use '-i'.

Regards.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

sed replace command

Hi. I need to append/prefix an & character to every 'single' & character (not when there are 2 or more grouped together) I find in a file. I can do it using this cmd: cat ${file} | sed -e 's/&/&&/g' > ${new_file} How can I modify this to ensure I only replace single &'s and not operate... (11 Replies)
Discussion started by: user052009
11 Replies

2. Shell Programming and Scripting

Need to log the sed command used to replace

In a shell script I am replacing the asterisks in a file: sed "s/\*/"0"/g" /home/download/$COMPANY_CODE/file_new > /home/download/$COMPANY_CODE/fileI need to log which positions were replaced & position(01:20) from the line it was replaced in. I am not sure how to do so. Also, instead of... (11 Replies)
Discussion started by: tomj5141
11 Replies

3. Shell Programming and Scripting

Find and replace using sed command

The content of the file filea.txt is as follows. --------- case $HOSTNAME in aaa) DS_PARM_VALUE_SET=vsDev APT_Configuration_File=/appl/infoserver/Server/Configurations/2node.apt ;; bbb) DS_PARM_VALUE_SET=vsQA... (3 Replies)
Discussion started by: kmanivan82
3 Replies

4. Shell Programming and Scripting

sed command to find and replace

Hello All, I need a sed command to find and replace below text in multiple files in a directory. Original Text :- "$SCRIPT_PATH/files" Replace with :- "$RESOURCE_FILE" Thank you in advance !!! Regards, Anand Shah (1 Reply)
Discussion started by: anand.shah
1 Replies

5. Shell Programming and Scripting

sed command to replace a word with new line and /

Hi, I have been trying to replace the key word "SQL> spool off " with "/ show errors" with out double quotes in all the files in a directory. above show erros should be displayed next line Could you please help me how to do that. I have tried something like this... (3 Replies)
Discussion started by: pointers
3 Replies

6. Shell Programming and Scripting

search and replace with sed command

hi, suggest me in the below script.. if In above I wanna replace "" with "]". (2 Replies)
Discussion started by: divya bandipotu
2 Replies

7. Shell Programming and Scripting

sed command to replace a character at last

Hi All, I have a file having one line only. It is like trapsess:inform|10.232.167.18|1|1|50|25|0|0|0|5|1|1|78|0037| I want to replace the numbers in last two columns by As. It should look like trapsess:inform|10.232.167.18|1|1|50|25|0|0|0|5|1|1|AA|AAAA| Please, suggest me any shell... (12 Replies)
Discussion started by: mukeshbaranwal
12 Replies

8. Shell Programming and Scripting

Replace with a variable in sed command

Hello, I have this command and it works fine. My question is that how can we replace the N by a variable, to print for instance a big number of lines. It means if I want 100 lines after an expression, to not put "N" 100 times in the sed. Code: $ sed -n '/aaa/{n;N;N;s///g;s/;/; /g;p;}'... (2 Replies)
Discussion started by: rany1
2 Replies

9. 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

10. Shell Programming and Scripting

sed search and replace command

Hi, I would like to seek help on how i can arrive on this result. sample.txt: product_code IN (param001) and product_type IN (param004) product_code IN (param002) and product_type IN (param005) product_code IN (param003) and product_type IN (param006) I would like to change the param001... (1 Reply)
Discussion started by: janzper
1 Replies
Login or Register to Ask a Question