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
# 8  
Old 02-29-2008
Sorry, in Solaris od doesn't support -w option. So try this:

Code:
tail -1 input_file | od -An -t oC | grep -c "012$"

# 9  
Old 02-29-2008
It works perfectly.

Thank you very much, Sir.
# 10  
Old 02-29-2008
One last Question:
I was trying to understand the command and had 1 question:

1. "tail" obtains the last line of the file and passes to the "od" command
2. "od" obtains the last character of the file and passes to "grep" command
3. grep -c "012$" returns the count of '1' or '0' depending upon the search of the only character.

1. what does "012$" signify? I saw that it also works when I just put grep -c "012"

2. Can you please let me know the significance of $ and is it mandatory or optional character in this situation and

3. what does "012" signify and how does it correspond to the new line charcter.

Again I would like to thank you for solving my query.
# 11  
Old 02-29-2008
Quote:
Originally Posted by sunilbm78
1. what does "012$" signify? I saw that it also works when I just put grep -c "012"

2. Can you please let me know the significance of $ and is it mandatory or optional character in this situation and

3. what does "012" signify and how does it correspond to the new line charcter.
The string "012" is the octal code of the newline character (\n). If you see "man ascii" you can find it.
od just prints out the octal code for every character it finds on the last line of input_file (tail -1).

The dollar sign in grep expression means: match the string "012" only at the end of the line. If "012" is found in the middle of the string returned by od, the result is 0.
Well, it is impossible to find the newline character in the middle of a line, so the regexp "012" or "012$" in this case doesn't matter. But I prefer to be precise Smilie
 
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