插入评论//当条件不匹配

问题描述 投票:0回答:2

如何得到想要的输出? 对于文本文件,当满足以下条件之一时:

  • 一行的第一个字段有字符串“type”或“const”(全部小写)
  • 该行有“(”和“)”
  • 以“{”开头的多行,以“}”作为一行的第一个字符结束
  • 以注释“//”开头的行

然后打印(像往常一样),
否则在行首插入注释“//”并打印

例如 输入:

TypeScript ch6 (2/3) Advanced Function Types
 Types TypeScript  
  Totality TSC Flag: noImplicitReturns Advanced Object Types
   The keyof operator TSC Flag: keyofStringsOnly The Record Type Mapped Types Built-in mapped types 
type Weekday = 'Mon' | 'Tue' | 'Wed'|'Thu'|'Fri'

type Day = Weekday | 'Sat' | 'Sun'
// Error: Function lacks ending return statement and return type does not include 'undefined'.
function getNextDay(w: Weekday): Day {
  switch(w) {
    case 'Mon': return 'Tue'
  }
}
{  }
{
}
{
  {
    test
  }
}
const i = 1
 TSC Flag: noImplicitReturns tsconfig"noImplicitReturns": true // Error: Not all code paths return a value.
function isBig(n: number) {
  if (n >= 100) {
    return true
  }
}
 Advanced Object Types Type Operators for Object Types 
 Companion Object Pattern
type APIResponse = {
  user: {
    userId: string,
    friendList: {
      count: number,
      friends: {
        firstName: string,
        lastName: string
      }[]
    }
  }
}
function getAPIResponse(): Promise<APIResponse> {
  // ...
}
const response = await getAPIResponse()
renderFriendList(response.user.friendList)
 friendListtypehint any FriendList

输出:

//TypeScript ch6 (2/3) Advanced Function Types
// Types TypeScript  
//  Totality TSC Flag: noImplicitReturns Advanced Object Types
//   The keyof operator TSC Flag: keyofStringsOnly The Record Type Mapped Types Built-in mapped types 
type Weekday = 'Mon' | 'Tue' | 'Wed'|'Thu'|'Fri'

type Day = Weekday | 'Sat' | 'Sun'
// Error: Function lacks ending return statement and return type does not include 'undefined'.
function getNextDay(w: Weekday): Day {
  switch(w) {
    case 'Mon': return 'Tue'
  }
}
{  }
{
}
{
  {
    test
  }
}
const i = 1
// TSC Flag: noImplicitReturns tsconfig"noImplicitReturns": true // Error: Not all code paths return a value.
function isBig(n: number) {
  if (n >= 100) {
    return true
  }
}
// Advanced Object Types Type Operators for Object Types 
// Companion Object Pattern
type APIResponse = {
  user: {
    userId: string,
    friendList: {
      count: number,
      friends: {
        firstName: string,
        lastName: string
      }[]
    }
  }
}
function getAPIResponse(): Promise<APIResponse> {
  // ...
}
const response = await getAPIResponse()
renderFriendList(response.user.friendList)
// friendListtypehint any FriendList

我试过这个脚本,它没有正确处理“以“{”开头并以“}”作为一行的第一个字符的多行”:

awk '{
  if ($1 ~ /type/ || $1 ~ /const/ || $0 ~ /\(.*)/ || $0 ~ /{.*}/ || $0 ~ /\/\//)
    print $0;
  else
    if ($0 !~ /}/)
      print "//", $0;
    else 
      print $0;
}' input
awk sed
2个回答
1
投票

以“{”开始并以“}”结束的多行 作为一行的第一个字符

这听起来像是

getline
函数的任务,为了简单起见,让我们考虑只做两件事情的情况,即上述条件和 一行的第一个字段有字符串“type”或“const”(全部小写) 并且您正在使用以下
file.txt

this should become comment
type Boolean = 0 | 1
this too should become comment
function func(){
  if(true){
    console.log("This is not comment")
    return 123
  }
}
this also should comment

然后

awk '$1~/type|const/{print;next}/{/{print;while(!/^}/){getline;print};next}{print "//",$0}' file.txt

给出输出

// this should become comment
type Boolean = 0 | 1
// this too should become comment
function func(){
  if(true){
    console.log("This is not comment")
    return 123
  }
}
// this also should comment

解释:如果第一个字段包含

