To check Blank Lines, Blank Records and Junk Characters in a File


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting To check Blank Lines, Blank Records and Junk Characters in a File
# 1  
Old 05-07-2015
To check Blank Lines, Blank Records and Junk Characters in a File

Hi All

Need Help

I have a file with the below format (ABC.TXT) :

Code:
®¿¿ABCDHEJJSJJ|XCBJSKK01|M|7348974982790
HDFLJDKJSKJ|KJALKSD02|M|7378439274898
KJHSAJKHHJJ|LJDSAJKK03|F|9898982039999
(cont......)

I need to write a script where it will check for : blank lines (between rows,before first line and after last line) and remove them. Also if there are any blank records between the "|" delimeter it will identify the row number and send a mail. Finally in case there are "junk characters" it will identify and remove them. How should I go about it?

Last edited by Scrutinizer; 05-07-2015 at 05:18 PM.. Reason: CODE tags
# 2  
Old 05-07-2015
Quote:
Originally Posted by chatwithsaurav
Hi All

Need Help

I have a file with the below format (ABC.TXT) :

Code:
®¿¿ABCDHEJJSJJ|XCBJSKK01|M|7348974982790
HDFLJDKJSKJ|KJALKSD02|M|7378439274898
KJHSAJKHHJJ|LJDSAJKK03|F|9898982039999
(cont......)

I need to write a script where it will check for : blank lines (between rows,before first line and after last line) and remove them. Also if there are any blank records between the "|" delimeter it will identify the row number and send a mail. Finally in case there are "junk characters" it will identify and remove them. How should I go about it?
I know what a blank line is. What is a ": blank line"? Is it a blank line that can contain colons in addition to <space>s and <tab>s?

By definition, a blank line only contains characters in the character class blank and the character | is never a member of that character class. So, what do you really mean by "blank records between the "|" delimiter"?

All of the characters in the 1st two fields of the 1st three lines of your sample ABC.TXT look like "junk characters" to me. What is your definition of a junk character? If " KJHSAJKHHJJ" isn't junk, what is it?
# 3  
Old 05-08-2015
Hi Don..

Apologies for not "specifying" correctly. Smilie

1>Well Blank Line would mean spaces and tabs only.
2>Blank records in between "|" delimiter means that the column is NULL (blank as of now) . User has forgot to place the data there.
Example ABCDEFGH|"NULL"|M|"NULL"
"NULL"|XYZABNH|"NULL"|4567344
3>Junk Characters would mean characters other that alphabets or numerical or special characters. Something which is not understandable.
Example ¢'Á|äÃ



" KJHSAJKHHJJ" or "ABCDEFGH" are examples which I have used instead of proper names.
# 4  
Old 05-08-2015
1) awk with default field separators would set NF to 0 for those lines. Alternatively, you could use the regex /^[ ]*$/ (space and <TAB>) to identify them.
2) You'll need to loop over all fields and check for the length of each.
3) "Something which is not understandable" can be very locale depending. All of your examples "¢'Á|äÃ" are very essential in languages other than (US) English or necessary for e.g. record/parameter delimiting.

Last edited by RudiC; 05-08-2015 at 05:28 AM..
# 5  
Old 05-08-2015
Hi Rudi

Thanks!Smilie

Actually I have trying this now :
Code:
a=`grep -c " " abc.txt`
if [ $a gt 0 ]
then
      {perform rest of code}
fi

But I need to get a better syntax where a simple grep/sed command will help me to find and count any spaces/tabs between any rows (above,middle or below) and also in front and after last of any lines in a row. Can you please help me in that?
Example
Code:
abc.txt :
Space1 [/BOF]
Space2
ABCHSDJRIR|MSDJAS|M|122121
Space3
ASDSDSADSAS|DASDASD|K|12328137 
Space4 ASDSADASDA|qwueiwuqoei|H|1219827918Space5
[/EOF]

Regards

Last edited by vbe; 05-08-2015 at 11:09 AM.. Reason: pressing code tags is not enough, you insert between your code...
# 6  
Old 05-08-2015
We need a better preparations of your post.
Use code tags as required by the forum rules.

What have you tried so far?
REAL code, not nonworking-abreviahted pseudo code.

The above saying (would) show the respect many people here deserves as they are trying to help you.
And its absolute annoying to ask for every single bit.

What is your definition of 'get a better syntax', while you not provide a valid/(basicly) working syntax at all.

Besides, there are NO SPACECHARS at all in your textfile.
AGAIN, USE CODE TAGS!
And provide samples AS-IS. (example: not a 2TB database, but a few lines of actual data to work with)

Thank you, have a nice weekend

Last edited by sea; 05-08-2015 at 11:02 AM..
This User Gave Thanks to sea For This Post:
# 7  
Old 05-08-2015
Absolutely.
And, the requirement in post#5 differs from the ones in post#1. A concise specification accompanied by a adequate sample is needed!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Script to find blank records in a file except for few columns

I have a file with the following format: X|High|2|GIC|DM||XHM|||6 Months X|Moderate|2|GIC|DM||XHM|||6 Months X|High|2|GCM|DM||XSF|||6 Months X|Med|2|GCM|DM||XSF|||6 Here there are ten columns but I need to print rows having blank records in any of the rows (except for 6th,8th and 9th... (10 Replies)
Discussion started by: chatwithsaurav
10 Replies

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

3. Shell Programming and Scripting

How to check if the file is empty or has blank space.?

Hi, I am using KSH. I am trying to check if the output file is empty or not. I tried with ] but what i see is my file is empty but still manages to have a size of 1 instead of 0. But my file doesnot have anything its empty. I am not sure how to check this. can any one help? (10 Replies)
Discussion started by: Sharma331
10 Replies

4. Shell Programming and Scripting

Removing blank lines from a file

Hi All, How do i remove continuos blank lines from a file. I have a file with data: abc; def; ghi; jkl; mno; pqr; In the above file, there are two blank lines. I want to remove, one out of them. My output should look like: (2 Replies)
Discussion started by: raosr020
2 Replies

5. Shell Programming and Scripting

Reform Lines in File without blank lines and spaces

Hello All, I have a file with data as below. Each line consists of 21 fields. I am not able to load them back to the database. 50733339,"834","834 ","005010X279A1","N","Y","007977163","0001 ",30,"2110D ","EB ","EB007 ","2 ","Conditional Required Data Element Miss ing... (3 Replies)
Discussion started by: Praveenkulkarni
3 Replies

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

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

8. Shell Programming and Scripting

Can't remove blank lines from a file

Hi Guys, I have been trying to remove blank lines from a file with no success. I tried using all the following options on the file: tr -s '\n' < abc.txt grep -v "^$" abc.txt sed '/^$/d' abc.txt sed '/./!d' abc.txt awk '/./' abc.txt The file is a text file. (11 Replies)
Discussion started by: npatwardhan
11 Replies

9. Shell Programming and Scripting

how to check weather file is blank or not?

Dear All I want to do following task. Kindly suggest. In my script every hour one file is genarated. say xyz.txt. Now if this file contain some data then i want to do task A and if file is blank then i want to do nothing. Kindly help me to do this. regards jaydeep (5 Replies)
Discussion started by: jaydeep_sadaria
5 Replies

10. Shell Programming and Scripting

Deleting the blank line in a file and counting the characters....

Hi, I am trying to do two things in my script. I will really appreciate any help in this regards. Is there a way to delete a last line from a pipe delimited flat file if the last line is blank. If the line is not blank then do nothing..... Is there a way to count a word that are starting... (4 Replies)
Discussion started by: rkumar28
4 Replies
Login or Register to Ask a Question