Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sed substitution and external command

Tags:

bash

sed

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 ?

like image 994
Michel Hua Avatar asked Sep 23 '13 11:09

Michel Hua


People also ask

What is substitution in 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.

What is sed command used for?

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 do you replace a variable in a file using sed?

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.

What is Option G in sed?

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.

How to replace a string in a file using sed command?

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.

What are the different types of Substitution commands in SED?

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.

What is sed command in Linux?

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.

What is global replacement in sed command?

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.


Video Answer


1 Answers

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
like image 179
Kent Avatar answered Oct 27 '22 00:10

Kent