Tried many options but unable to delete blank lines from text file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Tried many options but unable to delete blank lines from text file
# 8  
Old 07-24-2017
Quote:
Originally Posted by durden_tyler
(*)
As for why it doesn't work the way it looks - you have two different processes (a) a read process and (b) a write process, both sharing the same resource - your file. When they are executed by the OS, a race condition occurs between them which renders the final output unpredictable.
If the writing is done before the reading, you get a zero-byte file. If the writing is done mid-way during the reading process, you may get a "half-baked" output file. The safest way, therefore, is to either use a temporary file explicitly or let sed do it via the "-i" modifier - which is not available in all sed versions.
You could also use other tools - Bash, awk, Perl etc. but the temporary file will be involved, either explicitly or implicitly.
Hello durden_tyler/uuuunnnn,

That tr command worked for me. Also && condition will make sure like first reading of Input_file and it's output's redirection is done then only it will go to next command which is mv temp_Input_file Input_file, ideally it shouldn't fail. Kindly do let me know your views on same.

Thanks,
R. Singh
# 9  
Old 07-24-2017
Quote:
Originally Posted by RavinderSingh13
...
That tr command worked for me. Also && condition will make sure like first reading of Input_file and it's output's redirection is done then only it will go to next command which is mv temp_Input_file Input_file, ideally it shouldn't fail. Kindly do let me know your views on same.
...
...
The explanation was for the part in red in my post; I thought the post was clear about the erroneous part.
This User Gave Thanks to durden_tyler For This Post:
# 10  
Old 07-24-2017
Thanks everyone.

Yes the issue was that I was copying the values from windows to unix and carriage return was entered. As suggested I entered the values in unix and used

Code:
sed '/^$/d' temp.hash.txt > temp.hash.out

It worked fine.

Thanks
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Advanced & Expert Users

Delete blank spaces and blank lines in a file

Hi Gurus, Somebody can say me how to delete blank spaces and blank lines in a file unix, please. Thank you for advanced. (10 Replies)
Discussion started by: systemoper
10 Replies

2. Shell Programming and Scripting

Delete blank lines in a file

Hi All, I have a file and I need to delete the lines that are blank and is starting with some characters below. Something like below: Regular Ascii File: Line1: AGODA1 BUSAN||SK Lord Beach 4/6/2012 4/7/2012 68060 Line2: AGODA2 BUSAN||SK Beach Hotel 4/6/2012 4/7/2012 610200 Line3: ... (4 Replies)
Discussion started by: rkumar28
4 Replies

3. Shell Programming and Scripting

Delete the last empty/blank line of the text file

Hi All, I have a file.txt which seems like having three lines. wc -l file.txt 3 file.txt In fact, once it is open in text editor, this file has four lines where the last line is empty. how can i delete this last empty line of the file.txt? I tried the codes below so far but they... (6 Replies)
Discussion started by: senayasma
6 Replies

4. Shell Programming and Scripting

Delete blank lines, if blank lines are more than one using shell

Hi, Consider a file named "testfile" The contents of file are as below first line added for test second line added for test third line added for test fourth line added for test fifth line added for test (5 Replies)
Discussion started by: anil8103
5 Replies

5. Shell Programming and Scripting

Delete blank lines from a file

Hi, I want to use diff to compare two files in a Perl file. But one of the files has some blank lines at the end. So I want to delete the blank lines from the file firstly and then use diff to compare them. But I dont know how to delete the blank lines from the files. Meanwhile, the system is... (5 Replies)
Discussion started by: Damon_Qu
5 Replies

6. UNIX for Dummies Questions & Answers

Remove blank lines and comments from text file

Hi, I am using BASH. How can I remove any lines in a text file that are either blank or begin with a # (ie. comments)? Thanks in advance. Mike (3 Replies)
Discussion started by: msb65
3 Replies

7. Shell Programming and Scripting

Delete a block of text delimited by blank lines when pattern is found

I have a file which contains blocks of text - each block is a multi-lines text delimited by blank lines eg. <blank line> several lines of text ... pattern found on this line several more lines of text ... <blank line> How do you delete the block of text (including the blank lines) when... (17 Replies)
Discussion started by: gleu
17 Replies

8. UNIX for Dummies Questions & Answers

delete blank lines from a file

can anyone show me how to delete blank lines from a file. thanks in advance (2 Replies)
Discussion started by: sachin.gangadha
2 Replies

9. Shell Programming and Scripting

Delete blank lines at the end of file

I am attempting to delete blank lines in my file and I've used this command: sed '/^$/d' $file > $file.fixed all this seems to do is copy the file and not delete the blank lines located at the end of the file. Any assistance would be greatly appreciated. (3 Replies)
Discussion started by: TL56
3 Replies

10. Shell Programming and Scripting

regex to delete multiple blank lines in a file?

can't figure out a way to delete multiple empty lines but keep single empty lines in a file, file is like this #cat file 1 2 3 4 5 6 - What I want is 1 2 (6 Replies)
Discussion started by: fedora
6 Replies
Login or Register to Ask a Question