-1

One of my elder brother who is studying in Statistics. Now, he is writing his thesis paper in LaTeX. Almost all contents are written for the paper. And he took 5 number after point(e.g. 5.55534) for each value those are used for his calculation. But, at the last time his instructor said to change those to 3 number after point(e.g. 5.555) which falls my brother in trouble. Finding and correcting those manually is not easy. So, he told me to help.

I believe there is also a easy solution which is know to me. The snapshot of a portion of the thesis looks like-

      &se($\hat\beta_1$)&0.35581&0.35573&0.35573\\
                         &mse($\hat\beta_1$)&.12945&.12947&.12947\\
   \addlinespace
                         &$\hat\beta_2$&0.03329&0.03331&0.03331 \\
                          &se($\hat\beta_2$)&0.01593&0.01592&0.01591\\
                          &mse($\hat\beta_2$)&.000265&.000264&.000264 \\

                         \midrule
  {n=100} & $\hat\beta_1$&-.52006&-.52001&-.51946\\
                        &se($\hat\beta_1$)&.22819&.22814&.22795\\
                        &mse($\hat\beta_1$)&.05247&.05244&.05234\\
   \addlinespace
                         &$\hat\beta_2$&0.03134&0.03134&0.03133 \\
                         &se($\hat\beta_2$)&0.00979&0.00979&0.00979\\
                         &mse($\hat\beta_2$)&.000098&.000098&.000098

I want -

      &se($\hat\beta_1$)&0.355&0.355&0.355\\
                         &mse($\hat\beta_1$)&.129&.129&.129\\
......................................................................
........................................................................
........................................................................

Note: Don't feel boring for the syntax(These are LaTeX syntax).

If anybody has solution or suggestion, please provide. Thank you.

alhelal
  • 916
  • 11
  • 27
  • [tag:perl], [tag:vim], [tag:awk], [tag:sed] ...? Which is it? I removed all tags except [tag:sed], since that has an upvoted answer. – Martin Tournoij Jan 11 '17 at 22:18

2 Answers2

2

In sed:

$ sed 's/\(\.[0-9]\{3\}\)[0-9]*/\1/g' file
      &se($\hat\beta_1$)&0.355&0.355&0.355\\
                         &mse($\hat\beta_1$)&.129&.129&.129\\

ie. replace period starting numeric strings with at least 3 numbers with the leading period and three first numbers.

James Brown
  • 36,089
  • 7
  • 43
  • 59
1

Here is the command in vim:

:%s/\.\d\{3}\zs\d\+//g

Explanation:

: entering command-mode

% is the range of all lines of the file

s substitution command

\.\d\{3}\zs\d\+ pattern you would like to change

  • \. literal point (.)

  • \d\{3} match 3 consecutive digits

  • \zs start substitution from here

  • \d\+ one or more digits

g Replace all occurrences in the line


Concerning grep and cat they have nothing to do with replacing text. These commands are only for searching and printing contents of files.

Instead, what you are looking is substitution there are lots of commands in Linux that can do that mainly sed, perl, awk, ex etc.