retain negative value through sed command


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting retain negative value through sed command
# 8  
Old 06-21-2011
You can unite all commands in one sed invocation like this (with every command on a separate line):

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

# 9  
Old 06-21-2011
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
# 10  
Old 06-24-2011
Sed command works for some records but not all records in comma delimited csv file

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
# 11  
Old 06-24-2011
Code:
# echo '"(49,076)" ,"(1,746)"'
"(49,076)" ,"(1,746)"
# echo '"(49,076)" ,"(1,746)"' | sed 's/(/-/g;s/)//g'
"-49,076" ,"-1,746"

# 12  
Old 06-24-2011
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
# 13  
Old 06-25-2011
you dont need to save any reference so can try just sed substitute string Smilie
Code:
# a='"(49,076.25)" ,"(100,74,60)",-4,620.55'

Code:
# echo "$a"|sed 's/,\|["()]//g;s/-/,&/;s/^/-/;s/ /,/'
-49076.25,1007460,-4620.55

regards
ygemici
# 14  
Old 06-25-2011
uregent help needed on this

[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
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Sendmail K command regex: adding exclusion/negative lookahead to regex -a@MATCH

I'm trying to get some exclusions into our sendmail regular expression for the K command. The following configuration & regex works: LOCAL_CONFIG # Kcheckaddress regex -a@MATCH +<@+?\.++?\.(us|info|to|br|bid|cn|ru) LOCAL_RULESETS SLocal_check_mail # check address against various regex... (0 Replies)
Discussion started by: RobbieTheK
0 Replies

2. UNIX for Dummies Questions & Answers

Sed/awk to find negative numbers and replace with 1?

Greetings. I have a three column file, and there are some numbers in the second column that are <1. However I need all numbers to be positive, thus need to replace all those numbers with just one. I feel like there must be a simple way to use awk to find these numbers and sed to replace but can't... (5 Replies)
Discussion started by: Twinklefingers
5 Replies

3. Shell Programming and Scripting

Delete and retain some characters

Ive been trying to google and tried sed and awk. BUt still getting no exact formula. I would like to know how to parse this at: From: Compute Machin Appliance 3.2.9.10000 123456 To: Compute Machin Appliance 3.2.9.123456 (5 Replies)
Discussion started by: kenshinhimura
5 Replies

4. UNIX for Advanced & Expert Users

retain ownership

Hi, I have a script which transfers files/directories from one HP unix to another HP unix server using SCP but i need to retain ownership of files/folders same as source server my script is as follows cd /sasdata/TR_CNTO328/C0328T07/Dry_Run_1/Macros find . -type d -newer . -exec scp -pr {}... (6 Replies)
Discussion started by: tushar_spatil
6 Replies

5. Shell Programming and Scripting

how to retain zero

Hi everyone i have simple mathematical problem bash> echo date +%m bash> 03 bash> month=`date +%m` bash> lmonth=$month-1 bash> echo $lmonth bash>2 it gives me two but i want the answer of 02 please help me how to do this (6 Replies)
Discussion started by: aishsimplesweet
6 Replies

6. Shell Programming and Scripting

Need to retain particular rows

Hi All, I am in need of help to retain particular rows. I have a large file with 5 columns. Column 3 has signs like “=” “>” “<” and column 5 has values “nm” “%” or empty space. I need to keep only rows which have “=” in column 3 and “nm” in column 5. How can I do it in awk? Any help is greatly... (3 Replies)
Discussion started by: newpro
3 Replies

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

8. Shell Programming and Scripting

mv command to rename multiple files that retain some portion of the original file nam

Well the title is not too good, so I will explain. I need to move (rename) files using a simple AIX script. ???file1.txt ???file2.txt ???file1a.txt ???file2a.txt to be: ???renamedfile1'date'.txt ???renamedfile2'date'.txt ???renamedfile1a'date'.txt ???renamedfile2a'date'.txt ... (4 Replies)
Discussion started by: grimace15
4 Replies

9. Shell Programming and Scripting

Problem with sed and negative regexpr's

I have a file with various syntax that I'm try to run sed on to change over a specific regexp and having problems. I want to swap all instances of a token so long as the token isn't followed by either an alphanumeric character, an underscore, or an exclamation point. The test file looks like... (1 Reply)
Discussion started by: ericbmn1
1 Replies

10. Shell Programming and Scripting

how to retain leading zeros

Hi All, I am working with a fixed width file Forrmat. C1 Number (10,3) C2 Number (10,3) e.g. c1= 0000000100.000 c2= 0000000020.000 0000000100.0000000000020.000 I have to perform c1 - c2 . i.e. I want answer to be 0000000080.000. but I am loosing the leading zeros( only getting... (3 Replies)
Discussion started by: Manish Jha
3 Replies
Login or Register to Ask a Question