I have an input log file in this format
May 23 2012 11:59:56
a;b;c
May 21 2012 16:54:12
d;e;f
May 19 2012 16:22:52
g;h;i
...
I would want to output it in this format
2012-05-23
a;b;c
2012-05-21
d;e;f
2012-05-19
g;h;i
...
Using sed
, I know how to substitute the date lines
% sed 's/.*:.*:.*/match_string/' input.txt
match_string
a;b;c
match_string
d;e;f
match_string
g;h;i
...
Using date
, I know how to convert dates :
% date -d 'May 23 2012 11:59:56' '+%Y-%m-%d'
2012-05-23
But how can make match_string to be evaluated during the sed command ?
Replacing all the occurrence of the pattern in a line : The substitute flag /g (global replacement) specifies the sed command to replace all the occurrences of the string in the line.
SED is a text stream editor used on Unix systems to edit files quickly and efficiently. The tool searches through, replaces, adds, and deletes lines in a text file without opening the file in a text editor. Learn how to use the sed command and its options through easy-to-follow examples.
How SED Works. In the syntax, you only need to provide a suitable “new string” name that you want to be placed with the “old string”. Of course, the old string name needs to be entered as well. Then, provide the file name in the place of “file_name” from where the old string will be found and replaced.
In some versions of sed, the expression must be preceded by -e to indicate that an expression follows. The s stands for substitute, while the g stands for global, which means that all matching occurrences in the line would be replaced.
Replacing or substituting string : Sed command is mostly used to replace the text in a file. The below simple sed command replaces the word “unix” with “linux” in the file. $sed 's/unix/linux/' geekfile.txt Output : linux is great os. unix is opensource.
s - The substitute command, probably the most used command in sed. / / / - Delimiter character. It can be any character but usually the slash ( /) character is used. SEARCH_REGEX - Normal string or a regular expression to search for. REPLACEMENT - The replacement string. g - Global replacement flag.
Overview sed is a powerful text processing tool in the Linux command-line. We often do text substitution using compact sed one-liners. They’re pretty convenient. However, when we execute sed substitution with shell variables, there are some pitfalls we should be aware of.
g - Global replacement flag. By default, sed reads the file line by line and changes only the first occurrence of the SEARCH_REGEX on a line. When the replacement flag is provided, all occurrences are replaced. INPUTFILE - The name of the file on which you want to run the command.
If you have GNU Sed available, you could use the e
flag:
sed 's/.*:.*/date -d"&" "+%Y-%m-%d"/ge' file
will help you for your example, see the test:
kent$ echo "May 23 2012 11:59:56
a;b;c
May 21 2012 16:54:12
d;e;f
May 19 2012 16:22:52
g;h;i"|sed 's/.*:.*/date -d"&" "+%Y-%m-%d"/ge'
2012-05-23
a;b;c
2012-05-21
d;e;f
2012-05-19
g;h;i
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With