Changing integer columns to floating decimal columns


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Changing integer columns to floating decimal columns
# 1  
Old 01-29-2014
Changing integer columns to floating decimal columns

I have a document that has 7 columns.

eg.

Code:
$1      $2     $3     $4    $5        $6       $7
string string string string integer integer integer

The 6th and 7th columns are a mix of integers and floating decimals (with 4 decimal places). I need to convert the last 2 columns so that all values are floating decimals w/4 decimal places, while leaving the rest of the worksheet in tact. In all I have 96 documents that need this conversion.

I have been able to output an individual column to a new worksheet in the right format with:

Code:
awk <input_filename ' { printf ("%.4f\n",$7) }' > output filename

However, I don't want to make 2 separate files and concatenate the original, plus the 2 new files and then awk out the columns I need on 96 files.

Thanks

Last edited by Scott; 01-29-2014 at 03:33 PM.. Reason: Use code tags, please...
# 2  
Old 01-29-2014
If you don't mind the reformatting of the whitespace to a one-space:
Code:
awk <input_filename '{ $6=sprintf ("%.4f",$6); $7=sprintf ("%.4f",$7); print }' > output filename

# 3  
Old 01-29-2014
Code:
awk '{ for(N=6; N<=7; N++) $N=sprintf("%.4f", $N); print > FILENAME".new" }' OFS="\t" file1 file2 file3 file4 file5

This should make file1.new, file2.new, file3.new, file4.new, file5.new. You can't just overwrite your input while you're using it, and you shouldn't destroy your input files until verifying the output is what you want.
# 4  
Old 01-29-2014
That works perfectly! Thanks so much Smilie
This User Gave Thanks to kadm For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

How to trim the decimal place for all the columns?

Dear all, I have a file call test.txt which has 2000 columns, 1000 rows. If I want to trim all the columns to 3 decimal places, how can I do it? I know how to use awk prinf to trim specic columns. But I don't know how to trim all the columns. Thank you. data sample: 0.976004565 9.34567845... (6 Replies)
Discussion started by: forevertl
6 Replies

2. Shell Programming and Scripting

Compare 2 csv files by columns, then extract certain columns of matcing rows

Hi all, I'm pretty much a newbie to UNIX. I would appreciate any help with UNIX coding on comparing two large csv files (greater than 10 GB in size), and output a file with matching columns. I want to compare file1 and file2 by 'id' and 'chain' columns, then extract exact matching rows'... (5 Replies)
Discussion started by: bkane3
5 Replies

3. Shell Programming and Scripting

Trouble converting columns of integers to floating decimals

I have a document that has 7 columns. eg. $1 $2 $3 $4 $5 $6 $7 string string string string integer integer integer The 6th and 7th columns are a mix of integers and floating decimals (with 4 decimal places). I need to convert the last 2 columns so that all values are floating decimals w/4... (1 Reply)
Discussion started by: kadm
1 Replies

4. UNIX for Dummies Questions & Answers

awk to add/subtract an integer to/from each entry in columns?

---------- Post updated at 01:58 PM ---------- Previous update was at 01:48 PM ---------- For some reason my question is not getting printed. Here are the details: Greetings. I would like to add/subtact an integer to/from two columns of integers. I feel like this should be easy using awk... (3 Replies)
Discussion started by: Twinklefingers
3 Replies

5. Shell Programming and Scripting

Deleting all the fields(columns) from a .csv file if all rows in that columns are blanks

Hi Friends, I have come across some files where some of the columns don not have data. Key, Data1,Data2,Data3,Data4,Data5 A,5,6,,10,, A,3,4,,3,, B,1,,4,5,, B,2,,3,4,, If we see the above data on Data5 column do not have any row got filled. So remove only that column(Here Data5) and... (4 Replies)
Discussion started by: ks_reddy
4 Replies

6. Shell Programming and Scripting

Combine columns from many files but keep them aligned in columns-shorter left column issue

Hello everyone, I searched the forum looking for answers to this but I could not pinpoint exactly what I need as I keep having trouble. I have many files each having two columns and hundreds of rows. first column is a string (can have many words) and the second column is a number.The files are... (5 Replies)
Discussion started by: isildur1234
5 Replies

7. Shell Programming and Scripting

Single command for add 2 columns and remove 2 columns in unix/performance tuning

Hi all, I have created a script which adding two columns and removing two columns for all files. Filename: Cust_information_1200_201010.txt Source Data: "1","Cust information","123","106001","street","1-203 high street" "1","Cust information","124","105001","street","1-203 high street" ... (0 Replies)
Discussion started by: onesuri
0 Replies

8. Shell Programming and Scripting

Changing file with rows and columns into just 1 column

Hi I need a bash shell script that will take text files with 4 rows and different numbers of columns in each row and convert each one into a text file with just one column. I then subtract 1.5 from each number in the column (I have that part already) The next step after that is I want to have... (10 Replies)
Discussion started by: ac130pilot
10 Replies

9. UNIX for Dummies Questions & Answers

Changing Sequence of Columns

Hello, I might require some help here. I have a file, ITEMS with the below data : A1 20 A2 A3 B1 40 B2 B3 C1 70 C2 C3 D1 90 D2 D3 I need the output as : A2 A3 20 B2 B3 40 C2 C3 70 D2 D3 90 i.e, I need the sequence of columns to be changed and only 3 fields to be... (5 Replies)
Discussion started by: sushant172
5 Replies

10. UNIX for Dummies Questions & Answers

Changing the order of columns in a script

I have the following script: (echo -n `curl http://www.example.com/scores.txt | grep mylocation`; date +%Y%m%d%H%M%S) >> myscores.txt This script works fine, except that it places the timestamp at the end of the file myscores.txt. How do add the timestamp as the first column and then a tab and... (4 Replies)
Discussion started by: figaro
4 Replies
Login or Register to Ask a Question