Operating System - HP-UX
1757718 Members
2572 Online
108863 Solutions
New Discussion юеВ

stripping a non printable character from only last row of data only

 
SOLVED
Go to solution
Kevin Medlin
Occasional Contributor

stripping a non printable character from only last row of data only

I create an output file on UNIX. At the end of every row is a $. The file looks like this;

"var1","var2","var3"$
"var1","var2","var3"$
"var1","var2","var3"$
"var1","var2","var3"$

I can see the $ in vi when I use the command

:set list

I want to strip off the $ from the last row only. I want the above example to look like this;

"var1","var2","var3"$
"var1","var2","var3"$
"var1","var2","var3"$
"var1","var2","var3"

Does anyone know how to do this?
8 REPLIES 8
harry d brown jr
Honored Contributor

Re: stripping a non printable character from only last row of data only

kevin,

That's quite funny. You did a ":set list" which displays all control characters as extended characters, like ^F for ctrl-F and the $ (dollar sign) shows you where the end of the line is. The $ is not in the file.


live free or die
harry
Live Free or Die
Darrell Allen
Honored Contributor

Re: stripping a non printable character from only last row of data only

Hi Kevin,

Set list uses $ to show the end of the line. It is not a part of the data.

Use od to see what's actually in the file.

I presume this is in relation to your earlier post about loading a file into SQL Server. How about posting the results of:
tail -2 file | od -cb

Darrell
"What, Me Worry?" - Alfred E. Neuman (Mad Magazine)
Kevin Medlin
Occasional Contributor

Re: stripping a non printable character from only last row of data only

I ran

tail -1 file | od -cb

Here is the last section of the output;

" S E N I O
042 123 105 116 111 117
R C O N S U L T A N T " , " 0
122 040 103 117 116 123 125 114 124 101 116 124 042 054 042 060
3 0 8 7 " , " 0 " \n
060 070 067 042 054 042 060 042 012

Thanks for you help.
Darrell Allen
Honored Contributor

Re: stripping a non printable character from only last row of data only

Hi Kevin,

The last line of your file is:
"SENIORCONSULTANT","03087","0"

There is no $ at the end of the line. The only "unprintable" character is the newline (\n or octal 012) which delimits every line in a text file.

Your od output also shows there is no blank line at the end of the file.

Do you still have a problem with this file if you ftp it to Windows in ascii mode?

Darrell
"What, Me Worry?" - Alfred E. Neuman (Mad Magazine)
Kevin Medlin
Occasional Contributor

Re: stripping a non printable character from only last row of data only

Yes, the problem is the last \n on the last row. How can I remove it from the last row only?

I would want my od output to look like;

"var1","var2","var3"\n
"var1","var2","var3"\n
"var1","var2","var3"\n
"var1","var2","var3"

Any suggestions?
Darrell Allen
Honored Contributor
Solution

Re: stripping a non printable character from only last row of data only

Hi Kevin,

I made a suggestion in your other post:
http://forums.itrc.hp.com/cm/QuestionAnswer/1,,0xe57b50011d20d6118ff40090279cd0f9,00.html

It's not elegant but here it is again:

This is an ugly little script but it should work for you. At least it did on my test file of ~500 lines. The result is all lines print as is except the last which has the newline striped off.

while read line
do
echo "$line\c"
while read line
do
echo "\n$line\c"
done
done outfile

I'm sure there are better ways but...

Darrell
"What, Me Worry?" - Alfred E. Neuman (Mad Magazine)
Freddie C
Advisor

Re: stripping a non printable character from only last row of data only

How about this:
sed '$d' sourcefile > tmpfile
tail -1 sourcefile | tr -d \\012 >> tmpfile
H.Merijn Brand (procura
Honored Contributor

Re: stripping a non printable character from only last row of data only

perl -e '$/ = undef; $_ = <>; s/[\r\n]+$//; print' datafile >newfile
Enjoy, Have FUN! H.Merijn