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