retain negative value through sed command


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting retain negative value through sed command
# 1  
Old 06-20-2011
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
# 2  
Old 06-21-2011
Try this:
Code:
% echo '(49,076), (45), (0,123)' | sed 's/(\([0-9][0-9]*,\{0,1\}[0-9]*\))/-\1/g' 
-49,076, -45, -0,123

# 3  
Old 06-21-2011
retain negative value through sed command

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


# 4  
Old 06-21-2011
How did you check this command? What sed do you have? It works for me:
Code:
% echo '"(49,076)" ,"(1,746)"' | sed --posix 's/(\([0-9][0-9]*,\{0,1\}[0-9]*\))/-\1/g' 
"-49,076" ,"-1,746"

# 5  
Old 06-21-2011
Hello Yazu,
I am giving the command as same as you
Code:
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
Code:
"(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

Last edited by Franklin52; 06-21-2011 at 03:04 AM.. Reason: Please use code tags for data and code samples
# 6  
Old 06-21-2011
Quote:
And also please see echo why we have that ' " ?
It's quoted for the shell.

I don't understand:
Code:
% 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.
# 7  
Old 06-21-2011
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!!!
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