LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 02-27-2008, 03:43 PM   #1
naveensankineni
LQ Newbie
 
Registered: Feb 2008
Posts: 11

Rep: Reputation: 0
to print the last character on the last line of the file


Hi,

I need to print the last character on the last line of the file and check weather is ; or /

iam trying the follwing code

tail -1 script.sh | awk '{print $NF}'
 
Old 02-27-2008, 03:45 PM   #2
forrestt
Senior Member
 
Registered: Mar 2004
Location: Cary, NC, USA
Distribution: Fedora, Kubuntu, RedHat, CentOS, SuSe
Posts: 1,288

Rep: Reputation: 99
What is wrong with that? Seems to work or me.

Forrest

Never mind, it prints the last word of the file. The file I picked to test with only had one char for the last word.

Last edited by forrestt; 02-27-2008 at 03:47 PM.
 
Old 02-27-2008, 03:54 PM   #3
forrestt
Senior Member
 
Registered: Mar 2004
Location: Cary, NC, USA
Distribution: Fedora, Kubuntu, RedHat, CentOS, SuSe
Posts: 1,288

Rep: Reputation: 99
Try:

Code:
tail -1 fix_sendmail.sh | awk '{print substr($NF, length($NF), 1)}'
HTH

Forrest
 
Old 02-27-2008, 07:34 PM   #4
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
Code:
fix_sendmail.sh  | awk 'BEGIN{FS=""}END{print $NF}'

Last edited by ghostdog74; 02-27-2008 at 07:35 PM.
 
Old 02-27-2008, 08:52 PM   #5
osor
HCL Maintainer
 
Registered: Jan 2006
Distribution: (H)LFS, Gentoo
Posts: 2,450

Rep: Reputation: 78
What’s wrong with
Code:
tail -c1 file
Or, (if you have trailing newlines)
Code:
tail -c2 file
 
Old 02-27-2008, 08:53 PM   #6
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809

Rep: Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743
sed -n '$s/.*\(.\)/\1/p' filename > newfilename
 
Old 02-27-2008, 09:32 PM   #7
ta0kira
Senior Member
 
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078

Rep: Reputation: Disabled
How do all of these utils handle \r\n and \r end-of-lines? I've never tried any of them with other-than \n files.
ta0kira
 
Old 02-27-2008, 10:43 PM   #8
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
Quote:
Originally Posted by osor View Post
What’s wrong with
Code:
tail -c1 file
Or, (if you have trailing newlines)
Code:
tail -c2 file
nothing wrong, just that if its in systems like Solaris, might have to adjust the syntax a little.
Code:
tail -2c file
works both in linux and solaris.
 
Old 02-28-2008, 07:00 AM   #9
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809

Rep: Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743
Quote:
Originally Posted by ta0kira View Post
How do all of these utils handle \r\n and \r end-of-lines? I've never tried any of them with other-than \n files.
ta0kira
In the case of SED, the newline at the end of the line in the pattern buffer gets ignored. (You can see what is in the pattern buffer with the "l" command.)

I just did an experiment with a tab a the end of the line. My SED command returns the tab character as the last character of the line (ie not the last printable character) My hunch is that the final newline is the only one that is ignored, but I have not tested.
 
Old 02-28-2008, 09:49 AM   #10
naveensankineni
LQ Newbie
 
Registered: Feb 2008
Posts: 11

Original Poster
Rep: Reputation: 0
Thanks to all its working

tail -1 fix_sendmail.sh | awk '{print substr($NF, length($NF), 1)}'
 
Old 02-28-2008, 09:51 AM   #11
naveensankineni
LQ Newbie
 
Registered: Feb 2008
Posts: 11

Original Poster
Rep: Reputation: 0
script to check the line is a blank line or not

hi ,

i just need to check that my last line at the end of the script is a blank line or not

iam trying to satisfy the if condition for this but i coudn't

code which i had tried is

blank = `tail -1 /home/oracle/grep.sh`
if [ "$blank" = '' ]
then
echo "end of the line is the blank line"
else
echo "error"
fi


please suggest
 
Old 02-28-2008, 01:49 PM   #12
osor
HCL Maintainer
 
Registered: Jan 2006
Distribution: (H)LFS, Gentoo
Posts: 2,450

Rep: Reputation: 78
Quote:
Originally Posted by ghostdog74 View Post
nothing wrong, just that if its in systems like Solaris, might have to adjust the syntax a little.
Code:
tail -2c file
works both in linux and solaris.
I believe this is the ‘historical’ usage of tail. The command that I posted is the ‘portable’ (in particular POSIX-compliant) usage of tail. Since Solaris is POSIX-compliant, you should be able to use the usage I posted on Solaris as well (you might have to put /usr/xpg4/bin ahead in your PATH).
 
Old 02-28-2008, 08:12 PM   #13
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
Quote:
Originally Posted by osor View Post
you might have to put /usr/xpg4/bin ahead in your PATH).
yes, totally forgot about this. thks.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
cups print to file on command line kairen Slackware 2 01-30-2008 02:05 AM
Command to print last line of a text file Micro420 Linux - General 3 01-25-2007 01:17 AM
How to print a given line from a file on the standard output Fond_of_Opensource Linux - Newbie 1 08-24-2006 02:45 AM
Print line number X of a file (in shell) rheza Programming 4 01-04-2005 05:55 PM
Perl - Tpl file - Need to replace new line character. knnirmal Programming 2 09-07-2004 02:27 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 03:20 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration