Home > Blockchain >  Replace the number matching with pattern to date
Replace the number matching with pattern to date

Time:06-30

From the below file I wanted to replace all the numbers which match with a pattern say 165 or 164 with a pattern say "2022-06-10 10:43:55".

Please note columns are not static so numbers can be present in any column. I am trying to do using sed and awk but somehow didn't work for me.

Sample file:

51      Nick Trump   [email protected]   OTHERS     1653029034386   1653029034386   1653029034385   \N      \N           
52      Nick Trump   [email protected]              1653029527542   1653029527542   1653029527540   \N      \N                        
53      Nick Trump   [email protected]              1643029528100   1643029528100   1643029528099   \N      \N                          
54      Nick Trump   [email protected]              1653029530894   1653029530894   1653029530892   \N      \N                        
55      Nick Trump   [email protected]              1653029534291   1653029534291   1653029534290   \N      \N

Desired output:

51      Nick Trump   [email protected]   OTHERS     2022-06-10 10:43:55 2022-06-10 10:43:55 2022-06-10 10:43:55 \N      \N           
52      Nick Trump   [email protected]              2022-06-10 10:43:55 2022-06-10 10:43:55 2022-06-10 10:43:55 \N      \N             
53      Nick Trump   [email protected]              2022-06-10 10:43:55 2022-06-10 10:43:55 2022-06-10 10:43:55 \N      \N             
54      Nick Trump   [email protected]              2022-06-10 10:43:55 2022-06-10 10:43:55 2022-06-10 10:43:55 \N      \N          
55      Nick Trump   [email protected]              2022-06-10 10:43:55 2022-06-10 10:43:55 2022-06-10 10:43:55 \N      \N          

Appreciate your help here.
Thanks,

CodePudding user response:

I would use GNU sed for this task following way, let file.txt content be

51 Nick Trump [email protected] OTHERS 1653029034386 1653029034386 1653029034385 \N \N
52 Nick Trump [email protected] 1653029527542 1653029527542 1653029527540 \N \N
53 Nick Trump [email protected] 1643029528100 1643029528100 1643029528099 \N \N
54 Nick Trump [email protected] 1653029530894 1653029530894 1653029530892 \N \N
55 Nick Trump [email protected] 1653029534291 1653029534291 1653029534290 \N \N

then

sed 's/\w*16[45]\w*/2022-06-10 10:43:55/g' file.txt

gives output

51 Nick Trump [email protected] OTHERS 2022-06-10 10:43:55 2022-06-10 10:43:55 2022-06-10 10:43:55 \N \N
52 Nick Trump [email protected] 2022-06-10 10:43:55 2022-06-10 10:43:55 2022-06-10 10:43:55 \N \N
53 Nick Trump [email protected] 2022-06-10 10:43:55 2022-06-10 10:43:55 2022-06-10 10:43:55 \N \N
54 Nick Trump [email protected] 2022-06-10 10:43:55 2022-06-10 10:43:55 2022-06-10 10:43:55 \N \N
55 Nick Trump [email protected] 2022-06-10 10:43:55 2022-06-10 10:43:55 2022-06-10 10:43:55 \N \N

Explanation: I inform GNU sed to replace zero-or-more (*) word-characters (\w) followed by 16 followed by one of ([...]) 45 (i.e. 4 or 5) followed by zero-or-more (*) word-characters (\w) using 2022-06-10 10:43:55 globally (g)

(tested in GNU sed 4.2.2)

CodePudding user response:

Using sed

$ sed s'/16[45][[:digit:]]\ /2022-06-10 10:43:55/g' input_file
51      Nick Trump   [email protected]   OTHERS     2022-06-10 10:43:55   2022-06-10 10:43:55   2022-06-10 10:43:55   \N      \N
52      Nick Trump   [email protected]              2022-06-10 10:43:55   2022-06-10 10:43:55   2022-06-10 10:43:55   \N      \N
53      Nick Trump   [email protected]              2022-06-10 10:43:55   2022-06-10 10:43:55   2022-06-10 10:43:55   \N      \N
54      Nick Trump   [email protected]              2022-06-10 10:43:55   2022-06-10 10:43:55   2022-06-10 10:43:55   \N      \N
55      Nick Trump   [email protected]              2022-06-10 10:43:55   2022-06-10 10:43:55   2022-06-10 10:43:55   \N      \N
  • Related