2

How do I remove the string "\n" (not the newline character) from the end of only lines that begin with the string "///"? Thanks.

Example Input:

Some text\n

/// Some comment\n

Desired Output:

Some text\n

/// Some comment

I've tried to use

sed -i 's/\\n*$//' $1

But, it removes all of the trailing "\n" strings.

2

Add a regex address specifier at the front - since your pattern has forward slashes, it's clearer if you use a different expression separator:

sed -i '\%^///% s/\\n$//' Input

Testing:

$ sed '\%^///% s/\\n$//' Input 
Some text\n

/// Some comment

If you prefer "leaning toothpicks"

sed -i '/^\/\/\// s/\\n$//' Input

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.