目录中的.cpp文件包含以下文本:
/**
* Performs the standard binary search using two comparisons per level.
* Returns index where item is found or or the index where it chould
* be inserted if not found
*/
template <typename Comparable>
int binarySearch( const Comparable* a, int size, const Comparable & x )
{
int low = 0, high = size - 1; // Set the bounds for the search
while( low <= high )
{
// Examine the element at the midpoint
int mid = ( low + high ) / 2;
if( a[ mid ] < x )
low = mid + 1; // If x is in the array, it must be in the upper
else if( a[ mid ] > x )
high = mid - 1; // If x is in the array, it must be in the lower
else
return mid; // Found
}
// Return the position where x would be inserted to
// preserve the ordering within the array.
return low;
}
使用unix sed命令,如何打印上面的.cpp文件的内容,并删除所有内联注释字符串(如下所示://),并删除该行之后的所有文本?我在下面给出了我要寻找的示例。该行中所有// //标记及其后的所有内容都消失在了所需的输出中。
/**
* Performs the standard binary search using two comparisons per level.
* Returns index where item is found or or the index where it chould
* be inserted if not found
*/
template <typename Comparable>
int binarySearch( const Comparable* a, int size, const Comparable & x )
{
int low = 0, high = size - 1;
while( low <= high )
{
int mid = ( low + high ) / 2;
if( a[ mid ] < x )
low = mid + 1;
else if( a[ mid ] > x )
high = mid - 1;
else
return mid;
}
return low;
}
答案 0 :(得分:0)
如果您不需要使用sed
,可以使用grep
轻松完成:
cat file.cpp | grep -v \/\/
说明:
grep -v
将打印所有与模式不匹配的行,而模式\/\/
只是//
的转义版本
如果您确实需要使用sed
,则仍然可以轻松完成此操作(可以说,它quite a bit slower并不是正确的工作工具)。
cat file.cpp | sed '/\/\//d'
这匹配以//
开头的每一行并将其删除。
答案 1 :(得分:0)
要删除包含“ //”的每一行:
sed '/\/\//d' file.cpp
要删除“ //”及其后面的所有内容:
sed 's|//.*||' file.cpp
同时执行这两项操作(即删除“ //”以及该行之后的所有内容,如果前面没有空格,则删除整行):
sed '/^ *\/\//d;s|//.*||' file.cpp