Match EOF on newline in a file


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Match EOF on newline in a file
# 1  
Old 01-12-2012
Match EOF on newline in a file

Hi,
i have a file where the end-of-file might be at the end of of a valid text line or on a new line

case a)
p q r
s t u <eof>

case b)
p q r
s t u
<eof>

case c)
p q r
s t u
<no data, only carriage return>
<eof>

I have a requirement where <eof> line should not be read if it's on a newline (in a grep command)

I don't want to use tail -1 because that will omit data in case a), and
don't want to ignore blank lines because will lose the blank line after 's t u' in case c)

How do i do this?

Thanks,
-srinivas yelamanchili
# 2  
Old 01-12-2012
@ysrini: Did you try searching the forum? There're several posts on this one.
Code:
grep -v "^<eof>" inputfile

# 3  
Old 01-12-2012
Network

Quote:
Originally Posted by balajesuri
@ysrini: Did you try searching the forum? There're several posts on this one.
Code:
grep -v "^<eof>" inputfile


balajesuri, <eof> is not a string by itself, here eof stands for end-of-file and we use <...> notation for explaining something that's not obvious
In the data i provided since you cannot see where the end of file character is i denoted that by <eof>

My question was on how to avoid a newline that's the end of the file too, and not the string "<eof>"

Like newline is represented by "\n", how's eof character represented?
# 4  
Old 01-12-2012
Code:
sed '$d' inputfile; [[ `sed '$!d' inputfile` != '' ]] && sed '$!d' inputfile

Another one:
Code:
[[ `tail -1 input` != '' ]] && cat input || sed '$d' input


Last edited by balajesuri; 01-12-2012 at 09:01 AM..
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Insert a newline after match in files of specific name under some subdirectories?

Hi I'd like to add the newline: \tuser: nobody", or "<TAB>user: nobody to all files named: docker-compose.ymlin subfolders of pwd with names beginning with 10-20. Within these files, I'd like to find the line (there'll only be one) containing: command: celery workerNOTE: As far as... (2 Replies)
Discussion started by: duncanbetts
2 Replies

2. Shell Programming and Scripting

sed to remove newline chars based on pattern mis-match

Greetings Experts, I am in AIX; I have a file generated through awk after processing the input files. Now I need to replace or remove the new-line characters on all lines that doesn't have a ; which is the last character on the line. I tried to use sed 's/\n/ /g' After checking through the... (6 Replies)
Discussion started by: chill3chee
6 Replies

3. UNIX for Dummies Questions & Answers

Print a newline after first match in line

Hi everyone I have a file where CP occurs both within each line and at the very end: dwer 17 knsdask= * CP hwla 17 h'wopie un CP I would like to separate the line on the first CP to get: dwer 17 knsdask= * CP hwla 17 h'wopie un CP What I have so far is: awk '{for (x=1; x<NF; x++) ... (5 Replies)
Discussion started by: meet77
5 Replies

4. Shell Programming and Scripting

EOF of file

Hi, I ve a file with name "check" in this file "check" has two lines with contents now how to read these files line by line.. i used the code But these doesnt stop with two lines.. So how to set condition that only two lines be read and the program should stop its execution.. Please... (5 Replies)
Discussion started by: Priya Amaresh
5 Replies

5. Shell Programming and Scripting

sed truncating last line if EOF is not with newline

sed 's|^xyz.abc *= *.*|xyz.abc=60|' /test/myFile.properties > /test/myFile.properties1 myFile.properties has the last line as xyz.abc=23 does not end with newline. When I run this command the last line is truncated if there is no newline character at EOF. Looking for an alternative here. (1 Reply)
Discussion started by: sunn_hps
1 Replies

6. Shell Programming and Scripting

AWK Script Issue insert newline for a regular expression match

Hi , I am having an issue with the Awk script to insert newline for a regular expression match Having a file like this FILE1 #################### RXOER , RXERA , RXERC , RXERD .RXEA(RXBSN), RXERD , REXCD input RXEGT buffer RXETRY ####################### Want to match the RXE... (38 Replies)
Discussion started by: jaita
38 Replies

7. UNIX for Dummies Questions & Answers

Match newline character "\n" in lex

Hi everyone! This is my very first post, sorry if I'm not posting in the right category. I'm trying to match a newline "/n" using lex/yacc. For example, print(9,'\n',8) should print 9 8 now do I write a regular expression to match exactly " '\n' " Thanks! (1 Reply)
Discussion started by: code21
1 Replies

8. Shell Programming and Scripting

eof in data file

Hi, How to detect eof character in a data file? I am reading a data file generated from UNIX. I use java scanner, the scanner's hasNextLine() returns false in the middle of the file. I suspect there is a eof in the middle of the data file. Does anybody know how to check if a file contains eof... (1 Reply)
Discussion started by: redwing
1 Replies

9. Programming

How to match n number of newline character(\n) in lex and yacc

Hi , I need to develop a parser which should match something like 1. text a=5 " a=20"; 2. text a=..." a=20"; 3 text a=..." a=20 b=34 c=12 "; I have used this regular expression in my Lex file to generate the tokens: \".\s*.*\s.\" (8 Replies)
Discussion started by: vishwa787
8 Replies

10. Shell Programming and Scripting

How to check if the file DOES NOT have EOF

Hi, Please help me on this. how to check if the file DOES NOT have EOF ?? I am using ksh for this. (3 Replies)
Discussion started by: BasavarajaKC
3 Replies
Login or Register to Ask a Question