Linux sed command
Can anyone help me understand what the following command does in Linux.
sed -i file.c -e "s/ __attribute__ ((__u开发者_StackOverflow中文版nused__))$$/# ifndef __cplusplus\n __attribute__ ((__unused__));\n# endif/"
It's doing an inplace search-and-replace on file.c
, looking for
__attribute__ (__unused__)
at the end of a line, and replacing any occurences with
# ifndef __cplusplus\n __attribute__ (__unused__);\nendif
which works out to:
# ifndef __cplusplus
__attribute__ (__unused__)
# endif
THe doubled brackets and $ signs are to "escape" those characters in the shell.
It adds # ifndef __cplusplus
and # endif
around __attribute__ ((__unused__));
in file.c
As Greg says in a comment, the $$
will expand to the PID of the shell which doesn't make sense in the context. If it was a single dollar sign, or wasn't there, the command could be shortened to:
sed -i file.c -e "s/ __attribute__ ((__unused__))$/# ifndef __cplusplus\n&;\n# endif/"
since &
brings forward what was matched between the first pair of delimiters (slashes in this example). The single dollar sign causes the match to only be made if the string is at the end of the line.
精彩评论