Home Man
Search
Today's Posts
Register

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

Tags
shell scripts

Login to Reply

 
Thread Tools Search this Thread
# 1  
Old 07-07-2011
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
# 2  
Old 07-07-2011
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

# 3  
Old 07-07-2011
Alternate awk..
Code:
awk -F'-' '{print /-/?$2"-":$0}' inputfile

# 4  
Old 07-07-2011
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

# 5  
Old 07-07-2011
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
# 6  
Old 07-07-2011
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
# 7  
Old 07-07-2011
Quote:
Originally Posted by itkamaraj
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 ?
Login to Reply

« Previous Thread | Next Thread »
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
Remove leading and trailing spaces from a file svajhala Shell Programming and Scripting 9 07-13-2017 04:56 PM
Remove the leading and trailing date from a CSV file shivamayam Shell Programming and Scripting 7 07-13-2017 12:12 PM
In a row, replace negative sign and find minimum value among four columns jacobs.smith Shell Programming and Scripting 5 02-20-2013 02:56 PM
Clearing leading and trailing blanks from a string in C Shell phudgens Shell Programming and Scripting 1 05-01-2012 05:44 PM
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
Strip leading and trailing spaces only in a shell variable with embedded spaces jerardfjay Shell Programming and Scripting 6 03-07-2005 01:24 PM
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 05:22 AM.

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