Delete last characters in each column


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Delete last characters in each column
# 8  
Old 06-30-2013
Yes not really, but it does truly interpret the numbers as numbers, so it can add a leading zero in case of .00003263500000000000 for example, or remove a leading + sign or interpret scientific notation as opposed to pure text manipulation..
# 9  
Old 06-30-2013
Thanks for all the answers, it's really clarified and I got it working. Thank you. Smilie
# 10  
Old 06-30-2013
Quote:
Originally Posted by Scrutinizer
Yes not really, but it does truly interpret the numbers as numbers, so it can add a leading zero in case of .00003263500000000000 for example, or remove a leading + sign or interpret scientific notation as opposed to pure text manipulation..
Plus it does correct rounding instead of truncating (which was my first idea to look for it...)
# 11  
Old 06-30-2013
Quote:
Originally Posted by RudiC
Try
Code:
awk '{for (i=1; i<=NF; i++) sub(/...........$/,"",$i)}1' file

I was trying to set and use the OFMT variable (as you obviously want to modify long mantissae), but this did not work in my linux/mawk system - no idea why not!

Or, sed:
Code:
sed -r 's/.{11}( |$)/\1/g' file
-6.890809018 3.493488917 1.479883678 -2.297076354 -3.493423647 -4.437584732 -2.297076354 .000032635 -1.478850527 -6.890809018 .000032635 -4.437584732

The awk utility is treating the values in this file as strings until something is done that converts them to numeric values. If you want nine digits after the decimal point (with the values rounded instead of truncated), the following seems to work:
Code:
awk '{for (i=1; i<=NF; i++) $i += 0}1' CONVFMT="%.9f" OFMT="%.9f" file

If you are using a Solaris/SunOS system, use /usr/xpg4/bin/awk, /usr/xpg6/bin/awk, or nawk instead of /bin/awk or /usr/bin/awk.

PS Note that CONVFMT is used when converting the input string to a number, and OFMT is used when printing the converted number in the (implied) print statement. CONVFMT and OFMT both default to "%.6g". Since you want more than 6 significant digits after the decimal point in your output, you need to specify both to get the desired results.

Last edited by Don Cragun; 06-30-2013 at 07:00 PM.. Reason: Note reason for setting both CONVFMT and OFMT.
# 12  
Old 06-30-2013
@Don, I don't think that is accurate in this case because the implicit print statement is printing $0, which is a string, so OFMT does not come into play and CONVFMT should suffice.

Compare:
Code:
$ echo 1.23 | awk '{$1+=0; print $0}' OFMT="%.11f"
1.23
$ echo 1.23 | awk '{$1+=0; print $1}' OFMT="%.11f"
1.23000000000
$ echo 1.23 | awk '{$1+=0; print $0}' CONVFMT="%.11f"
1.23000000000
$ echo 1.23 | awk '{$1+=0; print $1}' CONVFMT="%.11f"
1.23

Also I think CONVFMT is used to convert a number to a string and not vice-versa..

Last edited by Scrutinizer; 06-30-2013 at 08:05 PM..
# 13  
Old 06-30-2013
Quote:
Originally Posted by Scrutinizer
@Don, I don't think that is accurate in this case because the implicit print statement is printing $0, which is a string, so OFMT does not come into play and CONVFMT should suffice.

Compare:
Code:
$ echo 1.23 | awk '{$1+=0; print $0}' OFMT="%.11f"
1.23
$ echo 1.23 | awk '{$1+=0; print $1}' OFMT="%.11f"
1.23000000000
$ echo 1.23 | awk '{$1+=0; print $0}' CONVFMT="%.11f"
1.23000000000
$ echo 1.23 | awk '{$1+=0; print $1}' CONVFMT="%.11f"
1.23

Also I think CONVFMT is used to convert a number to a string and not vice-versa..
Note the difference in results of the following two commands:
Code:
echo 1.2311111111111111111 | awk '{$1+=0; print}' OFMT="%.11f"
    produces:
1.23111
    and:
echo 1.2311111111111111111 | awk '{$1+=0; print}' OFMT="%.11f" CONVFMT="%.11f>"
    produces:
1.23111111111

CONVFMT is used to convert the input string to a number before $1 += 0 adds zero to it. The digits that are lost in that conversion won't magically be reconstructed when more digits after the decimal point are specified in OFMT. However, I readily admit that I don't understand why the 1st command only produces five digits after the decimal point in the output instead of having trailing 0's as needed to get 11 digits after the decimal point. (This example was run on OS X.)
# 14  
Old 06-30-2013
It seems to me $1+0 converts the string to a number and then CONVFMT is used to convert the number to a string to represent it in the record $0

