Script for removing newline character from file

Hi below is my file.
cat input.dat

101,abhilash,1000
102,prave
en,2000
103,partha,4
000
10
4,naresh,5000

(its just a example file)

and my output should be:

101,abhilash,1000
102,praveen,2000
103,partha,4000
104,naresh,5000

below is my code

cat input.dat |tr -d '\n' > output_file
perl -ne 's/,/++$i % 3 ? "," : "\n"/ge; print' output_file > final_file

.

but this code is not giving exact result.

Let me know any perfect approach.

Thanks in advance.

Totally longhand using OSX 10.7.5, default bash terminal...
This works for your highly limited file information and assumes the last 3 characters of each line ends with 000

#!/bin/sh
# nl_err.sh
echo '101,abhilash,1000
102,prave
en,2000
103,partha,4
000
10
4,naresh,5000' > /tmp/sample1.txt
n=0
newtext=""
# Corrupt file...
cat < /tmp/sample1.txt
tr -d '\n' < /tmp/sample1.txt > /tmp/sample2.txt
# Flat file...
text=$(cat < /tmp/sample2.txt)
echo ""
echo "$text"
echo ""
echo "Start the corrections..."
echo ""
# Start of conversion...
while true
do
	if [ "${text:$n:3}" == "000" ]
	then
		newtext=$newtext${text:$n:3}$'\n'
		n=$[ ( $n + 3 ) ]
	else
		newtext=$newtext${text:$n:1}
		n=$[ ( $n + 1 ) ]
	fi
	if [ $n -gt ${#text} ]
	then
		break
	fi
done
# End of conversion...
printf "$newtext" > /tmp/sample3.txt
# Final file...
cat < /tmp/sample3.txt
exit 0

Results:-

Last login: Thu Apr 24 18:57:04 on ttys000
AMIGA:barrywalker~> chmod 755 nl_err.sh
AMIGA:barrywalker~> ./nl_err.sh
101,abhilash,1000
102,prave
en,2000
103,partha,4
000
10
4,naresh,5000

101,abhilash,1000102,praveen,2000103,partha,4000104,naresh,5000

Start the corrections...

101,abhilash,1000
102,praveen,2000
103,partha,4000
104,naresh,5000
AMIGA:barrywalker~> _
cat input.dat |tr -d '\n' | sed 's/000/000\n/g' > output_file

Previous solutions assume that the last field always has 000 .
The following solution compares the number of fields, of the current line and the previous line:

awk -F, '(pNF+NF-1==cols) {print p0 $0; pNF=0; p0=""; next} (pNF==cols) {print p0} {pNF=NF; p0=$0} END {if (pNF==cols) print p0}' cols=3 input.dat
1 Like

Thanks guys for your reply.
but there is no such limitation tht always last field ends with '000'.
its just an example.
the last field may be a description or alpha-numeric.

so can't go with such pattern.

"objective is every line should have 5 fields."

every line having 5 fileds is fine...but for the below data, how can we assume if the '10' in the 3rd line goes to above line or below?

103,partha,4
000
10
4,naresh,5000
1 Like

Thanks "MadeInGermany" dude.
Its working perfect.
i had chekcd for multiple scenarios and its given perfect results.

one small additional requirement.
if line is devided into morethan 2 lines then its eliminating 1st line.
letme show u an example.

actual line:

1,abhilash,Trainy,India

in the source file its exist as:

1,abhilash,Tra
iny,I
ndia

with above code the result shown as

[E20044773]:TgtFiles> awk -F, '(pNF+NF-1==cols) {print p0 $0; pNF=0; p0=""; next} (pNF==cols) {print p0} {pNF=NF; p0=$0} END {if (pNF==cols) print p0}' cols=4 input.dat
 
1,abhilash,Trainy,I

any suggestions pls?

Thanks in advance

---------- Post updated at 12:52 AM ---------- Previous update was at 12:49 AM ----------

---------- Post updated at 03:15 AM ---------- Previous update was at 12:52 AM ----------

Hi Srini , i got your point but i could not see that issue with below code.

awk -F� '(pNF+NF-1==cols) {print p0 $0; pNF=0; p0=""; next} (pNF==cols) {print p0} {pNF=NF; p0=$0} END {if (pNF==cols) print p0}' cols=24 input.dat

now the issue is if the line splited into more than 2 lines then its only merging current and just previous line. not to all .
example i shown in previous thread.

can u help here?