Remove Blank Rows


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Remove Blank Rows
# 1  
Old 12-07-2010
Remove Blank Rows

Hello All, I am having a problem in automating my UNIX Script to remove the blank spaces in between records. When I incorporate it into my script, the output file is deleting the whole content Smilie

Basically this is what I am trying to do:
File contains data like this:
Code:
12314213423,Georgia,123yh1

12987613423,Anaheim,167yu4

13399887123,Osaka,592zl8

and it needs to be like this:
Code:
12314213423,Georgia,123yh1
12987613423,Anaheim,167yu4
13399887123,Osaka,592zl8

This is the code I incorporated in the script:
Code:
tr -d ' ' <tmscadtrans> tmscadtranstemp1 --> to remove the spaces in between
grep -v "^$" tmscadtemp1> tmscadtranstemp
grep -v "^$" tmscadtranstemp> tmscadtrans --> to remove the blank line at the EOF.

The commands above worked if ran in command prompt but when I run the script it does not work Smilie and I do not know what is wrong

Last edited by Scott; 12-07-2010 at 10:02 AM.. Reason: Please use code tags
# 2  
Old 12-07-2010
Code:
sed 'g/^ *$/d' inpFile

or

Code:
grep -v "^ *$" inpFile

R0H0N
# 3  
Old 12-07-2010
Hi,

Using perl:
Code:
$ perl -lne 'print unless /^\s*$/' infile

Regards,
Birei
# 4  
Old 12-07-2010
Code:
awk '!/^$/' file

or in that case:
Code:
awk '/,/' file


Last edited by cabrao; 12-07-2010 at 06:16 AM..
# 5  
Old 12-07-2010
This removes both empty lines and lines with only whitespace
Code:
awk NF file

This User Gave Thanks to Scrutinizer For This Post:
# 6  
Old 12-07-2010
I got an error when I ran my scripts using
Code:
sed 'g/^ *$/d'

Code:
sed: Function g/^ *$/d cannot be parsed.

But when I double checked my code, it was enclosed in single quotes.

Here is the code:
Code:
src=/var/opt/sapbi/TV/scripts/$1
echo $src
sed 'g/^ *$/d' $src

I tried
grep -v "^ *$" $src but it did not work.
I tried the awk commands but nothing happened to the file.

---------- Post updated at 08:32 AM ---------- Previous update was at 08:30 AM ----------

Just wondering, is there an alternative to Gunzip -f <InpFile>? Because upon checking, when the file is gunzipped, it creates those white spaces in between records. When I try to extract using 7zip, it does not add the white spaces.

Last edited by Scott; 12-07-2010 at 10:04 AM.. Reason: Code tags
# 7  
Old 12-07-2010
If non of these work, perhaps there is something invisible on those lines. Run them through od -c to check. Perhaps a CR return character, because the file originated from a Windows platform?
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Converting columns of text to rows, with blank lines

I've spent the past hour trying different things and googling for this solution and cannot find the answer. Found variations of this, but not this exact thing. I have the following text, which is the output from our mainframe. Each field is on a separate line, with a blank line between each... (7 Replies)
Discussion started by: lupin..the..3rd
7 Replies

2. Shell Programming and Scripting

Need to remove a selection of rows separated by blank lines

hello, here is an example: 9.07 9.05 0.00 2.28 0.00 0.08 1.93 3.62 10.97 12.03 12.03 0.00 2.73 0.00 0.07 (3 Replies)
Discussion started by: Baron1
3 Replies

3. Shell Programming and Scripting

Perl : blank lines are displayed in the output after deleting few rows from excel

I am working on an assignment to pull all the records from excel sheet programatically and use the data for further calculations. In this process, I first defined 10 records in excel sheet and executed the below code. In the first run it is OK. But after deleting last few rows in excel sheet and... (0 Replies)
Discussion started by: giridhar276
0 Replies

4. Shell Programming and Scripting

Copying down first row in to all the below blank rows in a .csv file

Hi All, I have many of files(.csv) of the format given below. Date,Name,Location 04/02/2012,A,India ,B,China ,C,USA Like this I have 1000's of rows and many columns in all my files. I need a shell script to copy down the Date(in this example column1) to the next 2 rows below(in the... (8 Replies)
Discussion started by: ks_reddy
8 Replies

5. Shell Programming and Scripting

How to remove blank lines

Hi, I am facing a problem related to removing blank lines from a text document. Input Error 17-05-2011 11:01:15 VisualSVN Server 2.1 1001 The following information was included with the event: line3 line4 Error 17-05-2011 11:00:25 VisualSVN Server 2.1 ... (13 Replies)
Discussion started by: mayursingru
13 Replies

6. Shell Programming and Scripting

How to remove Blank rows in a csv file

Hi, I need help to remove blank rows at the end of file. Sample data: "Oslo, Symra kino",Oslo,130-7,Symra 1,130-7-91 "Tønsberg, Brygga Kino SF",Tønsberg,202-1,Tønsberg SF 4,202-1-4 ,,,, ,,,, ,,,, ,,,, Expected data: "Oslo, Symra kino",Oslo,130-7,Symra 1,130-7-91 "Tønsberg, Brygga... (6 Replies)
Discussion started by: cnraja
6 Replies

7. Shell Programming and Scripting

Remove 1st two rows and last 2 rows

Hi All, I need to remove 1st 2 line from head and last 2 line from last. I thought it would be possible by using the Head and tail command. But after i am using it is not possible by it. Example:Input file 1 2 3 4 5 Example: Output file 3 But my head and tail command are not... (12 Replies)
Discussion started by: kam786sim
12 Replies

8. Shell Programming and Scripting

how to delete blank rows in a log file

Help How to delete all blank rows in log file (4 Replies)
Discussion started by: suryanarayana
4 Replies

9. UNIX for Dummies Questions & Answers

Remove blank lines

¿How can i remove blank lines between all lines in a long text file? Example WrongFile.txt : Line 1 Line 2 Line 3 CorrectFile.txt : Line 1 Line 2 Line 3 Thanks in advance :confused: (4 Replies)
Discussion started by: osymad
4 Replies

10. Solaris

finding & replacing blank rows/spaces in a file

Can anyone help me find and replace blank rows in a file with a numeric value (ie blankrow=someTxtOrNumValue), the file is over 500,000 rows long so it would need to be the quickest way as I'll need to do this for multiple files...I would be greatfull for any suggestions....thanks sample file:... (2 Replies)
Discussion started by: Gerry405
2 Replies
Login or Register to Ask a Question