Compare:
Code:
$ echo 1.2345678901234 | awk '{$1+=0; print $0}' CONVFMT="%.6f" OFMT="%.11f"
1.234568
$ echo 1.2345678901234 | awk '{$1+=0; print $1}' CONVFMT="%.6f" OFMT="%.11f"
1.23456789012
$ echo 1.2345678901234 | awk '{$1+=0; print $0}' CONVFMT="%.11f" OFMT="%.6f"
1.23456789012
$ echo 1.2345678901234 | awk '{$1+=0; print $1}' CONVFMT="%.11f" OFMT="%.6f"
1.234568

Code:
$ echo 1.2345678901234 | awk '{$1+=0; print $1}' CONVFMT="%.6f" OFMT="%.15f"
1.234567890123400


Last edited by Scrutinizer; 06-30-2013 at 08:44 PM..
This User Gave Thanks to Scrutinizer For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Delete special characters

My sed is not working on deleting the entire special characters and leaving what is necessary.grep connections_per a|sed -e 's/\<\!\-\-//g' INPUT: <!-- <connections_per_instance>1</connections_per_instance> --> <method>HALF</method> <!--... (10 Replies)
Discussion started by: kenshinhimura
10 Replies

2. Shell Programming and Scripting

Remove the first character from the fourth column only if the column has four characters

I have a file as follows ATOM 5181 N AMET K 406 12.440 6.552 25.691 0.50 7.37 N ATOM 5182 CA AMET K 406 13.685 5.798 25.578 0.50 5.87 C ATOM 5183 C AMET K 406 14.045 5.179 26.909 0.50 5.07 C ATOM 5184 O MET K... (14 Replies)
Discussion started by: hasanabdulla
14 Replies

3. Shell Programming and Scripting

delete first 2 characters for each line, please help

hi, ./R1_970330_210505.sard ./R1_970403_223412.sard ./R1_970626_115235.sard ./R1_970626_214344.sard ./R1_970716_234214.sard ... ... ... for these strings, i wanna remove the ./ for each line how can i do that? i know it could possibly be done by sed, but i really have not idea how... (4 Replies)
Discussion started by: sunnydanniel
4 Replies

4. Shell Programming and Scripting

Delete row if a a particular column has more then three characters in it

Hi i have a data like hw:dsfnsmdf:39843 chr2 76219829 51M atatata 51 872389 hw:dsfnsmdf:39853 chr2 76219839 51M65T atatata 51 872389 hw:dsfnsmdf:39863 chr2 76219849 51M atatata 51 872389 hw:dsfnsmdf:39873 chr2 ... (3 Replies)
Discussion started by: bhargavpbk88
3 Replies

5. Shell Programming and Scripting

need to Delete first 10 characters of a file name

Hello Everyone, I need help in deleting first 10 characters from the filename in a directory eg: 1234567890samplefile1.txt 1234567890samplefile2.txt and so on.. need to get the output as samplefile1.txt Thanks in Advance!!!! (8 Replies)
Discussion started by: Olivia
8 Replies

6. Shell Programming and Scripting

Delete characters from each line

Hi, I have a file that has data in the following manner, tt_0.00001.dat 123.000 tt_0.00002.dat 124.000 tt_0.00002.dat 125.000 This is consistent for all the entries in the file. I want to delete the 'tt_' and '.dat' from each line. Could anyone please guide me how to do this using awk or... (2 Replies)
Discussion started by: lost.identity
2 Replies

7. Shell Programming and Scripting

How to delete characters using a file

Hi All, I have a configuration file (file.cfg) in which data will be like this ; , _ + a to z A to Z Now i have to read a textfile (file.txt) and i need to check whether there is any other character present in text file that is not existing in (file.cfg). If other characters are present... (4 Replies)
Discussion started by: krishna_gnv
4 Replies

8. Shell Programming and Scripting

Delete not readable characters

Hi All, I wanted to delete all the unwanted characters in the string. ie, to delete all the characters which are not alpha numeric values. var1="a./bc" var2='abc/\."123' like to get the output as print var1 abc print var2 abc123 Could you guys help me out pls. Your help is... (3 Replies)
Discussion started by: ajilesh
3 Replies

9. UNIX for Dummies Questions & Answers

how to delete M-^M characters from a file

I am receiving a file with 'M-^M' characters...how do I get rid of these characters. I tried tr -d '\015' and sed '/^M//g', but they didnot work. Appreciate if someone can help me with this (1 Reply)
Discussion started by: hyennah
1 Replies

10. AIX

Delete specific characters

Hi every1 Well i have a list of numbers e.g 12304 13450 01234 00123 14567 what i want is a command to check if the number is starting from 0 and then delete the 0 without doing anything else!!!! any help wud b appreciated!!!!!!!!:( (4 Replies)
Discussion started by: masquerer
4 Replies
Login or Register to Ask a Question