Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/linux/27.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/unix/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Linux sed命令删除该行中的字符串及其后面的所有内容_Linux_Unix - Fatal编程技术网

Linux sed命令删除该行中的字符串及其后面的所有内容

Linux sed命令删除该行中的字符串及其后面的所有内容,linux,unix,Linux,Unix,目录中的.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(

目录中的.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;
}
/**
*使用每个级别的两个比较执行标准二进制搜索。
*返回找到项的索引或它选择的索引
*如果找不到,请插入
*/
模板
int-binarySearch(常量可比*a、int-size、常量可比&x)
{
int low=0,high=size-1;//设置搜索的边界
while(低x)
high=mid-1;//如果x在数组中,则它必须在较低的位置
其他的
返回mid;//找到
}
//返回将插入x的位置
//保留数组中的顺序。
低回报;
}
使用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;
}
/**
*使用每个级别的两个比较执行标准二进制搜索。
*返回找到项的索引或它选择的索引
*如果找不到,请插入
*/
模板
int-binarySearch(常量可比*a、int-size、常量可比&x)
{
int低=0,高=size-1;
while(低x)
高=中-1;
其他的
中途返回;
}
低回报;
}

如果您不需要使用
sed
,可以使用
grep
轻松完成:

cat file.cpp | grep -v \/\/
说明:

grep-v
将打印与模式不匹配的所有行,模式
\/\/
只是
/
的转义版本

如果您确实需要使用
sed
,这仍然可以很容易地完成(它可能不是适合此工作的合适工具,而且)


这将匹配以
/
开头的每一行并将其删除。

要删除包含“/”的每一行,请执行以下操作:

要删除“/”及其后的所有内容,请执行以下操作:

sed 's|//.*||' file.cpp
要同时执行这两项操作(即删除“/”及其后面的所有内容,如果前面只有空格,则删除整行内容):


欢迎来到SO!到目前为止你试过什么?
sed '/\/\//d' file.cpp
sed 's|//.*||' file.cpp
sed '/^ *\/\//d;s|//.*||' file.cpp