Kwilk
Kwilk

Reputation: 41

sed - What's the difference here?

test.txt file contains:

AAAAA
BBBBB 
CCCCC

or in hex:

41 41 41 41 41 0A 42 42 42 42 42 0A 43 43 43 43 43 0A

If I run:

sed s/A/B/g test.txt

it returns:

BBBBB
BBBBB
CCCCC

Likewise:

sed 's/\x41/B/g' test.txt

returns:

BBBBB
BBBBB
CCCCC

but if I run:

sed 's/\x0A/B/g' test.txt

it still returns:

AAAAA
BBBBB
CCCCC

Why?

Upvotes: 1

Views: 65

Answers (1)

jaypal singh
jaypal singh

Reputation: 77095

sed works on one line at a time. For each line of the file, sed puts it on pattern-space by removing the new line (\n) from the line and does some action. Once the action is done, it places the new line back to the line and prints it out by default and reads the next line into pattern-space (unless forced not to by using -n option). This continues until the end of file is reached.

For your attempt, when sed reads the first line, it has already removed the new line from the line, hence your substitution is basically a no-op. Once that is done, it puts the new line back to your first line, prints it and reads the second line into pattern space and continues.

To get your desired output, you will have to read the entire file in to pattern space, with each line separated by a new line character.

You can do so by saying:

$ sed ':a;N;s/\x0A/B/;ba' file
AAAAABBBBBBBCCCCC
  • :a creates a label
  • N appends the next line into pattern spaces separated by a new line so your pattern spaces no contains line1\nline2.
  • s/\x0A/B/ is removing the \n from your pattern space and replaces it with B.
  • ba tells the sed to go back to label :a and repeat the process.
  • In the second run sed again appends the next line in to pattern space. Now your pattern spaces looks like line1Bline2\nline3. When the substitution occurs, you are left with your desired output.

Upvotes: 2

Related Questions