LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 08-15-2011, 01:11 PM   #1
vinnar
LQ Newbie
 
Registered: Jul 2011
Posts: 5

Rep: Reputation: Disabled
Removing newline from only the last line of a file


Hi Guys,
I have a file myfile.dat which is having data in the following format -

Line1!##!\n
Line2!##!\n
Line3!##!\n
Line4!##!\n
Line5!##!\n

Adding \n to indicate newline after each line. I am trying to remove only the last '\n' and here is what I tried -

Code:
clear

NL=$'\n'
echo NL =  $NL
count=`wc -l myfile.dat | awk -F' ' '{print $1}'`
echo Count = $count
var=`tail -1 myfile.dat`
echo Var = $var

while read f
do
	if [ "$f" == "$var" ]; then
	echo Entered for loop
	echo ${f%!##!}
	#	f=${f/\n//}
		f=`${f%!##!}`
		echo Last line =  $f
	fi
done < myfile.dat
I am not able to get this work...

output -
==========
NL =
Count = 5
Var = Line5!##!
Entered for loop
Line5
./test.sh: line 16: Line5: command not found
Last line =

There is another post I found here that is discussing something similar, but manipulating it to make it work for my issue didnt help either...I have pasted below the modified command - http://www.linuxquestions.org/questi...t-line-677767/


Code:
sed -i -n -e '1,4p' -e '5,$H' -e '${g;s/\n//gp}' myfile.dat
Basically the goal is to be able to make the file have only 5 lines and not 6 - the program that eats this file thinks that since there is a carriage return at the end, there is a 6th line there and then ends up throwing an error because there is no data on that line. (Changing that program is not an option)

Any insight will be helpful.Thanks in advance.

-VN
 
Old 08-15-2011, 01:40 PM   #2
thesnow
Member
 
Registered: Nov 2010
Location: Minneapolis, MN
Distribution: Ubuntu, Red Hat, Mint
Posts: 170

Rep: Reputation: 56
sed '$s/\\n$//' myfile.dat
 
Old 08-15-2011, 02:13 PM   #3
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 23,066
Blog Entries: 11

Rep: Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910
Quote:
Originally Posted by thesnow View Post
sed '$s/\\n$//' myfile.dat
That doesn't actually work ...

Try
Code:
sed ':a;N;$!ba;s/\(.*\)\n/\1/' file

Cheers,
Tink
 
1 members found this post helpful.
Old 08-15-2011, 02:57 PM   #4
vinnar
LQ Newbie
 
Registered: Jul 2011
Posts: 5

Original Poster
Rep: Reputation: Disabled
Sorry, no that didnt help. Newline is still present.
 
Old 08-15-2011, 03:01 PM   #5
vinnar
LQ Newbie
 
Registered: Jul 2011
Posts: 5

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by Tinkster View Post
That doesn't actually work ...

Try
Code:
sed ':a;N;$!ba;s/\(.*\)\n/\1/' file

Cheers,
Tink
This did make a difference, but not the expected result should be - (and the last newline is still there. So this is like 5 lines now)

output-
========

Line1!##!
Line2!##!
Line3!##!
Line4!##!Line5!##!
 
Old 08-15-2011, 03:13 PM   #6
vinnar
LQ Newbie
 
Registered: Jul 2011
Posts: 5

Original Poster
Rep: Reputation: Disabled
Tinkster - came across this article - http://objectmix.com/awk/26812-sed-r...ew-line-2.html

The awk option suggested there did work and remove the last newline.
 
Old 08-15-2011, 03:15 PM   #7
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 23,066
Blog Entries: 11

Rep: Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910
Is this, by any chance, a DOS file, and instead of plain old
\n you have \n\l combos? I have no other explanation for the
behaviour you're seeing, as my snippet works as designed w/
the sample data above.

Code:
$cat five
NL =
Count = 5
Var = Line5!##!
Entered for loop
Line5

$
$cat five  |  wc -l
6
$sed ':a;N;$!ba;s/\(.*\)\n/\1/' five | wc -l
5
$sed ':a;N;$!ba;s/\(.*\)\n/\1/' five        
NL =
Count = 5
Var = Line5!##!
Entered for loop
Line5
$
Cheers,
Tink
 
Old 08-15-2011, 03:22 PM   #8
vinnar
LQ Newbie
 
Registered: Jul 2011
Posts: 5

Original Poster
Rep: Reputation: Disabled
File is probably generated by a windows bat script. In anycase, it moves through multiple machines (windows/linux) before it becomes available for me to process - so ya chances are that its a DOS file.

Link I posted earlier did solve my problem, but nevertheless, I will test out the possibility of \n\l as well and post if I find something interesting.

Thanks for the feedback.
 
Old 08-15-2011, 04:12 PM   #9
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957
Assuming you get the file into *nix line-ending format, here's an easy bash way to remove the final newline.
Code:
printf "$( <file )" >newfile
$(..) has a side effect of removing trailing newlines, so simply printf-ing it to a new file should do the trick. Don't forget to quote it, to avoid word splitting.

PS: Tip, cat -A will show you all non-printing characters in a text file. And there are several utilities out there for converting between line endings, such as tofrodos and flip. A simple sed one-liner can do the job as well: http://sed.sourceforge.net/sed1line.txt

Last edited by David the H.; 08-15-2011 at 04:17 PM.
 
1 members found this post helpful.
Old 08-15-2011, 05:23 PM   #10
crts
Senior Member
 
Registered: Jan 2010
Posts: 1,606

Rep: Reputation: 448Reputation: 448Reputation: 448Reputation: 448Reputation: 448
Hi,

so you basically just want to delete the last empty line. How about this:
Code:
sed '$d' file
This one will also transform it into unix format:
Code:
sed '$d;s/\r$//' file
[EDIT]
The above commands assume that there will always be a last empty line. If this is not the then the above commands should not be applied.
You could use
Code:
sed '$ {/^$/d}' file
instead. This will delete the last line only if it is empty.

Last edited by crts; 08-15-2011 at 05:31 PM.
 
Old 08-15-2011, 07:28 PM   #11
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.8, Centos 5.10
Posts: 17,240

Rep: Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324
I would start by using http://linux.die.net/man/1/dos2unix to convert to Linux line endings first ..
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Removing a line from file which is having specific pattern using shell script emcykm Linux - Newbie 5 03-21-2011 06:49 AM
[SOLVED] How to replace newline pattern in file by other newline pattern in a shell script XXLRay Linux - Software 9 11-29-2010 08:57 AM
[SOLVED] Removing line terminators from a plain text file. stf92 Linux - Software 11 07-31-2010 08:05 AM
Removing new line characters on every line execpt first line bioinformatics_guy Linux - Newbie 4 10-21-2008 01:41 PM
Removing newline. Tarts Programming 2 10-16-2003 12:49 PM


All times are GMT -5. The time now is 09:50 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration