Need to serach if a new line character exists on the last line in a file


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Need to serach if a new line character exists on the last line in a file
# 1  
Old 02-20-2008
Need to serach if a new line character exists on the last line in a file

I have a file in which I need to search if a new line character exists on the last line in the file. Please let me know how can I achieve it using Unix commands?
# 2  
Old 02-21-2008
Code:
awk 'END { if ($0=="") print "New line found."; else print "No new line found." }' input_file

# 3  
Old 02-28-2008
It is not working

I created 2 files one with new line character and other with no new line charcter. For both these files, it still gives the following message:

New line found.

Please help.
# 4  
Old 02-28-2008
What do you mean with newline character? A "\n"? An empty line?

What's the format of your file (dos/unix)? On which OS and shell?
# 5  
Old 02-28-2008
1. The file is on a Unix OS.

2. Unix Hardware configuration:
Sun Microsystems Inc. SunOS 5.9

3. I need to search for end of line character i.e. /n on the last line of this file.

Please let me know if you will need more information.
# 6  
Old 02-28-2008
Uhm... A bit complex but it should work:
Code:
tail -1 input_file | od -An -t oC -w1 | tail -1 | grep -c "012"

Returns 1 if newline found, otherwise 0.
# 7  
Old 02-28-2008
2 problems:
a. when the command is executed it specifies "usage" in the result and
b. Looks like it retruns a value '0' for both types of files.

Please see below:

sma2z@ianb1as1 [/apps/prodcopy/export/RW10X] $ --> tail -1 Alert_enuCPU18.0002.exp | od -An -t oC -w1 | tail -1 | grep -c "012"
usage: od [-bcCdDfFoOsSvxX] [-] [file] [offset_string]
od [-bcCdDfFoOsSvxX] [-t type_string]... [-A address_base] [-j skip] [-N
count] [-] [file...]
0

sma2z@ianb1as1 [/apps/prodcopy/export/RW10X] $ --> tail -1 Alert_enuCPU16.0001.exp | od -An -t oC -w1 | tail -1 | grep -c "012"
usage: od [-bcCdDfFoOsSvxX] [-] [file] [offset_string]
od [-bcCdDfFoOsSvxX] [-t type_string]... [-A address_base] [-j skip] [-N
count] [-] [file...]
0


Thanks for looking into my problem.
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Count specific character of a file in each line and delete this character in a specific position

I will appreciate if you help me here in this script in Solaris Enviroment. Scenario: i have 2 files : 1) /tmp/TRANSACTIONS_DAILY_20180730.txt: 201807300000000004 201807300000000005 201807300000000006 201807300000000007 201807300000000008 2)... (10 Replies)
Discussion started by: teokon90
10 Replies

2. Shell Programming and Scripting

Sed/grep: check if line exists, if not add line?

Hello, I'm trying to figure out how to speed up the following as I want to use multiple commands to search thousands of files. is there a way to speed things up? Example I want to search a bunch of files for a specific line, if this line already exists do nothing, if it doesn't exist add it... (4 Replies)
Discussion started by: f77hack
4 Replies

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

4. Shell Programming and Scripting

awk new line issue, saying string can't contain new line character

Hi , I am doing some enhancements in an existing shell script. There it used the awk command in a function as below : float_expr() { IFS=" " command eval 'awk " BEGIN { result = $* print result exit(result == 0) }"' } It calls the function float_expr to evaluate two values ,... (1 Reply)
Discussion started by: mady135
1 Replies

5. Solaris

Line too long error Replace string with new line line character

I get a file which has all its content in a single row. The file contains xml data containing 3000 records, but all in a single row, making it difficult for Unix to Process the file. I decided to insert a new line character at all occurrences of a particular string in this file (say replacing... (4 Replies)
Discussion started by: ducati
4 Replies

6. HP-UX

How to remove new line character and append new line character in a file?

Hi Experts, I have data coming in 4 columns and there are new line characters \n in between the data. I need to remove the new line characters in the middle of the row and keep the \n character at the end of the line. File is comma (,) seperated. Eg: ID,Client ,SNo,Rank 37,Airtel \n... (8 Replies)
Discussion started by: sasikari
8 Replies

7. Shell Programming and Scripting

[Solved] Problem in reading a file line by line till it reaches a white line

So, I want to read line-by-line a text file with unknown number of files.... So: a=1 b=1 while ; do b=`sed -n '$ap' test` a=`expr $a + 1` $here do something with b etc done the problem is that sed does not seem to recognise the $a, even when trying sed -n ' $a p' So, I cannot read... (3 Replies)
Discussion started by: hakermania
3 Replies

8. Shell Programming and Scripting

Awk not working due to missing new line character at last line of file

Hi, My awk program is failing. I figured out using command od -c filename that the last line of the file doesnt end with a new line character. Mine is an automated process because of this data is missing. How do i handle this? I want to append new line character at the end of last... (2 Replies)
Discussion started by: pinnacle
2 Replies

9. Shell Programming and Scripting

how to get line number of different words if exists in same line

I have some txt files. I have to create another text file which contains the portion starting from the format "Date Sex Address" to the end of the file. While using grep -n on Date it also gives me the previous line containg Date. and also Date may be DATE in some files. My file is like this... (10 Replies)
Discussion started by: Amiya Rath
10 Replies

10. UNIX for Dummies Questions & Answers

delete a line based on first character of the line

Hi, I need to delete all lines in a file which starts with "|" character. Can some one assist me? Thanks (2 Replies)
Discussion started by: borncrazy
2 Replies
Login or Register to Ask a Question