Replace \n char in Data

Tags
advanced, char, dat, data, ed, input, line, put, replace, \n

 
Thread Tools Search this Thread
# 1  
Old 11-20-2018
Replace \n char in Data

File is pipe delimited with 17 fields. We may get \n char (1 or more \n in one field or multi fileds) in data in any field.
Need to replace \n in data with space and not the Ture \n that is line separator.

I tried below awk command it did not work as expected.
Code:
awk '(NR-1)%2{$1=$1}1' RS=\| ORS=\| TestInput |egrep -v "^\|$">TestOUT

Input:
Code:
455000|TTTT|97233|UUUUUU REP||Juli||EEEE||P.O. Box 550 MMMMMMMMMM JJ 55555|||||||
333333|DDD|97233|UUUUUU REP||AMAR||AJAY||P.O. Box 69 MMMMMMMMMM JJ 6666 
JJJ BBBB P.O. Box 4 MMMMMMMMMM JJ 44444
delmer Speidel P.O. Box 242 MMMMMMMMMM JJ 99456, See File For More.....|||||||
888888|Director|97382|UUUUUU REP||ANTHONY|K|JOSHI||1144 JNM ROAD LLLLLLLLLLLLL JJ 82513, Laurie Ideker, Leon Sanderson
coralie Emmons P.O. Box 34 LLLLLLLLLLLLL JJ 82513
wanda Knowles P.O. Box 958 LLLLLLLLLLLLL JJ 82513, See File For More...|||||||
999999|President|97692|UUUUUU REP||See||File|||||||||

Expected Output:
Code:
455000|TTTT|97233|UUUUUU REP||Juli||Surwald||P.O. Box 550 MMMMMMMMMM JJ 55555|||||||
333333|DDD|97233|UUUUUU REP||AMAR||AJAY||P.O. Box 69 MMMMMMMMMM JJ 6666 JJJ BBBB P.O. Box 4 MMMMMMMMMM JJ 99456 delmer Speidel P.O. Box 242 MMMMMMMMMM JJ 99456, See File For More.....|||||||
888888|Director|97382|UUUUUU REP||ANTHONY|K|JOSHI||1144 JNM ROAD LLLLLLLLLLLLL JJ 82513, Laurie Ideker, Leon Sanderson coralie Emmons P.O. Box 34 LLLLLLLLLLLLL JJ 82513 wanda Knowles P.O. Box 958 LLLLLLLLLLLLL JJ 82513, See File For More...|||||||
999999|President|97692|UUUUUU REP||See||File|||||||||


Last edited by Scrutinizer; 11-20-2018 at 12:07 PM.. Reason: code tags
# 2  
Old 11-20-2018
Code:
awk -F'|' 'NF==17 {print;next}
             {s=(s)?s $0:$0
               if (split(s,a)==17) {print s;s=""}
             } 
             END {if (s) print s}' myFile

# 3  
Old 11-20-2018
Hmmm - how (by which algorithm / rule) has the EEEE in the first input line turned into Surwald in the expected Output? And how the third line's 44444 into 99456?


Try also
Code:
awk -F'|' '
        {while (NF<17)  {getline X
                         $0 = $0 " " X
                        }
        }
1
' file

# 4  
Old 11-21-2018
Replace \n char in Data

Thanks Rudi C,

Its my bad.

Expected output:

Code:
455000|TTTT|97233|UUUUUU REP||Juli||EEEE||P.O. Box 550 MMMMMMMMMM JJ 55555|||||||
333333|DDD|97233|UUUUUU REP||AMAR||AJAY||P.O. Box 69 MMMMMMMMMM JJ 6666 JJJ BBBB P.O. Box 4 MMMMMMMMMM JJ 44444 delmer Speidel P.O. Box 242 MMMMMMMMMM JJ 99456, See File For More.....|||||||
888888|Director|97382|UUUUUU REP||ANTHONY|K|JOSHI||1144 JNM ROAD LLLLLLLLLLLLL JJ 82513, Laurie Ideker, Leon Sanderson coralie Emmons P.O. Box 34 LLLLLLLLLLLLL JJ 82513 wanda Knowles P.O. Box 958 LLLLLLLLLLLLL JJ 82513, See File For More...|||||||
999999|President|97692|UUUUUU REP||See||File|||||||||

------ Post updated 11-21-18 at 11:24 AM ------

Thanks a lot Rudi C,

1)One more thing is in my file I may get Extra pipes(|) also( other than Field delimeter | ) then how to handle
2)Can u please explain me your command

Last edited by vgersh99; 11-20-2018 at 01:20 PM.. Reason: Code tags, please!
# 5  
Old 11-21-2018
1) If you tell us how to tell separator pipes from in-field-pipes, then someone could come up with some smart algorithm to handle that.
2) That little command keeps reading / appending new lines until the field count is 17; then: print (default action after "1" (= TRUE)).
# 6  
Old 11-22-2018
1)I'm not sure how to identify data pipes, but if we get | in 2 or 3 specific fields like Address and other.
So can we handle ?
# 7  
Old 11-22-2018
If your field delimiter is sometimes a field delimiter and sometimes data, you need to be able to very clearly identify each occurrence of that character as either data or delimiter. If you can't specify a clear rule that unambiguously determines whether a given character is a delimiter or data, there is no way to identify field boundaries.

And when you have field delimiters that are sometimes data AND record delimiters that are sometimes data, you have a real mess.

Your best choice would be to choose a different field delimiter that cannot ever appear as data.

|
Thread Tools Search this Thread
Search this Thread:
Advanced Search

More UNIX and Linux Forum Topics You Might Find Helpful
Replace \n char true line Separator rajeshkumare UNIX for Advanced & Expert Users 1 11-22-2018 07:30 AM
Replace the unexpected newline char with space in a Fixed width file deepakwins UNIX for Dummies Questions & Answers 5 08-08-2014 06:26 PM
Match a char with duplicates in a line and replace one of them redse171 Shell Programming and Scripting 5 05-22-2014 02:48 PM
Replace Char in XML Base on Condition asavaliya Shell Programming and Scripting 4 03-20-2014 01:15 PM
To find char field and replace null rohit_shinez Shell Programming and Scripting 5 08-07-2013 02:21 PM
Replace char between chars - help needed go0ogl3 Shell Programming and Scripting 10 07-07-2012 10:57 AM
Find and replace all extended char. asavaliya Shell Programming and Scripting 4 04-10-2012 02:19 PM
I don't know how to replace input char with appropriate integer solaris_user Programming 6 10-09-2011 11:19 PM
UNIX BASH replace char with dash w/o SED needsomehelp UNIX for Dummies Questions & Answers 11 10-08-2011 09:47 PM
Replace char on text file phillipss Shell Programming and Scripting 2 12-11-2010 01:19 AM
In vi editor I want to replace next line char by space RahulJoshi Shell Programming and Scripting 5 09-17-2010 05:44 AM
Special Char in Data file Amit.Sagpariya Shell Programming and Scripting 1 01-14-2010 06:05 PM
SED: Place char at starting and replace selected line hkansal Shell Programming and Scripting 2 06-11-2009 03:42 AM
How to replace any char with newline char. mightysam Shell Programming and Scripting 5 09-18-2008 09:15 PM
How to check a column contain numeric or char data type ?? jambesh Shell Programming and Scripting 12 10-06-2006 11:37 AM