Check for empty line at end of a dynamic header in each file


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Check for empty line at end of a dynamic header in each file
# 1  
Old 04-15-2015
Linux Check for empty line at end of a dynamic header in each file

Hi Folks,

I have a requirement to develop a shell script. PFB my requirement,

Requirement:
I need to check an empty line after the end of each header in respective file and if a empty line is present simply echo file OK and if empty line is not present echo "Adding empty line" and add an empty line to an existing file with out modifying any other content in the same file.

My Problem:
The problem where I am stuck is there are four files and in each file the header is spanned into multiple lines with no specific format (In other words dynamic, In one file the header is spanned in 4 lines , in other 5 lines and in other 6 lines). I am not able to find a way how to check the end of header and accomplish my above requirement.

I have googled and read many articles but could not find a relevant answer. It would be of great help if you can help me out.

I have attached the sample files (In fact the original files which I am testing) for your reference. Please help me out.

With Regards,
TPK
# 2  
Old 04-15-2015
In fact, there's not a single file in your samples that has an empty line after the header. MClubAnn_20150204.txt seems to have but has a <CR> (0x0D, carriage return) char in it. All the other have numerous (empty) fields in them.
It will be very difficult to tell header from data, nothing I'd dream up would work consistenly:
- not the field count
- not the structure (string fields vs. numeric fields)
- not unpaired double quotes

Plus, some seem to have header above the headers?
This User Gave Thanks to RudiC For This Post:
# 3  
Old 04-15-2015
Hi,
maybe to search first line with field in format [0-9][0-9]/[0-9][0-9]/[0-9][0-9], and all line before this line are "The Header" ?

Regards.
This User Gave Thanks to disedorgue For This Post:
# 4  
Old 04-15-2015
Hi Rudic and disedorgue,

Thank You for your thoughts. Even I thought of similar options but nothing worked.

If we forget about the header, presuming that the header will not be more than 5 rows, how to add an empty line after 5th line in every file if there is no empty line.

Some times the requirements seem to be crazy which will make us mad spending hours Smilie

With Regards,
TPK
# 5  
Old 04-15-2015
with gnu sed (not tested):
Code:
sed -e '5s/^..*$/\n&/' file

Regards.
# 6  
Old 04-16-2015
Hi disedorgue,

I have tried with that it's not working, I have tried with the MClubWA_20150204.txt file.

I have executed the below command and taken count, the count seems to be same as 26 before executing the command and after executing the command.

Code:
sed -e '5s/^..*$/\n&/' MClubWA_20150204.txt|wc -l

And I want to append it to existing file only but sed seems to print the result on console. Please let me know it would be of great help for me. I am trying this for past 4 days.

With Regards,
TPK
# 7  
Old 04-16-2015
Yes, sed writes the results it produces to its standard output. If you want the output saved in a file, you can redirect its output.

What operating system and shell are you using?

All of your sample files have some lines containing <carriage-return> characters. Some (maybe all) of your sample files have some lines that do not contain <carriage-return> characters. If these lines did not contain <carriage-return> characters, the standards would call these blank lines. Are you trying to create output files in DOS format (with all lines terminated by <carriage-return><newline> or are you trying to create output files in UNIX format (no <carriage-return>, just <newline> as the line terminator)?

What is your definition for empty line? The standards define an empty line to be a line that only contains one character (the <newline> character that terminates that line). Some of your files contain lines that only contain lots of <tab> characters terminated by a <carriage-return><newline> character pair. Do you consider that to be an empty line? Some of the DOS format blank lines appear as line 2 or 3 and are followed by other lines that still appear to be part of your headings. Some of your DOS format blank lines appear after what seems to be that file's heading and before that file's data.

Do you want add an empty line to your output file between the 5th and 6th input lines from your input files and also suppress copying all blank lines from your input (after removing <carriage-return>s)?
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Matching the header of a .CSV file with dynamic field names

I have a .CSV file (frequency - weekly) whose header contains the year-week value in two of the columns which keeps changing every week. For an instance please see below. Column1,Column2,Column3,Column4,Column5,Column6,Column7,Column8,Column9,Column10,Column11,Column12,Column13,201420... (4 Replies)
Discussion started by: dhruuv369
4 Replies

2. Shell Programming and Scripting

Need a program that read a file line by line and prints out lines 1, 2 & 3 after an empty line...

Hello, I need a program that read a file line by line and prints out lines 1, 2 & 3 after an empty line... An example of entries in the file would be: SRVXPAPI001 ERRO JUN24 07:28:34 1775 REASON= 0000, PROCID= #E506 #1065: TPCIPPR, INDEX= 003F ... (8 Replies)
Discussion started by: Ferocci
8 Replies

3. Linux

Add empty columns at the end of a CSV file

I have a .CSV file (lets say named as file.csv) with numeric and string values. The string might contain commas hence they are enclosed in double quotes as in the below format. column1,column2,column3,column4,column5,column6,column7 12,455,"string, with, quotes, and with, commas, in... (3 Replies)
Discussion started by: dhruuv369
3 Replies

4. UNIX for Dummies Questions & Answers

Removing empty lines at the end of a Tab-delimited file

I'm trying to remove all of the empty lines at the end of a Tab delimited file. They have no data just tabs. I've tried may things, here are a couple: sed /^\t.\t/d File1 > File2 sed /^\t{44}/d File1 > File2 What am I missing? (9 Replies)
Discussion started by: SirHenry1
9 Replies

5. Programming

Why is required to leave an empty line at the end of a C program?

I know it looks like a stupid question, but i really wanna know the reason. Actually, i think it's because the c compiler will detect it as the end of file "EOF" of the program, but, am i wrong? because it compiles it anyway, but keep showing warnings like "no new line at the end of file". I... (8 Replies)
Discussion started by: semash!
8 Replies

6. Shell Programming and Scripting

Add Empty columns at the end of csv file

Hi, Can you please tell me how to add empty columns at the end csv file? Currently there are 6 columns in the csv file. I want to add 35 empty columns at the end of this csv file. Thanks, Tushar (17 Replies)
Discussion started by: Tushar Bendale
17 Replies

7. Shell Programming and Scripting

Append Spaces At end of each line Leaving Header and Footer

How to append constant No of spaces suppose 52 at end of each line in a file (xyz) excluding first and last line. Please Help me out for the same. (1 Reply)
Discussion started by: deepam
1 Replies

8. Shell Programming and Scripting

Is there any commands to check the dynamic changes of a file

Hi guys i had a script which will generate a log file.Is there any commands to check the dynamic changes in the log file,i.e if i open the log file i should able to see the updating changes live...I hope u understand my query... (2 Replies)
Discussion started by: vinoo
2 Replies

9. Shell Programming and Scripting

check position of end of line(URGENT PLS)

I Have to check in a file that all the lines are ending at same posiotin. Ex : line 1 is ending at position 88 line 2 should at same position i.e 88 Thanks in advance (6 Replies)
Discussion started by: evvander
6 Replies

10. Shell Programming and Scripting

check position of end of line for some specific lines

-------------------------------------------------------------------------------- Have to check in a file that the lines starting with 620 and 705 are ending at same posiotin. 82012345 62023232323 70523949558 62023255454 9999 In the above lines, i have to check the lines starting... (1 Reply)
Discussion started by: senthil_is
1 Replies
Login or Register to Ask a Question