如何使用sed在特定行的开头添加文本?

时间:2019-09-20 16:10:26

标签: linux sed

我想在字符下方的每一行的开头添加一些文本。在终端中使用sed。

例如。如果我有textA.txt

@PL123
abcd
+
linewithmoretext
@PL456
efgh
+
2ndlinewithmoretext

以此类推,更多的行遵循相同的结构。

我希望我的输出是:textB.txt

@PL123
PREFIXabcd
+
linewithmoretext
@PL456
PREFIXefgh
+
2ndlinewithmoretext

我尝试过

sed 's/^/PREFIX/' textA.txt > textB.txt 

但是在所有行的开头插入PREFIX。但是我希望它更加具体,例如,我希望在包含@PL的行下方的每一行的开头添加PREFIX。 谁能帮我吗?我会非常感激。

3 个答案:

答案 0 :(得分:3)

只需使用awk:

Private Sub btnSend_Click()
Dim appOutLook As Outlook.Application
Dim MailOutLook As Outlook.MailItem
Dim strSubject As String
Dim strEmail As String
Dim strPDHSUM As String
Dim sqls As String
Dim MyDb As DAO.Database
Dim rsEmail As DAO.Recordset
    Set MyDb = CurrentDb
    Set rsEmail = MyDb.OpenRecordset("eqREPPDHSummaryZero")
    Set appOutLook = CreateObject("Outlook.Application")
    Set MailOutLook = appOutLook.CreateItem(olMailItem)
With rsEmail
    .MoveFirst
    Do Until rsEmail.EOF
    strEmail = .Fields(2)
    strPDHSUM = .Fields(1)
        With MailOutLook
            .BodyFormat = olFormatRichText
            .To = strEmail
            '.CC = ""
            '.bcc = ""
            .Subject = "PDH Summary"
            .HTMLBody = "Hello!" & "<br>" & "This is an automated reminder about the Professional Development Hour requirement for PFS. Each PFS staff member is required to have 4 hours of approved professional development each year. " & "<br>" & "<br>" & "So far this year you have taken " & strPDHSUM & " PD hours." & "<br>" & "<br>" & "Additional PDH classes are held each month and can be found at the " & "<a href=https://avalidaddress.com>PDH Class Schedule on OnBase</a> " & "If you feel there is an error in this information or need assistance signing up for PDH credits, please email " & "<a href=mailto:MyEmail@whereIWork.edu>Rob Loughrey</a>." & "<Br>" & "<br>" & "Thank you," & "<br>" & "PFS Education and Quality Unit"
            .Send
            '.Display    'Used during testing without sending (Comment out .Send if using this line)
        End With
        sqls = "INSERT INTO tblEmails " _
            & "(TypeofEmail, SendDateTime, EmailAddress) VALUES " _
            & "('PDH Summary', Now(), '" & strEmail & "');"
        DoCmd.RunSQL sqls

    .MoveNext
    Loop
End With
Set MyDb = Nothing
Set rsEmail = Nothing
End Sub````

答案 1 :(得分:2)

只要找到@PL,请阅读下一行,并在其前面加上PREFIX

sed '/@PL/{n;s/^/PREFIX/}' file

答案 2 :(得分:0)

我尝试了

sed '/@PL/aline' 

然后

perl -pi -e 's[line\n][PREFIX]g' 

,它也起作用。但是您的代码更简单,因此我将使用它们。