LinuxQuestions.org
Register a domain and help support LQ
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, 11: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, 11:13 AM   #2
Sefyir
Member
 
Registered: Mar 2015
Distribution: Linux Mint
Posts: 385

Rep: Reputation: 158Reputation: 158
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, 11:15 AM   #3
Habitual
LQ Addict
 
Registered: Jan 2011
Location: Youngstown, Ohio
Distribution: LM17.1/Xfce4.11.8
Posts: 7,159
Blog Entries: 10

Rep: Reputation: 1968Reputation: 1968Reputation: 1968Reputation: 1968Reputation: 1968Reputation: 1968Reputation: 1968Reputation: 1968Reputation: 1968Reputation: 1968Reputation: 1968
Try
Code:
sed '/^$/d' file
And Welcome to LQ!
 
Old 11-18-2015, 11: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, 11:28 AM   #5
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,243

Rep: Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684
Please try and do some of the work. How would you normally redirect data to another file?
 
Old 11-18-2015, 11: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, 11:56 AM   #7
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,243

Rep: Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684
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, 12:06 PM   #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, 12:17 PM   #9
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,243

Rep: Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684
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, 02:30 PM   #10
Habitual
LQ Addict
 
Registered: Jan 2011
Location: Youngstown, Ohio
Distribution: LM17.1/Xfce4.11.8
Posts: 7,159
Blog Entries: 10

Rep: Reputation: 1968Reputation: 1968Reputation: 1968Reputation: 1968Reputation: 1968Reputation: 1968Reputation: 1968Reputation: 1968Reputation: 1968Reputation: 1968Reputation: 1968
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, 01:08 AM   #11
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,243

Rep: Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684
Yes, not winning my vote as someone who should continue to receive help
 
Old 11-19-2015, 12:10 PM   #12
DavidMcCann
Senior Member
 
Registered: Jul 2006
Location: London
Distribution: CentOS, Salix
Posts: 4,160

Rep: Reputation: 1223Reputation: 1223Reputation: 1223Reputation: 1223Reputation: 1223Reputation: 1223Reputation: 1223Reputation: 1223Reputation: 1223
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, 01:10 PM   #13
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,243

Rep: Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684
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 07:48 AM
cut first 10 lines of file master.txt and paste in ab1.txt and so on yogeshkumkar Programming 4 08-31-2011 08:23 AM
[SOLVED] Removing blank lines and commentary from a config file rgdacosta Linux - Newbie 19 08-19-2010 07:31 AM
removing blank lines in a text file christianunix Linux - Newbie 11 10-29-2007 01:24 AM
Replace blank/almost blank lines in file Wynd Linux - General 3 01-27-2004 05:49 PM


All times are GMT -5. The time now is 12:13 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