retain negative value through sed command

Hi Experts ,

We have a reuirement that we have to remove multiple comma's within double quotes in the columns as we have already taken care through our sed command .

Here is my code :-

sed -e ':a' -e 's/\("[^"][^"]*\),\([^"][^"]*"\)/\1\2/;ta' $Orign_File > $dummy
sed 's/"//g' $dummy > $Process_file

We have four amount columns that the value comes in negative in Excel it looks like -49,076 .If it is normal negative my script will handle .but the values are formatted in such a way when we look in text pad it looks like �(49,076)� .Can we handle this brackets and retain my negative value , please help in this resolution as i have target date over.

Thanks for your help in advance

Try this:

% echo '(49,076), (45), (0,123)' | sed 's/(\([0-9][0-9]*,\{0,1\}[0-9]*\))/-\1/g' 
-49,076, -45, -0,123

Hi Yazu/experts,

Thanks for your quick reply , This command woll not work..

I had given values like this
"(49,076)" ,"(1,746)"

but your below command will not work for this
sed 's/(\([0-9][0-9]*,\{0,1\}[0-9]*\))/-\1/g'

Can you please help me out to solve this issue..

How did you check this command? What sed do you have? It works for me:

% echo '"(49,076)" ,"(1,746)"' | sed --posix 's/(\([0-9][0-9]*,\{0,1\}[0-9]*\))/-\1/g' 
"-49,076" ,"-1,746"

Hello Yazu,
I am giving the command as same as you

sed 's/(\([0-9][0-9]*,\{0,1\}[0-9]*\))/-\1/g'  file name

and the file name is minus and the values are given like this

"(49,067)","(1,746)"

And also please see echo why we have that ' " ? its only embeded in double quotes,its comma delimited file
thanks in advance

It's quoted for the shell.

I don't understand:

% cat filename
"(49,076)" ,"(1,746)"

% sed 's/(\([0-9][0-9]*,\{0,1\}[0-9]*\))/-\1/g' filename
"-49,076" ,"-1,746"

Give an example of the real input.

Hi Yazu ,

I appologise ,it works for me ..Sorry for the confusion..
quick question.., I have wrriten two other sed commands..plus our command ,how do i put all this in loop to check for all this and do the work...

first command replaces multiple comma's
second command replaces double quotes
third command that is your command to retain negative values

sed -e ':a' -e 's/\("[^"][^"]*\),\([^"][^"]"\)/\1\2/;ta'
sed 's/"//g'
sed 's/(\([0-9][0-9]*,\{0,1\}[0-9]
\))/-\1/g'

Thanks Yazu , you solved my 95 percent of the work!!!

You can unite all commands in one sed invocation like this (with every command on a separate line):

sed ':a s/\("[^"][^"]*\),\([^"][^"]*"\)/\1\2/;ta
s/"//g
s/(\([0-9][0-9]*,\{0,1\}[0-9]*\))/-\1/g'

Hi Yazu

Thanks for your valuable input..I agve the sed command like this below

sed -e 's/\("[^,]*\)[,]\([^"]"\)/\1\2/g' $Orign_File | sed -e ':a' -e 's/\("[^"][^"]*\),\([^"][^"]"\)/\1\2/;ta' | sed 's/"//g' | sed 's/(\([0-9][0-9]*,\{0,1\}[0-9]*\))/-\1/g' > $Process_file

Its working for our scenerio, i will test the original file and get back to you in case of queries..

Thank you
much apprciated
Babu

Hi Yazu/Experts

Any help would be much appreciated!!!

Yazu , some how i could unite all the sed commands like this below

sed -e 's/\("[^,]*\)[,]\([^"]"\)/\1\2/g' $Orign_File | sed -e ':a' -e 's/\("[^"][^"]*\),\([^"][^"]"\)/\1\2/;ta' | sed 's/"//g' | sed 's/(\([0-9][0-9]*,\{0,1\}[0-9]*\))/-\1/g' > $Process_file

It works for some records in file but it rejects for some records in file.
I have attached two files in which Test.txt will work fine with the above sed command,but Sample.txt will reject the records.
No idea why its rejecting..
Can any one please help on this..
Thanks in Advance

# echo '"(49,076)" ,"(1,746)"'
"(49,076)" ,"(1,746)"
# echo '"(49,076)" ,"(1,746)"' | sed 's/(/-/g;s/)//g'
"-49,076" ,"-1,746"

Hi All

I have input like this "(49,076.25)" ,"(100,74,60)",-4,620.55
I need the otput as -49076.25,1007460,-4620.55

I used the below sed command..
sed -e ':a' -e 's/\("[^"][^"]*\),\([^"][^"]"\)/\1\2/;ta' test | sed -e 's/\("[^,]*\)[,]\([^"]"\)/\1\2/g' | sed 's/"//g' | sed 's/(\([0-9.][0-9.]*,\{0,1\}[0-9.]*\))/-\1/g' > new

I am getting almost 95 percent output as -49076.25,1007460,-4,620.55
But if you can see -4,620.55 the comma is not been removed

Please help on this

you dont need to save any reference so can try just sed substitute string :slight_smile:

# a='"(49,076.25)" ,"(100,74,60)",-4,620.55'
# echo "$a"|sed 's/,\|["()]//g;s/-/,&/;s/^/-/;s/ /,/'
-49076.25,1007460,-4620.55

regards
ygemici

[quote=bshivali;302533846]
Hi All

I have input like this "(49,076.25)" ,"(100,74,60)",-4,620.55
I need the otput as -49076.25,1007460,-4620.55

I used the below sed command..
sed -e ':a' -e 's/\("[^"][^"]*\),\([^"][^"]"\)/\1\2/;ta' test | sed -e 's/\("[^,]*\)[,]\([^"]"\)/\1\2/g' | sed 's/"//g' | sed 's/(\([0-9.][0-9.]*,\{0,1\}[0-9.]*\))/-\1/g' > new

I am getting almost 95 percent output as -49076.25,1007460,-4,620.55
But if you can see -4,620.55 the comma is not been removed

its only "(49,076.25)" ,"(100,74,60)",-4,620.55
previous quote by y gemici gives me null command on excution

i have concatenated like this

sed -e 's/\("[^,]*\)[,]\([^"]"\)/\1\2/g' test | sed -e ':a' -e 's/\("[^"][^"]*\),\([^"][^"]"\)/\1\2/;ta' | sed 's/,\|["()]//g;s/-/,&/;s/^/-/;s/ /,/'
> new

thanks in advance

[quote="bshivali,post:14,topic:290140"]

where is null?

# sed 's/,\|["()]//g;s/-/,&/;s/^/-/;s/ /,/'

does this gets null? i dont think so..
which your sed version? and your platform?

if you want to use reference although easy sed command then you can try this :wink:

# sed 's/"(\([^ ]*\),\([^ ]*\))" *,"(\([^ ]*\),\([^ ]*\),\(.*\))",\(.*\),\(.*\)/-\1\2,\3\4\5,\6\7/' file
-49076.25,1007460,-4620.55

regards
ygemici