[英]sed command to remove a string and everything after it in that line
目錄中的.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;
}
如果您不需要使用sed
,則可以使用grep
輕松完成:
cat file.cpp | grep -v \/\/
說明:
grep -v
將打印所有與模式不匹配的行,模式\\/\\/
只是//
的轉義版本
如果您確實需要使用sed
,那么仍然可以輕松完成(可以說,它不是工作的正確工具,而且速度相當慢 )。
cat file.cpp | sed '/\/\//d'
這會匹配以//
開頭的每一行並將其刪除。
要刪除包含“ //”的每一行:
sed '/\/\//d' file.cpp
要刪除“ //”及其后面的所有內容:
sed 's|//.*||' file.cpp
同時執行這兩個操作(即刪除“ //”以及該行之后的所有內容,如果前面沒有空格,則刪除整行):
sed '/^ *\/\//d;s|//.*||' file.cpp
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.