change value to negative


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting change value to negative
# 1  
Old 09-13-2011
change value to negative

How to change value in column 5 to negative based on value in column 2 ?
Example: For all records with A in column 2 change value in column 5 to negative.

file
Code:
1234~A~b~c~10~e~f~g~h~09/10/09
1234~A~b~c~75~e~f~g~h~11/12/10
1234~A~b~c~40~e~f~g~h~12/06/10
5678~B~b~c~2~e~f~g~h~01/11/11
5678~B~b~c~6~e~f~g~h~03/28/13
8910~A~b~c~80~e~f~g~h~07/29/11
1023~B~b~c~15~e~f~g~h~10/22/09
1023~B~b~c~22~e~f~g~h~04/22/14

desired output
Code:
1234~A~b~c~-10~e~f~g~h~09/10/09
1234~A~b~c~-75~e~f~g~h~11/12/10
1234~A~b~c~-40~e~f~g~h~12/06/10
5678~B~b~c~2~e~f~g~h~01/11/11
5678~B~b~c~6~e~f~g~h~03/28/13
8910~A~b~c~-80~e~f~g~h~07/29/11
1023~B~b~c~15~e~f~g~h~10/22/09
1023~B~b~c~22~e~f~g~h~04/22/14
# 2  
Old 09-13-2011
Code:
awk -F '~' ' BEGIN {OFS="~"}
          {
          if($2=="A") 
             { $5*=-1}
          print $0
          }  ' inputfile  > outputfile

# 3  
Old 09-13-2011
Hi,

Using Perl:
Code:
$ cat infile
1234~A~b~c~10~e~f~g~h~09/10/09
1234~A~b~c~75~e~f~g~h~11/12/10
1234~A~b~c~40~e~f~g~h~12/06/10
5678~B~b~c~2~e~f~g~h~01/11/11
5678~B~b~c~6~e~f~g~h~03/28/13
8910~A~b~c~80~e~f~g~h~07/29/11
1023~B~b~c~15~e~f~g~h~10/22/09
1023~B~b~c~22~e~f~g~h~04/22/14
$ perl -F"~" -lane 'printf "%s\n", join "~", @F[0..3], $F[1] eq "A" ? $F[4]*-1 : $F[4], @F[5..$#F]' infile 
1234~A~b~c~-10~e~f~g~h~09/10/09
1234~A~b~c~-75~e~f~g~h~11/12/10
1234~A~b~c~-40~e~f~g~h~12/06/10
5678~B~b~c~2~e~f~g~h~01/11/11
5678~B~b~c~6~e~f~g~h~03/28/13
8910~A~b~c~-80~e~f~g~h~07/29/11
1023~B~b~c~15~e~f~g~h~10/22/09
1023~B~b~c~22~e~f~g~h~04/22/14

Regards,
Birei
# 4  
Old 09-13-2011
for the awk code
Thanks for quick reply
2 issues:

1)It made 0 into -0 which create and issue to process file
2) It is rounding the numbers. Can we keep the number or round to 2 decimals at least?

that is all

thanks again
# 5  
Old 09-13-2011
Code:
kent$  cat a.tmp
1023~B~b~c~22~e~f~g~h~04/22/14
8910~A~b~c~0~e~f~g~h~07/29/11
8910~A~b~c~10.23448~e~f~g~h~07/29/11



kent$  awk -F'~' 'BEGIN{OFS="~"}$2=="A"{$5=($5==0)?$5:"-"$5}1' a.tmp
1023~B~b~c~22~e~f~g~h~04/22/14
8910~A~b~c~0~e~f~g~h~07/29/11
8910~A~b~c~-10.23448~e~f~g~h~07/29/11

Or: [note, this rounds the numbers]

kent$  awk -F'~' 'BEGIN{OFS="~"}$2=="A"{$5=-$5}1' a.tmp                                                                                  
1023~B~b~c~22~e~f~g~h~04/22/14
8910~A~b~c~0~e~f~g~h~07/29/11
8910~A~b~c~-10.2345~e~f~g~h~07/29/11

This User Gave Thanks to sk1418 For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Post Here to Contact Site Administrators and Moderators

Bits in Negative

Hi All, I have received a notification that I have posted a question double times. But I have not done all this intentionally. I have just joined this site and was not aware of the rules. Also I have my bits in negative. what does that mean. Thanks (1 Reply)
Discussion started by: Palak Sharma
1 Replies

2. Shell Programming and Scripting

Negative testing in unix

how can we do negative testing in unix ? (2 Replies)
Discussion started by: eshalife
2 Replies

3. UNIX for Dummies Questions & Answers

Negative testing in Unix

How to do Negative testing in Unix ?? Please reply asap. Regards Esha (0 Replies)
Discussion started by: eshalife
0 Replies

4. Shell Programming and Scripting

Find Where Values Change From Positive To Negative and viceversa

Hi all, I have a file that looks like shown below. I want to find places where the value in column 2 change from negative to positive and vice versa and return the value on column 1 at that point. I wonder if this is possible in shell script or awk .. please help! Here is the original data ... (6 Replies)
Discussion started by: malandisa
6 Replies

5. Shell Programming and Scripting

PCRE negative lookahead

I have read many tutorials and cannot get this to work. I need to use pcre (because that is what the library in the software we are using uses) and pcregrep everything except /home from the /etc/fstab pcregrep '(?!/home)' /etc/fstab It returns the entire fstab (This is on a RHEL5... (1 Reply)
Discussion started by: insania
1 Replies

6. Shell Programming and Scripting

find negative search

I have a config file as below. This file is delimited by ; First field is the starting directory for find command. Second field is -mtime value for the find command. Third field is combination of folder and file delimited by | /home/export/temp;+30;file1|dir1|file2... (3 Replies)
Discussion started by: cmoulee
3 Replies

7. SCO

Help with negative inodes on sco 6

Hello. I just installed a SCO Openserver 6 box and it's suckin' mud. sar -v (see below) shows something that has me quite concerned... after time, it shows that the number of inodes being used as a negative value. When this happens, the server runs extremely slow until I reboot. The server... (0 Replies)
Discussion started by: debtman3535
0 Replies

8. Programming

Negative Offset

Function: int fcntl(int fd, int cmd, struct flock * lock) Data Type: struct flock This structure is used with the fcntl function to describe a file lock. It has these members: off_t l_start This specifies the offset of the start of the region to which the lock applies, and... (1 Reply)
Discussion started by: DNAx86
1 Replies

9. UNIX for Dummies Questions & Answers

negative permissions

Hi. I want to know how can I negate a write permission for a file to an expecific user when that user have that permission becouse he belongs to a group what have a write permission for the file. (4 Replies)
Discussion started by: sickoboy
4 Replies
Login or Register to Ask a Question