Hi ,
My input file contain :
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 :
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 :
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....
Hi,
If you want to keep the 8,10,16,17,22 positions value, expect output will:
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.
Hi ,
My input file contain :
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 :
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 :
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:
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???
RudiC
April 26, 2017, 5:11pm
4
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
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
rdrtx1
April 26, 2017, 6:33pm
5
awk '{for (i=3; i<=NF; i++) $i=(keep ~ ("," (i-2) ",")) ? $i : 0; print}' keep=,8,10,16,17,22, infile
input file contain :
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.
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.
Hi,
with perl:
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.
awk suggestion:
awk -v fields=8,10,16,17,22 -F' *= *|[ \t]*' '
BEGIN {
split(fields,F,/,/)
for(i in F)
C[F]
}
{
for(i=1; i<NF; i++)
if(!(i in C))
$(i+1)=0
sub(/ /,"=")
print
}
' file
Another way in (gnu) sed:
echo "8,10,16,17,22" | sed 's/,/;/g;s/[0-9]\+/s\/[0-9]\\+\/A\&A\/&/g' | sed -f - -e 's/^\|$/ /g;:B;s/\([^A]\)[0-9]\+\([^A]\)/\1A0A\2/;tB;s/A//g;s/^ \| $//g' file
Regards.