2

I have non-standard data, which I'd like to standardise

file:

d101 11001 e101 9665 f101 9663 d102 11002 e102 11003 f102 11004 g102 11005 

desired output:

d101 11001 e101 12001 f101 12002 d102 11002 e102 11003 f102 11004 g102 11005 

so the logic should be, if length of column2 = 4 it should replace it with incremental numbering of a provided series: in this case 1200 is series, & 1, 2, 3 .. are increments.

0

    2 Answers 2

    6
    $ awk -v n=12000 'length($2)==4 {$2=++n} {print}' file d101 11001 e101 12001 f101 12002 d102 11002 e102 11003 f102 11004 g102 11005 

    Note that we first increment n and then assign, to use the new value. If we wanted to start printing from 12000 we would use: $2=n++, first assign and then increase.

      0

      Using Raku (formerly known as Perl6)

      raku -ne 'state $i; print .words[0]~" "; put S/^ \d**4 $/{++$i + 12000}/ given .words[1];' 

      Briefly, raku is called at the command line with the -ne (linewise, non-autoprinting) flags. A state variable is declared (once and only once), for incrementing within the code. In the second statement the .words[0] first whitespace separated 'word' (i.e. column) is printed followed by a ~ tilde concatenated " " blank space (for separating the output columns). In the third statement the S/// (big-S) substitution command searches for \d**4 exactly 4 digits within given .words[1] (the second column). A match is replaced with {++$i + 12000} a computed value (curlies denote code blocks within regexes), and returned (because S/// big-S substitution returns the modified string).

      Sample Input:

      d101 11001 e101 9665 f101 9663 d102 11002 e102 11003 f102 11004 g102 11005 

      Sample Output:

      d101 11001 e101 12001 f101 12002 d102 11002 e102 11003 f102 11004 g102 11005 

      https://raku.org

        You must log in to answer this question.

        Start asking to get answers

        Find the answer to your question by asking.

        Ask question

        Explore related questions

        See similar questions with these tags.