Replace space by newline error


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Replace space by newline error
# 1  
Old 08-20-2015
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

Code:
 
Source Data :
:61:D100,74NCH1 :61:D797,50NCH2 :61:D89,38NCHK2 :61:D99,38NCHK12 :61:D79,38NCHK22 :61:D29,38NCHK5

Code:
 
Target Data required :
:61:D100,74NCH1 
:61:D797,50NCH2 
:61:D89,38NCHK2
:61:D99,38NCHK12 
:61:D79,38NCHK22 
:61:D29,38NCHK5

Code:
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

Last edited by kamijia83; 08-20-2015 at 04:14 PM..
# 2  
Old 08-20-2015
Code:
$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.

Last edited by featheredfrog; 08-20-2015 at 05:21 PM.. Reason: I can't keep my mouth shut, okay?
# 3  
Old 08-20-2015
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?
# 4  
Old 08-20-2015
Thanks for the replies ! Will post the source data soon.
Space in front of the new line is not required
# 5  
Old 09-09-2015
Hello
I modified the command from

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

to (remove space)
Code:
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 :
Code:
 
:20:9400115251184711  :61:1509090908DR991,43NCOL09:04:0032 :86:/OCMT/EUR991,43/PT/FT/BN/ORANGE ESPAGNE S.A.U

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

Expected Target Data
Code:
 
: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
# 6  
Old 09-09-2015
works for me:
Code:
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 .

This User Gave Thanks to quirkasaurus For This Post:
# 7  
Old 09-09-2015
Thanks !! But could you suggest something with sed or some modification in my sed code!!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Programming

Concatenate integer and space/newline for cout in C++

I'm trying to print out integers and space/newline for a nicer output, for example, every 20 integers in a row with ternary operator. In C I could do it with:printf("%d%s",tmp_int, ((j+1)%20) ? "\t":"\n"); but could not figure out the equivalent in C++: cout << ((j+1)%20)?... (4 Replies)
Discussion started by: yifangt
4 Replies

2. UNIX for Dummies Questions & Answers

Replace the unexpected newline char with space in a Fixed width file

Input eg: Ouput Expected. The #rd line had the unexpted new line, which need to be replaced with space. I was planing to go with checking the length of each line using awk and if the length is less than the defeined limit, (12 in above case) will replace the newline with space. ... (5 Replies)
Discussion started by: deepakwins
5 Replies

3. Shell Programming and Scripting

Replace 3rd occurance of SPACE with newline

I have file with SQL output as 0001 firstname1 lastname1 0002 firstname2 lastname2 0003 firstname3 lastname3 0004 firstname4 lastname4 Expected output : 0001 firstname1 lastname1 0002 firstname2 lastname2 0003 firstname3 lastname3 0004 firstname4 lastname4 Let me know if this can... (9 Replies)
Discussion started by: sameermohite
9 Replies

4. Shell Programming and Scripting

Newline to space code removes 0 from the first line

I am having a peculiar problem. First I run the code below to append 0 at the start of each line in some hundreds of files that I have in a directory. These files have each word in a newline. for f in *.dat; do echo "0" > tmpfile cat $f >> tmpfile mv tmpfile $f done Then I run this... (7 Replies)
Discussion started by: shoaibjameel123
7 Replies

5. Shell Programming and Scripting

Replace newline character between a double quotes to a space

Hi Guys, I have a file with content as below aj.txt "Iam allfine" abcdef abcd "all is not well" What I'm trying to say is my data has some new line characters in between quoted text. I must get ride of the newline character that comes in between the quoted text. output must be:... (8 Replies)
Discussion started by: ajahuja
8 Replies

6. UNIX for Dummies Questions & Answers

Remove newline & space

Can someone help me on this. I have a file that has a long line just like below. The long line keeps on being truncated to the next line (new line + space) for some reason. Basically, I just need to remove this problem. Hope somebody can help! Thanks! INPUT FILE: structuralObjectClass:... (4 Replies)
Discussion started by: Orbix
4 Replies

7. Shell Programming and Scripting

Using sed I want to replace space by newline

Input: -------------------------- 123asd 456sdasda 789a ------------------------- output wanted: --------------------- 123asd 456sdasda 789a ---------------------- I want this by sed in simple way please help (I know by: tr ' ' '\n' < inputfile )I want it by sed only (5 Replies)
Discussion started by: RahulJoshi
5 Replies

8. Shell Programming and Scripting

replace >< with > newline <

Hi All, I have the command in PERL for performing this, but Can you please suggest me how can i perform this using AWK: My input xml file looks like this: <aaa>hello</aaa><bbb>hai</bbb> I want the output like this ( means need new line after end of each xml tag): <aaa>hello</aaa>... (1 Reply)
Discussion started by: HemaV
1 Replies

9. Shell Programming and Scripting

Breaking long lines into (characters, newline, space) groups

Hello, I am currently trying to edit an ldif file. The ldif specification states that a newline followed by a space indicates the subsequent line is a continuation of the line. So, in order to search and replace properly and edit the file, I open the file in textwrangler, search for "\r " and... (14 Replies)
Discussion started by: rowie718
14 Replies

10. UNIX for Advanced & Expert Users

newline character, space and tab after a string

no problem (6 Replies)
Discussion started by: angelina
6 Replies
Login or Register to Ask a Question