Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. It's 100% free, no registration required.

I have had a requirement where I need to move data to a new line based on a text:

Input
:61: 456 B66666 :61: 878 N78777 :61: 534533534 BNNN

Output
:61: 456 B66666
:61: 878 N78777
:61: 534533534 BNNN

So basically as soon as it encounters :61: it should move to a new line.

share|improve this question

2 Answers 2

Here is a very simple solution which works perfectly on your example:

sed 's/ :61:/\n:61:/g' < input_file

You may have to adapt it a little, especially if you don't always have a space before :61: in your input files.

share|improve this answer

Similar to @lgeorget's answer, this adds a newline before any ":61:" that is not at the beginning of the line:

perl -pe 's/(?<!^)(?=:61:)/\n/g' file
share|improve this answer
2  
(?<!^) is a negative look-behind assertion: the current location does not immediately follow the start of the line. (?=:61:) is a positive look-ahead assertion: the current location is immediately followed by the string ":61:". If both of those are true, replace this (zero-width) string with a newline. –  glenn jackman Oct 22 '14 at 18:29

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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