将grep和grep -v搜索结合在一起

时间:2016-10-07 07:34:45

标签: bash shell unix

我正在尝试将SELECT column_name, table_name FROM `information_schema`.`columns` WHERE `table_schema` = DATABASE() AND `table_name` in ('table1', 'table2'); grep搜索结合在一起。 输出应显示以grep -v结尾的所有行,但要排除以.xml开头的行。

以下是我试过的命令;没有工作:

  • $
  • grep *.xml file1.txt | grep -v '$' file1.txt > output
    
  • grep *.xml | grep -v '$' file1.txt > output
    
  • grep *.xml grep -v '$' file1.txt > output
    

2 个答案:

答案 0 :(得分:8)

要匹配行首的$,请使用^将其锚定到行的开头。此外,$本身与行尾相匹配(它是一个特殊字符,就像^),而*将不会按照您的想法执行(它的工作原理)正则表达式与shell globbing模式相比有所不同)。所以,

grep -v '^\$'

将过滤掉以$开头的所有行。

你可以做任何一次

grep '\.xml$' file1.txt | grep -v '^\$'

grep '^[^$].*\.xml$' file1.txt

查找文件file1.txt中所有不以$开头但以.xml结尾的行。

请注意,我也转义.xml中的点,否则匹配任何字符,第二个命令通过使用包含除{{1}之外的所有字符的字符范围([ ... ])来组合这两个条件($匹配任意数量的任何字符)。

单引号是必要的,以便shell不会将正则表达式解释为shell globbing模式。

答案 1 :(得分:-1)

你应该使用" cat"命令将输出定向到文件。

然后使用正则表达式过滤关键字,在这种情况下,所有以$符号开头的行都是' ^ [$]'。

所以你可以使用命令Sub Open_Workbook_Dialog() Dim my_FileName As Variant MsgBox "Pick your TOV file" '<--| txt box for prompt to pick a file my_FileName = Application.GetOpenFilename(FileFilter:="Excel Files,*.xl*;*.xm*") '<--| Opens the file window to allow selection If my_FileName <> False Then Workbooks.Open Filename:=my_FileName Call Sample '<--|Calls the Filter Code and executes Call Filter '<--|Calls the Filter Code and executes End If End Sub Public Sub Sample() Dim ws As Worksheet Dim aCell As Range, Rng As Range Dim col As Long, lRow As Long Dim colName As String '~~> Change this to the relevant sheet Set ws = ThisWorkbook.Sheets("Sheet1") With ws Set aCell = .Range("A1:X50").Find(What:="CountryCode", LookIn:=xlValues, LookAt:=xlWhole, _ MatchCase:=False, SearchFormat:=False) '~~> If Found If Not aCell Is Nothing Then '~~> Cut the entire column aCell.EntireColumn.Cut '~~> Insert the column here Columns("F:F").Insert Shift:=xlToRight Else MsgBox "Country Not Found" End If End With End Sub Public Sub Filter() Dim rCountry As Range, helpCol As Range With Worksheets("Sheet1") '<--| refer to data worksheet With .UsedRange Set helpCol = .Resize(1, 1).Offset(, .Columns.Count) '<--| get a "helper" column just at the right of used range, it'll be used to store unique country names in End With With .Range("A1:Q" & .Cells(.Rows.Count, 1).End(xlUp).Row) '<--| refer to its columns "A:Q" from row 1 to last non empty row of column "A" .Columns(6).AdvancedFilter Action:=xlFilterCopy, CopyToRange:=helpCol, Unique:=True '<-- call AdvancedFilter on 6th column of the referenced range and store its unique values in "helper" column Set helpCol = Range(helpCol.Offset(1), helpCol.End(xlDown)) '<--| set range with unique names in (skip header row) For Each rCountry In helpCol '<--| iterate over unique country names range (skip header row) .AutoFilter 6, rCountry.Value2 '<--| filter data on country field (6th column) with current unique country name If Application.WorksheetFunction.Subtotal(103, .Cells.Resize(, 1)) > 1 Then '<--| if any cell other than header ones has been filtered... Worksheets.Add Worksheets(Worksheets.Count) '<--... add new sheet ActiveSheet.Name = rCountry.Value2 '<--... rename it .SpecialCells(xlCellTypeVisible).Copy ActiveSheet.Range("A1") 'copy data for country under header End If Next End With .AutoFilterMode = False '<--| remove autofilter and show all rows back End With helpCol.Offset(-1).End(xlDown).Clear '<--| clear helper column (header included) End Sub