Unix/Linux Go Back    


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

Shell Programming and Scripting


Closed    
 
Thread Tools Search this Thread Display Modes
    #1  
Old Unix and Linux 07-07-2011   -   Original Discussion by selvankj
selvankj's Unix or Linux Image
selvankj selvankj is offline
Registered User
 
Join Date: Jul 2011
Last Activity: 13 December 2017, 1:44 AM EST
Posts: 46
Thanks: 14
Thanked 1 Time in 1 Post
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 06:50 AM.. Reason: Please use code tags for code and data samples, thank you
Sponsored Links
    #2  
Old Unix and Linux 07-07-2011   -   Original Discussion by selvankj
itkamaraj's Unix or Linux Image
itkamaraj itkamaraj is offline Forum Advisor  
Registered User
 
Join Date: Apr 2010
Last Activity: 5 November 2017, 9:59 PM EST
Posts: 3,148
Thanks: 45
Thanked 698 Times in 674 Posts

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 Unix and Linux 07-07-2011   -   Original Discussion by selvankj
michaelrozar17's Unix or Linux Image
michaelrozar17 michaelrozar17 is offline
Registered User
 
Join Date: Jul 2010
Last Activity: 22 September 2016, 3:57 AM EDT
Location: IN
Posts: 894
Thanks: 15
Thanked 183 Times in 174 Posts
Alternate awk..

Code:
awk -F'-' '{print /-/?$2"-":$0}' inputfile

    #4  
Old Unix and Linux 07-07-2011   -   Original Discussion by selvankj
itkamaraj's Unix or Linux Image
itkamaraj itkamaraj is offline Forum Advisor  
Registered User
 
Join Date: Apr 2010
Last Activity: 5 November 2017, 9:59 PM EST
Posts: 3,148
Thanks: 45
Thanked 698 Times in 674 Posts

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 Unix and Linux 07-07-2011   -   Original Discussion by selvankj
selvankj's Unix or Linux Image
selvankj selvankj is offline
Registered User
 
Join Date: Jul 2011
Last Activity: 13 December 2017, 1:44 AM EST
Posts: 46
Thanks: 14
Thanked 1 Time in 1 Post
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 07:26 AM.. Reason: Please use code tags for code and data samples, thank you
Sponsored Links
    #6  
Old Unix and Linux 07-07-2011   -   Original Discussion by selvankj
divya bandipotu's Unix or Linux Image
divya bandipotu divya bandipotu is offline
Registered User
 
Join Date: Jun 2011
Last Activity: 11 February 2014, 5:54 AM EST
Posts: 35
Thanks: 16
Thanked 3 Times in 3 Posts
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 07:23 AM.. Reason: Please use code tags for code and data samples, thank you
Sponsored Links
    #7  
Old Unix and Linux 07-07-2011   -   Original Discussion by selvankj
selvankj's Unix or Linux Image
selvankj selvankj is offline
Registered User
 
Join Date: Jul 2011
Last Activity: 13 December 2017, 1:44 AM EST
Posts: 46
Thanks: 14
Thanked 1 Time in 1 Post
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
Closed

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Linux 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 12:47 PM
Removing leading and trailing spaces only in PERL kumar04 Shell Programming and Scripting 2 04-04-2009 04:17 PM
remove trailing and leading spaces using tr command sureshg_sampat Shell Programming and Scripting 5 03-04-2009 05:04 AM
Removing leading and trailing spaces of data between the tags in xml. jhmr7 UNIX for Dummies Questions & Answers 2 05-18-2005 11:27 AM
Leading and Trailing Spaces sleepster Shell Programming and Scripting 7 10-29-2003 11:48 PM



All times are GMT -4. The time now is 07:36 AM.