How to check newline character in file?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to check newline character in file?
# 1  
Old 06-04-2012
How to check newline character in file?

Hi All,

I have file with only one record,always be only one record. as like below.
if that line contains newline end of the line..no need to add, if not just add the new line character.
Code:
END OF FILE. ROW COUNT: 7

Please help me..

Thanks,
# 2  
Old 06-04-2012
Try:
Code:
awk 1 file

Code:
sed '' file

# 3  
Old 06-04-2012
Thanks quick reply.. i will try..

---------- Post updated at 03:02 AM ---------- Previous update was at 02:56 AM ----------

i am really appreciate...one more thanks...
# 4  
Old 06-04-2012
you can use

Code:
 
wc -l < filename

it will show the output as 2 ( if you have a newline )
This User Gave Thanks to itkamaraj For This Post:
# 5  
Old 06-04-2012
Quote:
Originally Posted by bmk
Hi All,

I have file with only one record,always be only one record. as like below.
if that line contains newline end of the line..no need to add, if not just add the new line character.
Code:
END OF FILE. ROW COUNT: 7

Please help me..

Thanks,
Code:
sed r file

or
Code:
if [[ $(od -w10 -c file|tac|sed -n '2s/.*\(..\)$/\1/p') = "\n" ]] ; then echo "$(<file)" ; else echo -en "$(<file)\n" ; fi

# 6  
Old 06-04-2012
@ygemici: why all that bash/ksh juggling Smilie, when this part:
Code:
echo "$(<file)"

would suffice? or (POSIX)
Code:
printf "%s\n" "$(cat file)"

# 7  
Old 06-04-2012
Quote:
Originally Posted by Scrutinizer
@ygemici: why all that bash/ksh juggling Smilie, when this part:
Code:
echo "$(<file)"

would suffice? or (POSIX)
Code:
printf "%s\n" "$(cat file)"

yes you're right Smilie , actually just printf was enough instead of if loop Smilie
Code:
printf "%s\n" "$(cat file)"


Last edited by ygemici; 06-04-2012 at 07:41 AM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Remove newline character from column spread over multiple lines in a file

Hi, I came across one issue recently where output from one of the columns of the table from where i am creating input file has newline characters hence, record in the file is spread over multiple lines. Fields in the file are separated by pipe (|) delimiter. As header will never have newline... (4 Replies)
Discussion started by: Prathmesh
4 Replies

2. Shell Programming and Scripting

How to remove newline character if it is the only character in the entire file.?

I have a file which comes every day and the file data look's as below. Vi abc.txt a|b|c|d\n a|g|h|j\n Some times we receive the file with only a new line character in the file like vi abc.txt \n (8 Replies)
Discussion started by: rak Kundra
8 Replies

3. Shell Programming and Scripting

Warning while sorting : A newline character was added to the end of file

Hi, I am trying to sort a csv file which has say 10 lines each line having a row size that is upto 30183 no. of COLUMNS (Row length = 30183). There is a LINE FEED (LF) at the end of each line. When I try to sort this file say, based on the second FIELD using the below command, sort -t ',' +1... (5 Replies)
Discussion started by: DHeisenberg
5 Replies

4. Shell Programming and Scripting

Script for removing newline character from file

Hi below is my file. cat input.dat 101,abhilash,1000 102,prave en,2000 103,partha,4 000 10 4,naresh,5000 (its just a example file) and my output should be: 101,abhilash,1000 102,praveen,2000 103,partha,4000 104,naresh,5000 below is my code cat input.dat |tr -d '\n' >... (6 Replies)
Discussion started by: abhilash_nakka
6 Replies

5. Shell Programming and Scripting

Appending newline character End of File

Hi Gurus, Need help. I'm a beginner in Unix. I have a requirement, need to add or append newline (\n) character in file. Sample Data: 1|Main|Test|~# 2|Main|Hello|~# 3|Main|Unix|~# 4|Main|File|~#Output: 1|Main|Test|~# 2|Main|Hello|~# 3|Main|Unix|~# 4|Main|File|~#\n -- append only... (13 Replies)
Discussion started by: Gouri Solleti
13 Replies

6. Shell Programming and Scripting

How to add newline character at end of file?

Hi All, I have following piece of code in UNIX C Shell script and I want to add one more command which can add newline at the end of file only if there is no newline character exists. foreach file (`ls $dd_PLAYCARD_EDI_IN`) if ( -f $dd_PLAYCARD_EDI_IN/${file} ) then cat -n... (4 Replies)
Discussion started by: jnrohit2k
4 Replies

7. Shell Programming and Scripting

Echo to file using SH without adding newline character

Hello! I am able to do this in bash, using: echo -ne HELLO > file.txt and then, 'HELLO' is written into file.txt without the newline character to be added in the end of the file. How is this possible to be done using sh instead of bash? If I try something similar is SH, then inside... (3 Replies)
Discussion started by: hakermania
3 Replies

8. Shell Programming and Scripting

How to avoid Newline character in generated text file?

Hi All, Just need small help in resolving the special new line character in generated output file. In one of my shell script I am using following lines to get the spool file (i.e. sfile.txt) and AAA_XXXX_p111_n222.txt AAA_YYYY_p111_n222.txt Here assuming v_pnum="p111" v_nid="n222" ... (1 Reply)
Discussion started by: shekharjchandra
1 Replies

9. Shell Programming and Scripting

To remove the newline character while appending into a file

Hi All, We append the output of a file's size in a file. But a newline character is appended after the variable. Pls help how to clear this. filesize=`ls -l test.txt | awk `{print $5}'` echo File size of test.txt is $filesize bytes >> logfile.txt The output we got is, File size of... (4 Replies)
Discussion started by: amio
4 Replies

10. AIX

check for a particular character inside a file and substitute with a given character?

i am a newbie to shell script,so i want a kshell script in which i need to check for a particular character inside a file through conditional looping(like if ,case,while)and if that character exists ,then substitute a given character to that character. consider a file test.txt,inside the file... (1 Reply)
Discussion started by: karthikprasathk
1 Replies
Login or Register to Ask a Question