LinuxQuestions.org
Help answer threads with 0 replies.
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 11-18-2015, 10:12 AM   #1
conrog66
LQ Newbie
 
Registered: Nov 2015
Posts: 5

Rep: Reputation: Disabled
Question Removing blank lines from a txt file


Hi all,

I was just wondering if there was a way to remove blank lines from a .txt file.

I have been using the DHT22 sensor on my raspberry pi to collect temperature and humidity data.

The data collected is stored in the txt file under the name 'my_data.txt'. However, between each recording there is a blank line.

Is there a code which will allow me to move this data onto another txt file but without the blank lines in between?

Thanks, Connor
 
Old 11-18-2015, 10:13 AM   #2
Sefyir
Member
 
Registered: Mar 2015
Distribution: Linux Mint
Posts: 634

Rep: Reputation: 316Reputation: 316Reputation: 316Reputation: 316
Did you search it?

https://duckduckgo.com/?t=lm&q=remov...rom+file+linux

http://stackoverflow.com/questions/1...om-a-unix-file
 
Old 11-18-2015, 10:15 AM   #3
Habitual
LQ Veteran
 
Registered: Jan 2011
Location: Abingdon, VA
Distribution: Catalina
Posts: 9,374
Blog Entries: 37

Rep: Reputation: Disabled
Try
Code:
sed '/^$/d' file
And Welcome to LQ!
 
Old 11-18-2015, 10:20 AM   #4
conrog66
LQ Newbie
 
Registered: Nov 2015
Posts: 5

Original Poster
Rep: Reputation: Disabled
Thanks! How would I use that code in order to move the data without the blank lines, into a new txt file called something like my_data2.txt?

Sorry to be a pain
 
Old 11-18-2015, 10:28 AM   #5
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,996

Rep: Reputation: 3187Reputation: 3187Reputation: 3187Reputation: 3187Reputation: 3187Reputation: 3187Reputation: 3187Reputation: 3187Reputation: 3187Reputation: 3187Reputation: 3187
Please try and do some of the work. How would you normally redirect data to another file?
 
Old 11-18-2015, 10:47 AM   #6
conrog66
LQ Newbie
 
Registered: Nov 2015
Posts: 5

Original Poster
Rep: Reputation: Disabled
When I try to enter my code a get a syntax error stating there's something wrong with this line: sed '/^$/d' my_data.txt

What could be the reason for this?
 
Old 11-18-2015, 10:56 AM   #7
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,996

Rep: Reputation: 3187Reputation: 3187Reputation: 3187Reputation: 3187Reputation: 3187Reputation: 3187Reputation: 3187Reputation: 3187Reputation: 3187Reputation: 3187Reputation: 3187
So to confirm, you are at a command prompt and typing the line shown?:
Code:
$ sed '/^$/d' my_data.txt
What is the exact message?

Also, is the file from Windows? If so you may need to run it through dos2unix prior to running linux tools on it.
 
Old 11-18-2015, 11:06 AM   #8
conrog66
LQ Newbie
 
Registered: Nov 2015
Posts: 5

Original Poster
Rep: Reputation: Disabled
The exact message is-

File "edit_script.py", line 3
Sed '/^$/d' my_data(copy).txt
^
SyntaxError: invalid syntax

I am writing the script in leafpad on raspberry pi
 
Old 11-18-2015, 11:17 AM   #9
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,996

Rep: Reputation: 3187Reputation: 3187Reputation: 3187Reputation: 3187Reputation: 3187Reputation: 3187Reputation: 3187Reputation: 3187Reputation: 3187Reputation: 3187Reputation: 3187
sed is lower case for a start. Also, this seems to be part of a python script, not only is this vital information but you cannot directly call sed from within python without using other commands.

lastly, you are writing the script in leafpad, but what was the txt file written with?
 
Old 11-18-2015, 01:30 PM   #10
Habitual
LQ Veteran
 
Registered: Jan 2011
Location: Abingdon, VA
Distribution: Catalina
Posts: 9,374
Blog Entries: 37

Rep: Reputation: Disabled
Quote:
Originally Posted by conrog66 View Post
Hello,

I am trying to remove blank lines from a txt.file. I have googled a potential solution which is using- sed -e /^$/d my_data.txt.
Wow. Check out the stones on him.
 
Old 11-19-2015, 12:08 AM   #11
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,996

Rep: Reputation: 3187Reputation: 3187Reputation: 3187Reputation: 3187Reputation: 3187Reputation: 3187Reputation: 3187Reputation: 3187Reputation: 3187Reputation: 3187Reputation: 3187
Yes, not winning my vote as someone who should continue to receive help
 
Old 11-19-2015, 11:10 AM   #12
DavidMcCann
LQ Veteran
 
Registered: Jul 2006
Location: London
Distribution: PCLinuxOS, Debian
Posts: 6,130

Rep: Reputation: 2302Reputation: 2302Reputation: 2302Reputation: 2302Reputation: 2302Reputation: 2302Reputation: 2302Reputation: 2302Reputation: 2302Reputation: 2302Reputation: 2302
Quote:
Originally Posted by grail View Post
Yes, not winning my vote as someone who should continue to receive help
And you were capable of understanding sed and regular expressions when you started Linux? After 15 years I've still never used either; luckily I can use an emulator to run my faithful QL text editor to do jobs like removing CR or blank lines.
 
Old 11-19-2015, 12:10 PM   #13
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,996

Rep: Reputation: 3187Reputation: 3187Reputation: 3187Reputation: 3187Reputation: 3187Reputation: 3187Reputation: 3187Reputation: 3187Reputation: 3187Reputation: 3187Reputation: 3187
I think you misunderstood David. I am not against helping the individual, but he has opened a second thread and claimed he came up with the sed shown by searching Google, see here and not only is this a blatant fib but it also goes against the LQ rules of reposting the same question.

So no, I am with you on the fact that this is not necessarily an easy topic for a beginner, but I am not impressed when someone tries to usurp our generous nature either.
 
  


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] Perl: how to replace blank lines in a file with given lines from another karamaz0v Programming 8 04-19-2012 06:48 AM
cut first 10 lines of file master.txt and paste in ab1.txt and so on yogeshkumkar Programming 4 08-31-2011 07:23 AM
[SOLVED] Removing blank lines and commentary from a config file rgdacosta Linux - Newbie 19 08-19-2010 06:31 AM
removing blank lines in a text file christianunix Linux - Newbie 11 10-29-2007 12:24 AM
Replace blank/almost blank lines in file Wynd Linux - General 3 01-27-2004 04:49 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 03:09 AM.

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
Open Source Consulting | Domain Registration