how to get rid of blank line in a flat text file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting how to get rid of blank line in a flat text file
# 8  
Old 05-27-2003
Here's a streamlined awk version:
Code:
awk '/^E/ {next}
NF > 0'

The default action in awk is print, so a pattern with no action just prints the line. The first pattern/action pair skips lines that start with "E". Since awk does an autosplit into $1, $2, ... on each input line after trimming leading and trailing whitespace (with the default delimiter /[ \t]+/), if there are only spaces, there are no fields, i.e., NF == 0 is true.

Note that after closing an action, the awk parser is in a "pattern state", so the above could be written on one line:
Code:
awk '/^E/ { next } NF > 0'

or
Code:
awk 'NF > 0 || !/^E/'

Notice in the latter that the easier condition is checked first, i.e., the regexp pattern match is only done if there are non-space characters on the line. With a huge file (or repeated execution) this means the latter should perform better (this claim is not tested).
# 9  
Old 05-28-2003
Bug

Thank you all for your help. Does anyone can recommand some books for shell scripting?

Thanks
# 10  
Old 05-28-2003
This forum is a great start. Use the search function of this site as well - you'll find this question has been asked many times..
# 11  
Old 05-28-2003
another question

Now I have two flat text files, I only need to choose some of the fields from both files and combine into one file, then load to a database table. Is there any way to do it?

Thanks for your help.
# 12  
Old 05-28-2003
You'll need to be a bit more descriptive with what you're trying to do here.. i.e. what lines inparticular are you going to want to pull out? Read up on the grep command. If you're looking for lines with the word "hi" in them, you'd use something like grep "hi" file1 file2 > new_file.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

In a file, replace blank line by the last line not blank above

Dear All, In a CSV file, say that a given column has been extracted. In that column, information is missing (i.e. blank lines appear). I would like to replace the blank lines by the last valid line (not blank) previously read. For example, consider the extract below: 123 234 543 111... (7 Replies)
Discussion started by: bagvian
7 Replies

2. Shell Programming and Scripting

How to identify exact text and then add a blank line above it using sed?

I need to identify the exact text of San Antonio Generator Running in the output my script which lands to a text file. Once SED finds the specific text, I need it to insert one line above the matched text. Here is what I have so far that isn't working all that well for me. Any help would be... (7 Replies)
Discussion started by: jbrass
7 Replies

3. Shell Programming and Scripting

Get line number in flat file

Hi, Is there a way to find out the line number from where the data starts? like if the data contains column header, irrespective of the text in the column header we should get the line number from which contains the column header. I am sorry if I haven't explained the problem clearly. ... (8 Replies)
Discussion started by: kedar_laveti
8 Replies

4. Shell Programming and Scripting

How to remove blank line from a text file?

Hi All, I am creating a text file using perl. The first record I am writing as "$line" and all the other as "\n$line". At the end the file is having N number of lines. I am using this file for MLOAD (Teradata), which is reading N+1 lines in the file and failing.I am not able to find new line... (2 Replies)
Discussion started by: unankix
2 Replies

5. UNIX for Dummies Questions & Answers

Any way to get rid of ^M characters in a text file using pr?

When I use vi to see what's in the file I get this: int add1(int x) {^M return x + 1;^M} ^Mint subtract1(int x) {^M return x - 1;^M} ^Mint double_it(int x) {^M return x * 2;^M} ^Mint halve_it(int x) {^Mreturn x / 2;^M} ^Mint main() {^M int myint;^M int result;^M ... (2 Replies)
Discussion started by: Nonito84
2 Replies

6. 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

7. Shell Programming and Scripting

Getting Rid of Having to Write to Flat Files

Ok, so i've been having to write to flat files lately and then making my script read information from the flat file and then work off of that. i dont want to keep doing that because i believe it creates a mess. i like to keep my work all to one script instead of having that one script... (7 Replies)
Discussion started by: SkySmart
7 Replies

8. Shell Programming and Scripting

Random word from a flat text file

Hello, I need to take a random word from a flat text file with words in it seperated by spaces. The code I am using, always gives me the first word. Can anyone please shed some light on this. Here's my code. Thanks echo table roof ceiling jar computer monitor keyboard carpet >... (5 Replies)
Discussion started by: Freakhan
5 Replies

9. Shell Programming and Scripting

insert text and add blank line

I need your help, I want to add a text every 2nd line and also a blank line after 3 line (In the output 2nd line is "changetype: modify" and every 4th line is blank line) Input file format dn: abc orclsourceobjectdn: abcd dn: bcd orclsourceobjectdn: bcda dn: cba orclsourceobjectdn:... (7 Replies)
Discussion started by: athidhi
7 Replies

10. UNIX for Dummies Questions & Answers

Creating flat text file (ASCII)

Hi everybody. I need help and I hope someone is willing to help me out here. My wholesale company is currently moving to new software. The old software is running on a UNIX platform. We need to migrate data from the UNIX system, but our former software provider refuses to assist the data... (5 Replies)
Discussion started by: Wdonero
5 Replies
Login or Register to Ask a Question