Hi All,
I have problem to solve
aaaa,aaaaa,aaa,aaaa,aaa,aa ,aa
bbbb,bbbbbbbbb,bbbb,bbbbb ,bb
to
aaaa;aaaaa,aaa ;aaaa;aaa,aa ;aa
bbbb;bbbbbbbbb;bbbb;bbbbb ;bb
i try use sed to find and replace, but dont know how to replace specific column position.
can u help me??
thx for the comment.
echo "aaaa;aaaaa;aaa ;aaaa;aaa;aa ;aa"|awk -F ";" '{$2="bbbbbbbb"; printf "%s;%s;%s;%s;%s\n", $1,$2,$3,$4,$5}'
awk -F sets the delimiter for columns and then each column is represented by $1 $2 $3 $4 $5. Change them as you see fit.
what i'm concern is, replace all commas delimiter to semicolon except several column
e.g
i have a file contains two lines:
aaaa,aaa,aa,aaaa,aaaa aa
cccc,ccccc ,cccc,cccc,cc
and i want change it into
aaaa;aaa,aa;aaaa;aaaa aa
cccc;ccccc ;cccc;cccc,cc
*sorry my english not well enough
echo "aaaa,aaa,aa,aaaa,aaaa aa"|sed -e 's/a/c/g' | awk -F "," '{printf "%s;%s%s;%s;%s,%s\n", $1,$2,$3,$4,substr($5,0,4),substr($5,6,2)}'
I hope you can understand the code. Like I said with the code I already had gave you it was possible to change any field with appropriate tweaking.
redoubtable:
echo "aaaa,aaa,aa,aaaa,aaaa aa"|sed -e 's/a/c/g' | awk -F "," '{printf "%s;%s%s;%s;%s,%s\n", $1,$2,$3,$4,substr($5,0,4),substr($5,6,2)}'
I hope you can understand the code. Like I said with the code I already had gave you it was possible to change any field with appropriate tweaking.
not change the 'a' letter to 'c' like u mention above,.
and i modify to
echo "aaaa,aaa,aa,aaaa,aaaa aa" | awk -F "," '{printf "%s;%s%s;%s;%s,%s\n", $1,$2,$3,$4,substr($5,0,4),substr($5,6,2)}'
from ur code, it will
change
aaaa,aaa,aa,aaaa,aaaa aa
into
aaaa;aaaaa;aaaa;aaaa,aa
the result line length reduced,... it should be
aaaa;aaa,aa;aaaa;aaaa aa
Just add a space in printf format strings:
echo "aaaa,aaa,aa,aaaa,aaaa aa" | awk -F "," '{printf "%s;%s%s ;%s;%s,%s\n", $1,$2,$3,$4,substr($5,0,4),substr($5,6,2)}'
in my input sample
aaaa,aaa,aa,aaaa,aaaa aa
i just want to change commas in position 5,12,17 into semicolon..
no change in data
i try to make an array and make a script, see below
set -A dlm -- 5 12 17
echo ${#dlm []}
inc=0
echo "cat FILENAME | sed \ " > ascript
while [ $inc -lt ${#dlm [ ]} ] ; do
cmd=" -e '/^\(.\{${dlm[$inc]}\}\),\(.*\)/ s//\1\;\2/g' \ "
echo $cmd >> ascript
let inc=inc+1
done
chmod 775 ascript
ascript
but still problem occurs,
is there somtin wrong with my script ??
To replace the comma with a semicolon:
sed '/,/;/g' file
Regards
but the result not match with requirement,.
the script u gave same like i use
tr ',' ';'
is there any clues to change commas in specified position within a line??
Well,.
I came with this solution, but litle bit messy i think
set -A dlm -- 5 12 17
inc=0
while [ $inc -lt ${#dlm
[*]} ] ; do
cmd="$cmd s/^(.{${dlm[$inc]}}),(.*)/\1;\2/g ;"
let inc=inc+1
done
perl -pe "${cmd}" FILENAME
Another way:
awk '{print substr($0,1,4)";"substr($0,6,6)";"substr($0,13,4)";"substr($0,18)}'
Regards