Unix find missing new line


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Unix find missing new line
# 1  
Old 05-17-2011
Unix find missing new line

Hi All,

I wanted to check out a missing new line character from last line on Unix.

If file is having newline character at the end of last line then it should show as "\n" else will display the last character whatever it is.

OR

If file is having newline character at end of last line then it should show as "\n" else will say "New line character is missing at the end of last line"

Please let me know how can achieve this.
# 2  
Old 05-17-2011
You can use od to show the last characacter of a file.
Code:
$ echo -n hello > testfile1
$ echo hello > testfile2
$ file=testfile1
$ od -An -tc -j $(( $(ls -l $file  | awk '{print $5}') - 1 )) $file
   o
$ file=testfile2
$ od -An -tc -j $(( $(ls -l $file  | awk '{print $5}') - 1 )) $file
  \n
$

This User Gave Thanks to Perderabo For This Post:
# 3  
Old 05-17-2011
This is working perfectly fine for me ... thanks for your quick revert on this.

One request ... may I ask you how this line is working, please ?
# 4  
Old 05-17-2011
Another method (must be run as a shell script):

Code:
lastchar=`tail -c 1 "${filename}"`
if [ "${lastchar}""X" = "X" ]
then
       echo "Last character is: \\\n"
else
       echo "Last character is: ${lastchar}"
fi

# 5  
Old 05-17-2011
Well ls -l $file | awk '{print $5}' gets the file size. Subtract 1 from that to get the number of bytes to ignore. od dumps the file in various formats. "-An" says to omit any address info. "-tc" says to display it the character format. And "-j" says to skip that many bytes.
# 6  
Old 05-17-2011
Hi Perderabo,

Thanks once again for your help.

---------- Post updated at 10:14 PM ---------- Previous update was at 10:12 PM ----------

Hi Methyl,

I tried executing below command & getting below error,

Code:
bash-2.03$ lastchar=`tail -c 1 "$filename"`
tail: cannot open input

Please advise.
# 7  
Old 07-05-2011
Hi Perderabo,

I have came across some limitation of "od" command, can you please help me to correct this,

I have one file with below details,

Code:
$ ls -lrt test
-rwxrwxr-x   1 user    sys         1 Jul  5 08:02 test

When I used command to find last line character it throws me an error,

Code:
$ ls -lrt test | | awk '{print $5}'
1
$ od -An -tc -j 0 test
Invalid offset
usage: od [-bcCdDfFoOsSvxX] [-] [file] [offset_string]
       od [-bcCdDfFoOsSvxX] [-t type_string]... [-A address_base] [-j skip] [-N count] [-] [file...]

Could you please help me.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Directory missing .how to find the cause .

Hi, i am using rhel6.4, i lost my directory under /home .is there any way to find the reason how that directory deleted and how to recover deleted folder. (2 Replies)
Discussion started by: sayhirams
2 Replies

2. UNIX for Advanced & Expert Users

How to find a string in a line in UNIX file and delete that line and previous 3 lines ?

Hi , i have a file with data as below.This is same file. But actual file contains to many rows. i want to search for a string "Field 039 00" and delete that line and previous 3 lines in that file.. Can some body suggested me how can i do using either sed or awk command ? Field 004... (7 Replies)
Discussion started by: vadlamudy
7 Replies

3. Shell Programming and Scripting

[Solved] line breaks missing when emailed from unix to win

hi i am sending an email from unix to windows platform and using uuencode to attach the plain text files (.txt). But when i read the attached file in notepad the linebreaks are gone. uuencode samplefile.txt samplefile.txt | mail -s "test email" <mailid.com> if i copy paste the... (6 Replies)
Discussion started by: midhun19
6 Replies

4. UNIX for Dummies Questions & Answers

find/xargs/*grep: find multi-line empty "try-catch" blocks - eg, missing ; not in a commented block

How can I recursively find all files in a directory and print out the file and first line number of any text blocks that match the below cases? This would seem to involve find, xargs, *grep, regex, etc. In summary, I want to find so-called empty "try-catch blocks" that do not contain code... (0 Replies)
Discussion started by: lifechamp
0 Replies

5. Shell Programming and Scripting

find: missing argument to `-exec' while redirecting using find in perl

Hi Friends, Please help me to sort out this problem, I am running this in centos o/s and whenever I run this script I am getting "find: missing argument to `-exec' " but when I run the same code in the command line I didn't find any problem. I am using perl script to run this ... (2 Replies)
Discussion started by: ramkumarselvam
2 Replies

6. Shell Programming and Scripting

How do i find the first number in each line and insert dummy string into the missing columns?

Hi, I have one input file with the following content: MY_inpfile.txt Aname1 Cname1 Cname2 1808 5 Aname2 Cname1 1802 47 Bname1 ? 1819 22 Bname2 Cname1 1784 11 Bname3 1817 9 Zname1 Cname1 1805 59 Zname2 Cname1 Cname2 Cname3 1797 27 Every line in my input file have a 4 digit... (5 Replies)
Discussion started by: Szaffy
5 Replies

7. Shell Programming and Scripting

command-line line 0: Missing yes/no argument

Hi Guys When I run the below command ssh -o 'PasswordAuthentication yes' -o 'PreferredAuthentications publickey' -i $HOME/.ssh/id_dsa Server_Name I found the below error ommand-line line 0: Missing yes/no argument Kindly help me to sort out Double post, continued... (0 Replies)
Discussion started by: Pratik4891
0 Replies

8. Shell Programming and Scripting

Unix help to find blank lines in a file and print numbers on that line

Hi, I would like to know how to solve one of my problems using expert unix commands. I have a file with occasional blank lines; for example; dertu frthu fghtu frtty frtgy frgtui frgtu ghrye frhutp frjuf I need to edit the file so that the file looks like this; (10 Replies)
Discussion started by: Lucky Ali
10 Replies

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

10. UNIX for Advanced & Expert Users

missing Path(in UNIX) when i launch a job on to unix machine using windows SSh

hi i want run an unix application from a windows program/application.i am using SSH(command line version)to log on to a unix machine from windows. the application has to read a configuration file inorder to run. the configuration file .CFG is in bin in my home directory. but the application... (1 Reply)
Discussion started by: megastar
1 Replies
Login or Register to Ask a Question