LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Removing blank lines from a txt file (https://www.linuxquestions.org/questions/linux-newbie-8/removing-blank-lines-from-a-txt-file-4175559202/)

conrog66 11-18-2015 10:12 AM

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

Sefyir 11-18-2015 10:13 AM

Did you search it?

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

http://stackoverflow.com/questions/1...om-a-unix-file

Habitual 11-18-2015 10:15 AM

Try
Code:

sed '/^$/d' file
And Welcome to LQ!

conrog66 11-18-2015 10:20 AM

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

grail 11-18-2015 10:28 AM

Please try and do some of the work. How would you normally redirect data to another file?

conrog66 11-18-2015 10:47 AM

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?

grail 11-18-2015 10:56 AM

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.

conrog66 11-18-2015 11:06 AM

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

grail 11-18-2015 11:17 AM

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?

Habitual 11-18-2015 01:30 PM

Quote:

Originally Posted by conrog66 (Post 5451585)
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.

grail 11-19-2015 12:08 AM

Yes, not winning my vote as someone who should continue to receive help

DavidMcCann 11-19-2015 11:10 AM

Quote:

Originally Posted by grail (Post 5451894)
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.

grail 11-19-2015 12:10 PM

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.


All times are GMT -5. The time now is 08:25 PM.