Replace all decimal values in a column


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Replace all decimal values in a column
# 1  
Old 03-03-2015
Replace all decimal values in a column

Hi
My input file looks

Code:
String000002  GeneWise        CW     48945   49354   .       -       0       Pt=PEQU_00004;
String000002  LEN   NA    52125   52604   0.945751        -       .       PID=PEQU_00005;lvid_id=PEQ_28708;
String000002  LEN   CW     52125   52604   .       -       0       Pt=PEQU_00005;
String000002  WEise        NA    66200   66667   45.48   -       .       PID=PEQU_00006;lvid_id=Os03t0797100-00-D1363;Shift=0;
String000002  WEise        CW     66200   66667   .       -       0       Pt=PEQU_00006;
String000002  GUST        NA    90829   91128   0.21    +       .       PID=PEQU_00007;lvid_id=A00088;
String000002  GUST        CW     90829   91128   0.21    +       0       Pt=PEQU_00007;
String000002  LEN   NA    104627  107284  0.499954        -       .       PID=PEQU_00008;lvid_id=PEQ_36749;
String000002  LEN   CW     104627  105584  .       -       1       Pt=PEQU_00008;

I want to replace all my 6th column entries to .

My original file has decimal values in column 6. It also has . as 6th column entries. I just want to replace all 6th column entries to .
I don't want to change the format or spacing of the file Need to retain the file spacing and format as it is.
My required output file is :

Code:
String000002  GeneWise        CW     48945   49354   .       -       0       Pt=PEQU_00004;
String000002  LEN   NA    52125   52604   .        -       .       PID=PEQU_00005;lvid_id=PEQ_28708;
String000002  LEN   CW     52125   52604   .       -       0       Pt=PEQU_00005;
String000002  WEise        NA    66200   66667   .   -       .       PID=PEQU_00006;lvid_id=Os03t0797100-00-D1363;Shift=0;
String000002  WEise        CW     66200   66667   .       -       0       Pt=PEQU_00006;
String000002  GUST        NA    90829   91128   .    +       .       PID=PEQU_00007;lvid_id=A00088;
String000002  GUST        CW     90829   91128   .    +       0       Pt=PEQU_00007;
String000002  LEN   NA    104627  107284  .        -       .       PID=PEQU_00008;lvid_id=PEQ_36749;
String000002  LEN   CW     104627  105584  .       -       1       Pt=PEQU_00008;

Is there a sed command that I could use?
# 2  
Old 03-04-2015
Hello Siya,

For your request not to change the formatting in file, I don't think so it is possible in a input file where space in between fields is inconsistent as in your provided input. As awk will consider it as same default width space only. If you need to change only field 6th then following may help.
Code:
 awk '{for(i=1;i<=NF;i++){if(i==6){$i="."}}} 1' Input_file

Thanks,
R. Singh
# 3  
Old 03-04-2015
If the spacing in your input file is a mixture of spaces and tabs, this should work with any sed that conforms to the standards.
Code:
sed 's/\([^[:space:]]*[[:space:]]*[^[:space:]]*[[:space:]]*[^[:space:]]*[[:space:]]*[^[:space:]]*[[:space:]]*[^[:space:]]*[[:space:]]*\)[[:digit:].]*/\1./' file

If the spacing in your input file is just spaces (no tabs), the following slightly simpler sed command should work with any sed that conforms to the standards:
Code:
sed 's/\([^ ]* *[^ ]* *[^ ]* *[^ ]* *[^ ]* *\)[[:digit:].]*/\1./' file

With GNU utilities sed, this might work if you change sed in either of the above commands to sed --posix.

