Shell Programming and Scripting

BSD, Linux, and UNIX shell scripting — Post awk, bash, csh, ksh, perl, php, python, sed, sh, shell scripts, and other shell scripting languages questions here.

Help to move leading negative sign to trailing position

👤 Login to reply

    #1  
Old 07-07-2011
selvankj selvankj is offline
Registered User
 
Help to move leading negative sign to trailing position

Hi All,

I am having a file which contains negative numbers, wht i am doing is re-formattting the file(moving few columns and add few hard codings between), while reformatting i would want the negative numbers to have the sign as trailing rather than leading.

Existing
Code:
-2400.00
34
0.00
-987.00
-92.56

Required
Code:
2400.00-
34
0.00
987.00-
92.56-

FYI am using awk on every line to reformat the line to create a new format
Code:
cat $FILENAME |grep ^3 | while read line
do
  echo "$line" | nawk ' BEGIN {FS=","}  {print("5",$1,$28,"$27",$26)}' >> New_file
done


Last edited by Franklin52; 07-07-2011 at 05:50 AM.. Reason: Please use code tags for code and data samples, thank you
Sponsored Links
    #2  
Old 07-07-2011
itkamaraj's Unix or Linux Image
itkamaraj itkamaraj is offline Forum Advisor  
Registered User
 
Code:
 
$ nawk '{ if(index($0,"-") > 0){print substr($0,2,length($0))"-"} else {print $0}}' test
2400.00-
34
0.00
987.00-
92.56-

$ cat test
-2400.00
34
0.00
-987.00
-92.56

Sponsored Links
    #3  
Old 07-07-2011
michaelrozar17's Unix or Linux Image
michaelrozar17 michaelrozar17 is offline
Registered User
 
Alternate awk..
Code:
awk -F'-' '{print /-/?$2"-":$0}' inputfile

    #4  
Old 07-07-2011
itkamaraj's Unix or Linux Image
itkamaraj itkamaraj is offline Forum Advisor  
Registered User
 
Code:
 
cat $FILENAME |grep ^3 | while read line
do
echo "$line" | nawk ' BEGIN {FS=","} {print("5",$1,$28,"$27",$26)}' >> New_file
done

use this... ( unnecessary of cat, while, echo )

Code:
nawk ' /^3/ BEGIN {FS=","} {print("5",$1,$28,"$27",$26)}' $FILENAME >> New_file

Sponsored Links
    #5  
Old 07-07-2011
selvankj selvankj is offline
Registered User
 
Thank you ITKAMARAJ..
I am actually going to use printf to use certain formatting(like padding with zero etc)

Code:
nawk '{ if(index($0,"-") > 0){print substr($0,2,length($0))"-"} else {print $0}}' test

the above worked perfectly fine ... in that case i will have to first use this to the file(to conver the column with trailing sign and then use my other formatting ? or can i include the above to this statement
Code:
nawk ' /^3/ BEGIN {FS=","} {print("5",$1,$28,"$27",$26)}' $FILENAME

?

Last edited by Franklin52; 07-07-2011 at 06:26 AM.. Reason: Please use code tags for code and data samples, thank you
Sponsored Links
    #6  
Old 07-07-2011
divya bandipotu's Unix or Linux Image
divya bandipotu divya bandipotu is offline
Registered User
 
cat 9temp
Code:
-2400.00
34
0.00
-987.00
-92.56


With the help of sed:
Code:
cat 9temp | sed 's/^-\(.*\)/\1\-/'
2400.00-
34
0.00
987.00-
92.56-


Last edited by Franklin52; 07-07-2011 at 06:23 AM.. Reason: Please use code tags for code and data samples, thank you
Sponsored Links
    #7  
Old 07-07-2011
selvankj selvankj is offline
Registered User
 
Quote:
Originally Posted by itkamaraj View Post
Code:
 
$ nawk '{ if(index($0,"-") > 0){print substr($0,2,length($0))"-"} else {print $0}}' test
2400.00-
34
0.00
987.00-
92.56-
 
$ cat test
-2400.00
34
0.00
-987.00
-92.56

if the above column is other than 1st ..like if the column containg the value is 13th column ?
Sponsored Links
👤 Login to reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Finding the most positive and negative value and defining its position gisele_l Shell Programming and Scripting 7 04-20-2010 11:47 AM
Removing leading and trailing spaces only in PERL kumar04 Shell Programming and Scripting 2 04-04-2009 03:17 PM
remove trailing and leading spaces using tr command sureshg_sampat Shell Programming and Scripting 5 03-04-2009 04:04 AM
Removing leading and trailing spaces of data between the tags in xml. jhmr7 UNIX for Dummies Questions & Answers 2 05-18-2005 10:27 AM
Leading and Trailing Spaces sleepster Shell Programming and Scripting 7 10-29-2003 10:48 PM



All times are GMT -4. The time now is 01:45 PM.

Unix & Linux Forums Content Copyright©1993-2018. All Rights Reserved.
×
UNIX.COM Login
Username:
Password:  
Show Password





Not a Forum Member?
Forgot Password?