Replace space by newline error

Hello
I have had a requirement where I need to move data to a new line based on a text .So basically as soon as it encounters :61: it should move to a new line

 
Source Data :
:61:D100,74NCH1 :61:D797,50NCH2 :61:D89,38NCHK2 :61:D99,38NCHK12 :61:D79,38NCHK22 :61:D29,38NCHK5  
 
Target Data required :
:61:D100,74NCH1 
:61:D797,50NCH2 
:61:D89,38NCHK2
:61:D99,38NCHK12 
:61:D79,38NCHK22 
:61:D29,38NCHK5 
Command Used :
  sed 's/\([[:space:]]\)\(:61:\)/\1\n\2/g'  file1 > file2  

Issues faced :
1) sometimes the last record is missed (like the Record:61:D29,38NCHK5 will not show up in the target)
2) One of the source entries will get duplicated in the target

Can somebody please suggest any other command or any modifications
Thanks

$echo :61:D100,74NCH1 :61:D797,50NCH2 :61:D89,38NCHK2 :61:D99,38NCHK12 :61:D79,38NCHK22 :61:D29,38NCHK5 | sed -e 's/ :/\
> :/g'
:61:D100,74NCH1
:61:D797,50NCH2
:61:D89,38NCHK2
:61:D99,38NCHK12
:61:D79,38NCHK22
:61:D29,38NCHK5

At the end of that first line is /<space>:/\<return>:/g

If the :61: is a required break character set, and not just space-colon, you would type /<space>:61:/\<return>:61:/ to get the required effect. If it's really just the space that needs to be replaced with a newline, which appears to be the case in your trial dataset, try the tr command. man tr.

I can't reproduce either issue, command applied to source data yields exactly target data required.
Pls. post a data set that produces the issues. BTW, is the space in front of the <new line> really required?

Thanks for the replies ! Will post the source data soon.
Space in front of the new line is not required

Hello
I modified the command from

sed 's/\([[:space:]]\)\(:61:\)/\1\n\2/g'  file1 > file2

to (remove space)

sed 's/\([[:space:]]\)\(:61:\)/\n\2/g'  file1 > file2

so the original issue I was facing got resolved but this change is giving me another issue

Source data :

 
:20:9400115251184711  :61:1509090908DR991,43NCOL09:04:0032 :86:/OCMT/EUR991,43/PT/FT/BN/ORANGE ESPAGNE S.A.U  

Target Data I am getting

 
:20:9400115251184711
:61:1509090908DR991,43NCOL09
:04:0032
:86:/OCMT/EUR991,43/PT/FT/BN/ORANGE ESPAGNE S.A.U 

Expected Target Data

 
:20:9400115251184711
:61:1509090908DR991,43NCOL09:04:0032
:86:/OCMT/EUR991,43/PT/FT/BN/ORANGE ESPAGNE S.A.U 

Really Appreciate all the help !!
Bob

works for me:

cat << EOF |
:20:9400115251184711  :61:1509090908DR991,43NCOL09:04:0032 :86:/OCMT/EUR991,43/PT/FT/BN/ORANGE ESPAGNE S.A.U
EOF
tr ' ' '\012' |
awk '/^:/{printf("\n%s", $1);next;}
    {printf(" %s", $1);}
    END{printf("\n");}
    ' |
grep .
1 Like

Thanks !! But could you suggest something with sed or some modification in my sed code!!

it's do your job ?

echo ' :20:9400115251184711  :61:1509090908DR991,43NCOL09:04:0032 :86:/OCMT/EUR991,43/PT/FT/BN/ORANGE ESPAGNE S.A.U' | sed 's/[[:space:]]:[0-9][0-9]:/\'$'\n&/g'
1 Like

Hello

This will not work as I need to only modify this for places where I find :61

Thanks !! This will not work as I only need to run this command where I find :61:

Applying your sed script to your sample data on my linux machine yields

:20:9400115251184711 
:61:1509090908DR991,43NCOL09:04:0032 :86:/OCMT/EUR991,43/PT/FT/BN/ORANGE ESPAGNE S.A.U

, and rightly so. One <newline> is inserted between every :61: and the space in front of it. I can't understand why you expect a <newline> before :86:

1 Like

If you got Perl, please, give it a try:

 perl -pe 's/(:61:.+?)\s+/\n$1\n/g and s/^\n|(\n)\n/$1/g' file1 > file2