我有一个文件a.text:
hello world
my world
hello universe
如果第二个单词是“world”,我想打印完整的字符串:
[root@sc-rdops-vm18-dhcp-57-128:/var/log] cat a | awk -F " " '{if($2=="world") print $1}'
hello
my
但我想要的输出是:
[root@sc-rdops-vm18-dhcp-57-128:/var/log] cat a | awk -F " " '{if($2=="world") print <Something here>}'
hello world
my world
关于我如何做到这一点的任何指示?
提前致谢。
答案 0 :(得分:2)
awk '{if ($2=="world") {print}}' file
输出:
hello world my world
答案 1 :(得分:2)
首先,由于您正在编写单个if语句,因此可以使用awk 'filter{commands;}'
模式,如此
awk -F " " '$2=="world" { print <Something here> }'
要打印整行,您可以使用print $0
awk -F " " '$2=="world"{print $0}' file
可以写成
awk -F " " '$2=="world"{print}' file
但是{print}
是默认操作,因此在过滤器之后可以省略它:
awk -F " " '$2=="world"' file
甚至没有-F选项,因为空格是默认的FS值
awk '$2=="world"' file
答案 2 :(得分:1)
如果您想/必须使用 awk '$0~/world/' file.txt
来解决问题:
$0
如果一行(即~/world/
)与字符串“world”匹配(即world
),则打印整行
如果您只想查看 awk '$2 == "world"' file.txt
的第二列:
DECLARE @DynamicPivotQuery AS NVARCHAR(MAX)
DECLARE @ColumnName AS NVARCHAR(MAX)
SELECT @ColumnName= ISNULL(@ColumnName + ',','')
+ QUOTENAME([Month])
FROM (SELECT DISTINCT TOP 99.99 PERCENT [Month] FROM [Service Contract Data].[dbo].[filtered_data] ORDER BY [Month]) AS [Month]
SET @DynamicPivotQuery =
N'SELECT [Customer ID], ' + @ColumnName + '
INTO [Service Contract Data].[dbo].[Pivot_Results] FROM [Service Contract Data].[dbo].[filtered_data]
PIVOT(SUM([Service Contract Data].[dbo].[filtered_data].[Subscription Amount])
FOR [Month] IN (' + @ColumnName + ')) AS PVTTable'
EXEC sp_executesql @DynamicPivotQuery;