假设我有这样一个文件:
$ head -n 15 foo.md
# User Database
#
# Note that this file is consulted directly only when the system is running
# in single-user mode. At other times this information is provided by
# Open Directory.
#
# See the opendirectoryd(8) man page for additional information about
# Open Directory.
##
nobody:*:-2:-2:Unprivileged User:/var/empty:/usr/bin/false
root:*:0:0:System Administrator:/var/root:/bin/sh
daemon:*:1:1:System Services:/var/root:/usr/bin/false
_uucp:*:4:4:Unix to Unix Copy Protocol:/var/spool/uucp:/usr/sbin/uucico
_taskgated:*:13:13:Task Gate Daemon:/var/empty:/usr/bin/false
我想查看除'#'之外的内容,
它可以通过反转选项来实现,比如
$ grep -v“^#”< foo.md
我打算否定^
$ grep -E "[^#]" < foo.md
# User Database
#
# Note that this file is consulted directly only when the system is running
# in single-user mode. At other times this information is provided by
并且
$ grep -Eo "[^#]+" < foo.md
User Database
Note that this file is consulted directly only when the system is running
in single-user mode. At other times this information is provided by
Open Directory.
See the opendirectoryd(8) man page for additional information about
Open Directory.
nobody:*:-2:-2:Unprivileged User:/var/empty:/usr/bin/false
命令未按我的意愿输出。
如何用否定来解决这个问题?
答案 0 :(得分:1)
如果我是对的,你希望只有非评论行,最后5行,而不是以'#'开头?
对于我来说,在Mac OS X或CentOS上使用grep -v“^#”非常有效,输出中只有最后5行。
选项-v表示反转匹配,字符'^'表示'行的开头'
答案 1 :(得分:1)
您的文件在- (NSManagedObjectContext *) managedObjectContext {
NSThread *thisThread = [NSThread currentThread];
if (thisThread == [NSThread mainThread])
{
//For the Main thread just return default context iVar
if (managedObjectContext != nil) {
return managedObjectContext;
}
NSPersistentStoreCoordinator *coordinator = [self persistentStoreCoordinator];
if (coordinator != nil) {
managedObjectContext = [[NSManagedObjectContext alloc] initWithConcurrencyType:NSPrivateQueueConcurrencyType];
[managedObjectContext setPersistentStoreCoordinator: coordinator];
}
return managedObjectContext;
}
else
{
//Return separate MOC for each new thread
NSManagedObjectContext * threadManagedObjectContext = [[thisThread threadDictionary] objectForKey:@"MOC_KEY"];
if (threadManagedObjectContext == nil)
{
threadManagedObjectContext = [[NSManagedObjectContext alloc] initWithConcurrencyType:NSPrivateQueueConcurrencyType];
[threadManagedObjectContext setPersistentStoreCoordinator: [self persistentStoreCoordinator]];
[[thisThread threadDictionary] setObject:threadManagedObjectContext forKey:@"MOC_KEY"];
}
return threadManagedObjectContext;
}
}
之前有空格,因此您需要对其进行说明。这里不需要否定并使一切变得复杂 - 特别是在尝试反向匹配时,所以不要为此烦恼。
#
<强>输出强>:
grep -v "^\s*#" file.txt
要反过来,只需丢失反转匹配(nobody:*:-2:-2:Unprivileged User:/var/empty:/usr/bin/false
root:*:0:0:System Administrator:/var/root:/bin/sh
daemon:*:1:1:System Services:/var/root:/usr/bin/false
_uucp:*:4:4:Unix to Unix Copy Protocol:/var/spool/uucp:/usr/sbin/uucico
_taskgated:*:13:13:Task Gate Daemon:/var/empty:/usr/bin/false
)选项:
-v
<强>输出强>:
grep "^\s*#"
使用您的命令
# User Database # # Note that this file is consulted directly only when the system is running # in single-user mode. At other times this information is provided by # Open Directory. # # See the opendirectoryd(8) man page for additional information about # Open Directory. ##
,您只能告诉grep (grep -Eo "[^#]+"
)在没有#的情况下匹配行的部分 - 所以它显然很明显 除#字符外,输出所有内容。
POSIX版本:
-o