type
const
,那么
print
按原样行并转到
next
行,如果行包含
{
然后
print
按原样使用重复
print 
ing 行直到以
}
字符开头的行,完成后转到
next
行。所有尚未完成的行都将以
//
和输出字段分隔符(默认为空格)为前缀。

(在 GNU Awk 5.0.1 中测试)


0
投票

这满足您列出的要求,使用任何 awk:

$ cat tst.awk
{
    comment = 0
    if ( inBlock ) {
        if ( /^}/ ) {
            inBlock = 0
        }
    }
    else if ( /.*{$/ ) {
        inBlock = 1
    }
    else if ( !( ($1 ~ /^(type|const|\/\/)/) || /\(.*)/ ) ) {
        comment = 1
    }
}
comment {
    $0 = "//" $0
}
{ print }

$ awk -f tst.awk file
TypeScript ch6 (2/3) Advanced Function Types
// Types TypeScript
//  Totality TSC Flag: noImplicitReturns Advanced Object Types
//   The keyof operator TSC Flag: keyofStringsOnly The Record Type Mapped Types Built-in mapped types
type Weekday = 'Mon' | 'Tue' | 'Wed'|'Thu'|'Fri'
//
type Day = Weekday | 'Sat' | 'Sun'
// Error: Function lacks ending return statement and return type does not include 'undefined'.
function getNextDay(w: Weekday): Day {
  switch(w) {
    case 'Mon': return 'Tue'
  }
}
{  }
{
}
{
  {
    test
  }
}
const i = 1
// TSC Flag: noImplicitReturns tsconfig"noImplicitReturns": true // Error: Not all code paths return a value.
function isBig(n: number) {
  if (n >= 100) {
    return true
  }
}
// Advanced Object Types Type Operators for Object Types
// Companion Object Pattern
type APIResponse = {
  user: {
    userId: string,
    friendList: {
      count: number,
      friends: {
        firstName: string,
        lastName: string
      }[]
    }
  }
}
function getAPIResponse(): Promise<APIResponse> {
  // ...
}
const response = await getAPIResponse()
renderFriendList(response.user.friendList)
// friendListtypehint any FriendList

但它不会根据您显示的输入产生您显示的预期输出,因为预期输出不符合您列出的要求,请参阅我在您的问题下的 3 条评论。

如果我们在您的“请勿评论”列表中添加 2 个要求,我们可以更接近您发布的预期输出:

  • 空行或所有空白行
  • 该行有“{”和“}”

可以通过将上面的一行调整为:

$ cat tst.awk
{
    comment = 0
    if ( inBlock ) {
        if ( /^}/ ) {
            inBlock = 0
        }
    }
    else if ( /.*{$/ ) {
        inBlock = 1
    }
    else if ( NF && !( ($1 ~ /^(type|const|\/\/)/) || /\(.*)/ || /\{.*}/ ) ) {
        comment = 1
    }
}
comment {
    $0 = "//" $0
}
{ print }

$ awk -f tst.awk file
TypeScript ch6 (2/3) Advanced Function Types
// Types TypeScript
//  Totality TSC Flag: noImplicitReturns Advanced Object Types
//   The keyof operator TSC Flag: keyofStringsOnly The Record Type Mapped Types Built-in mapped types
type Weekday = 'Mon' | 'Tue' | 'Wed'|'Thu'|'Fri'

type Day = Weekday | 'Sat' | 'Sun'
// Error: Function lacks ending return statement and return type does not include 'undefined'.
function getNextDay(w: Weekday): Day {
  switch(w) {
    case 'Mon': return 'Tue'
  }
}
{  }
{
}
{
  {
    test
  }
}
const i = 1
// TSC Flag: noImplicitReturns tsconfig"noImplicitReturns": true // Error: Not all code paths return a value.
function isBig(n: number) {
  if (n >= 100) {
    return true
  }
}
// Advanced Object Types Type Operators for Object Types
// Companion Object Pattern
type APIResponse = {
  user: {
    userId: string,
    friendList: {
      count: number,
      friends: {
        firstName: string,
        lastName: string
      }[]
    }
  }
}
function getAPIResponse(): Promise<APIResponse> {
  // ...
}
const response = await getAPIResponse()
renderFriendList(response.user.friendList)
// friendListtypehint any FriendList

但我不知道如何处理您的预期输出,它从您的输入中评论了这一行:

TypeScript ch6 (2/3) Advanced Function Types

但不评论这个:

renderFriendList(response.user.friendList)

因为他们都满足你的要求:

  • 该行有“(”和“)”
© www.soinside.com 2019 - 2024. All rights reserved.