在命令行中编辑文本文件并创建一个新文件

时间:2017-08-14 10:10:58

标签: bash

我有一个大文件看起来像例子:

chr1:16872433-16872504  54  112622
chr1:16872433-16872504  55  112110
chr1:16872433-16872504  56  110996
chr1:16872433-16872504  57  110306
chr1:16861773-16861845  20  38808
chr1:16861773-16861845  21  39768
chr1:16861773-16861845  22  40344
chr1:16861773-16861845  23  40637
chr1:16861773-16861845  24  41311
chr2:7990338-7990408    8   0
chr2:7990338-7990408    9   0
chr2:7990338-7990408    10  0
chr2:7990338-7990408    11  0
chr2:7990338-7990408    12  0

我想提取以" chr1:16872433-16872504"开头的每个部分。并创建一个新的.txt文件。 我怎么能用bash做到这一点?我尝试了grep命令,但我不知道如何让它成为条件。

1 个答案:

答案 0 :(得分:1)

@IBAction func btnContactsTapped() {
        let store = CNContactStore()

        if CNContactStore.authorizationStatus(for: .contacts) == .notDetermined {
            store.requestAccess(for: .contacts, completionHandler: { (authorized: Bool, error: Error?) -> Void in
                if authorized {
                    retrieveContactsWithStore(store: store)
                }
            })
        } else if CNContactStore.authorizationStatus(for: .contacts) == .authorized {
            self.retrieveContactsWithStore(store: store)
        }


        func retrieveContactsWithStore(store: CNContactStore) {
            do {
                let groups = try store.groups(matching: nil)
                let predicate = CNContact.predicateForContactsInGroup(withIdentifier: groups[0].identifier)
                //let predicate = CNContact.predicateForContactsMatchingName("John")
                let keysToFetch = [CNContactFormatter.descriptorForRequiredKeys(for: .fullName), CNContactEmailAddressesKey] as [Any]

                let contacts = try store.unifiedContacts(matching: predicate, keysToFetch: keysToFetch as! [CNKeyDescriptor])
//                self.objects = contacts
                DispatchQueue.main.async(execute: { () -> Void in
                    print("Contacts: \(contacts)")
                })
            } catch {
                print(error)
            }
        }
    }

为您提供以下输出

grep -E 'chr1:16872433-16872504' your.txt  > new.txt

根据您的要求[“chr1:16872433-16872504”]