What is newline character — '\n'

前端 未结 8 2125
挽巷
挽巷 2020-12-07 01:18

This is a very basic concept, but something I have never been able to articulate that well. and I would like to try to spell it and see where I go wrong.

If I have t

8条回答
  •  感情败类
    2020-12-07 01:53

    From the sed man page:

    Normally, sed cyclically copies a line of input, not including its terminating newline character, into a pattern space, (unless there is something left after a "D" function), applies all of the commands with addresses that select that pattern space, copies the pattern space to the standard output, appending a newline, and deletes the pattern space.

    It's operating on the line without the newline present, so the pattern you have there can't ever match. You need to do something else - like match against $ (end-of-line) or ^ (start-of-line).

    Here's an example of something that worked for me:

    $ cat > states
    California
    Massachusetts
    Arizona
    $ sed -e 's/$/\
    > /' states
    California
    
    Massachusetts
    
    Arizona
    

    I typed a literal newline character after the \ in the sed line.

提交回复
热议问题