With old versions of sed that do not recognize equivalence class expressions in basic regular expressions, you can change all occurrences of:
  • [:digit:] with 0-9 and
  • [:space:] with <space><tab> (where <space> is a literal space character and <tab> is a literal tab character
in the above sed substitute commands.

Both of the above produce the exact output you requested when they are run with sed on OS X Yosemite version 10.10.2.
# 4  
Old 03-04-2015
Hi.

There is a perl method for keeping field separators intact:
Code:
#!/usr/bin/env bash

# @(#) s1	Demonstrate perl method of keeping field separators intact.

# Utility functions: print-as-echo, print-line-with-visual-space, debug.
# export PATH="/usr/local/bin:/usr/bin:/bin"
LC_ALL=C ; LANG=C ; export LC_ALL LANG
pe() { for _i;do printf "%s" "$_i";done; printf "\n"; }
pl() { pe;pe "-----" ;pe "$*"; }
db() { ( printf " db, ";for _i;do printf "%s" "$_i";done;printf "\n" ) >&2 ; }
db() { : ; }
C=$HOME/bin/context && [ -f $C ] && $C perl

FILE=${1-data1}

pl " Input data file $FILE:"
pe " ( Separators: any run of dash, TAB SPACE, comma, equal-sign)"
od -bc $FILE

pl " Results, change data field 6 to \".\", keeping all separators:"
./p1 data1 |
od -bc

pl " perl code:"
cat p1

exit 0

producing:
Code:
$ ./s1

Environment: LC_ALL = C, LANG = C
(Versions displayed with local utility "version")
OS, ker|rel, machine: Linux, 2.6.26-2-amd64, x86_64
Distribution        : Debian 5.0.8 (lenny, workstation) 
bash GNU bash 3.2.39
perl 5.10.0

-----
 Input data file data1:
 ( Separators: any run of dash, TAB SPACE, comma, equal-sign)
0000000 141 054 142 055 143 040 040 144 011 145 011 011 146 011 040 040
          a   ,   b   -   c           d  \t   e  \t  \t   f  \t        
0000020 147 075 150 012
          g   =   h  \n
0000024

-----
 Results, change data field 6 to ".", keeping all separators:
0000000 141 054 142 055 143 040 040 144 011 145 011 011 056 011 040 040
          a   ,   b   -   c           d  \t   e  \t  \t   .  \t        
0000020 147 075 150 012
          g   =   h  \n
0000024

-----
 perl code:
#!/usr/bin/env perl

# @(#) p1	Demonstrate method for keeping field separators.
# Field 10 because array index begins at 0, and there is no
# leading separator on the line. So line field 6 is at 11-1 -> 10.

use warnings;
use strict;

my ( $debug, $i, @field );

# Interchange next 2 lines to enable printing of all fields and
# separators in individual components of array "field".
$debug = 1;
$debug = 0;

while (<>) {
  chomp;
  @field = split /([-\s,=]+)/;
  if ($debug) {
    for ( $i = 0; $i < $#field; $i++ ) {
      print STDERR "$i,\"$field[$i]\"\n";
    }
	print STDERR "\n";
  }
  $field[10] = ".";
  print join( "", @field ), "\n";
}

Best wishes ... cheers, drl

Last edited by drl; 03-04-2015 at 05:57 PM..
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Replace a numeric values in a certain column

Hi All, I am trying to replace a certain value from one place in a file . In the below file at position 35 I will have 8 I need to modify all 8 in that position to 7 I tried awk '{gsub("8","7",$35)}1' infile > outfile ----> not working sed -i 's/8/7'g' infile --- it is replacing all... (3 Replies)
Discussion started by: arunkumar_mca
3 Replies

2. Shell Programming and Scripting

Need to grep for decimal values only in the second column.

Hi, I have a file in which I need to print all the lines that have decimal values in the second column. The below prints all the decimal values from the second column but I need the complete line to be printed. cat hello.out | sed 's/ */:/g' | cut -d : -f 2 | ggrep -Eo "+\.+" Can you... (2 Replies)
Discussion started by: mohtashims
2 Replies

3. Shell Programming and Scripting

Trying to get an awk script to replace values in column

I'm trying to make an awk script to compare values I've set as var1, var2, and var3 earlier in the script to the values in the userinputted column of four text files called Node1.txt, Node2.txt, Node3.txt, and Node4.txt and then replace the values in that userinputted column with either ttt or gcc,... (8 Replies)
Discussion started by: Eric1
8 Replies

4. Shell Programming and Scripting

Replace column values from other file

I have one file as it has the following format File1 S No Site IP Address 1 Australia 192.168.0.1/26 2 Australia 192.168.0.2/26 3 Australia 192.168.0.3/26 I need awk/sed command to replace the column2 value ( under Site) with some other... (8 Replies)
Discussion started by: samaritan
8 Replies

5. Shell Programming and Scripting

Rounding decimal values in a column

Hi, I wanted to round all the values in a column to nearest integer. I have multiple files with only two columns and I want to round values in column 2. e.g input_file A1 23.971578 A2 34.624976 A3 46.403446 A4 375 A5 1 A6 3 A7 ... (3 Replies)
Discussion started by: ashu0001
3 Replies

6. Shell Programming and Scripting

Selective Replace awk column values

Hi, I have the following data: 2860377|"DATA1"|"DATA2"|"65343"|"DATA2"|"DATA4"|"11"|"DATA5"|"DATA6"|"65343"|"DATA7"|"0"|"8"|"1"|"NEGATIVE" 32340377|"DATA1"|"DATA2"|"65343"|"DATA2"|"DATA4"|"11"|"DATA5"|"DATA6"|"65343"|"DATA7"|"0"|"8"|"1"|"NEG-DID"... (3 Replies)
Discussion started by: sdohn
3 Replies

7. UNIX for Dummies Questions & Answers

replace a column with values from another file

Dear all, I have a file1.pdb in pdb format and a dat file2 containing values, corresponding to the atoms in the pdb file. these values (file2.dat) need to be in the column instead of the 0.00 (file1) values for each atom in file1.pdb .(the red values must be replaced by the blue ones,in order)... (11 Replies)
Discussion started by: chen.xiao.po
11 Replies

8. UNIX for Dummies Questions & Answers

Replace values in a specified column of a file

Hello, I have a file with four columns and I would like to replace values in the second column only. An arbitrary example is: 100 A 105 B 200 B 205 C 300 C 305 D 400 D 405 E 500 E 505 F I need to replace the second column as shown below: ... (4 Replies)
Discussion started by: Gussifinknottle
4 Replies

9. Shell Programming and Scripting

replace the column values.

I have the below file ...where some of the column values should replaced with desired values ....below file u can find that 3 column where ever 'AAA' comes should replaced with ' CC ' NOTE : we have to pass the column number ,AAA,CC (modified value) as the parameters to the code. ... (6 Replies)
Discussion started by: charandevu
6 Replies

10. UNIX for Advanced & Expert Users

replace a column values with the first value in column

Hi All, I have a file which has data in following format: "Body_Model","2/1/2007","2/1/2007" "CSCH74","0","61" "CSCS74","0","647" "CSCX74","0","3" "CSYH74","0","299" "CSYS74","0","2514" "CSYX74","0","3" "Body_Model","3/1/2007","3/1/2007" "CSCH74","0","88" "CSCS74","0","489"... (3 Replies)
Discussion started by: sumeet
3 Replies
Login or Register to Ask a Question