LinuxQuestions.org
Share your knowledge at the LQ Wiki.
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 11-03-2018, 10:25 AM   #1
murugesan
Member
 
Registered: May 2003
Location: Bangalore ,Karnataka, India, Asia, Earth, Solar system, milky way galaxy, black hole
Distribution: murugesan openssl
Posts: 181

Rep: Reputation: 29
Handling exception when last line of input file without new line


Code:
$ export PS1='$ '
$ export PS2=''
$ # ----------------------Content of file ./EndOfFileNotHavingNewLine.txt Below:------
$ /bin/cat ./EndOfFileNotHavingNewLine.txt
ROW 01
ROW 02
ROW 03$ # ----------------------Content of file ./EndOfFileNotHavingNewLine.txt Above:------
$ /usr/bin/tac ./EndOfFileNotHavingNewLine.txt
ROW 03ROW 02
ROW 01
$ /bin/sed '1!G;h;$!d' ./EndOfFileNotHavingNewLine.txt
ROW 03
ROW 02
ROW 01
$ /bin/cat -e ./EndOfFileNotHavingNewLine.txt | /usr/bin/tail -1 | /bin/grep -E "\\$" >/dev/null 2>&1
$ export LAST_LINE_WITHOUT_NL=$?
$ # Using awk
$ /usr/bin/awk 'BEGIN {
        RowNum=0;
}
{
        arr[RowNum++]=$0;
}
END {
        RowNum--;
        if ( 0 != ENVIRON["LAST_LINE_WITHOUT_NL"] )
        {
                printf( "%s", arr[RowNum]);
        }
        else
        {
                printf( "%s\n", arr[RowNum]);
        }
        RowNum--;
        while ( 0 <= RowNum )
        {
                printf( "%s\n", arr[RowNum]);
                RowNum--;
        }
}' ./EndOfFileNotHavingNewLine.txt
ROW 03ROW 02
ROW 01
$
 
Old 11-03-2018, 02:47 PM   #2
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 26,695

Rep: Reputation: 7972Reputation: 7972Reputation: 7972Reputation: 7972Reputation: 7972Reputation: 7972Reputation: 7972Reputation: 7972Reputation: 7972Reputation: 7972Reputation: 7972
Quote:
Originally Posted by murugesan View Post
Code:
$ export PS1='$ '
$ export PS2=''
$ # ----------------------Content of file ./EndOfFileNotHavingNewLine.txt Below:------
$ /bin/cat ./EndOfFileNotHavingNewLine.txt
ROW 01
ROW 02
ROW 03$ # ----------------------Content of file ./EndOfFileNotHavingNewLine.txt Above:------
$ /usr/bin/tac ./EndOfFileNotHavingNewLine.txt
ROW 03ROW 02
ROW 01
$ /bin/sed '1!G;h;$!d' ./EndOfFileNotHavingNewLine.txt
ROW 03
ROW 02
ROW 01
$ /bin/cat -e ./EndOfFileNotHavingNewLine.txt | /usr/bin/tail -1 | /bin/grep -E "\\$" >/dev/null 2>&1
$ export LAST_LINE_WITHOUT_NL=$?
$ # Using awk
$ /usr/bin/awk 'BEGIN {
        RowNum=0;
}
{
        arr[RowNum++]=$0;
}
END {
        RowNum--;
        if ( 0 != ENVIRON["LAST_LINE_WITHOUT_NL"] )
        {
                printf( "%s", arr[RowNum]);
        }
        else
        {
                printf( "%s\n", arr[RowNum]);
        }
        RowNum--;
        while ( 0 <= RowNum )
        {
                printf( "%s\n", arr[RowNum]);
                RowNum--;
        }
}' ./EndOfFileNotHavingNewLine.txt
ROW 03ROW 02
ROW 01
$
Some reason you posted this in a non related thread that had been closed for six years?
 
Old 11-03-2018, 03:09 PM   #3
BW-userx
LQ Guru
 
Registered: Sep 2013
Location: Somewhere in my head.
Distribution: Slackware (15 current), Slack15, Ubuntu studio, MX Linux, FreeBSD 13.1, WIn10
Posts: 10,342

Rep: Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242
Quote:
The command "cat" is used to display a file (or to concatenate several).

To view a file in reverse (starting with the last line until the first), there is simply the "tac" (cat in reverse).

tac file

Like the command "cat", you can concatenate several files, which will be put together, but in reverse.

tac file1 file2 file3
looks to be all by itself no sed needed.. interesting, I never...

https://ccm.net/faq/2094-linux-displ...ile-in-reverse
 
Old 11-03-2018, 08:32 PM   #4
murugesan
Member
 
Registered: May 2003
Location: Bangalore ,Karnataka, India, Asia, Earth, Solar system, milky way galaxy, black hole
Distribution: murugesan openssl
Posts: 181

Original Poster
Rep: Reputation: 29
>> no /bin/sed needed.. interesting
Updated the comment =&gt; to display google search results.
=&gt; referring HTML code for =>
we have resolved those exceptions(C C++ and scripting at AIX CYGWIN_NT HP-UX Linux SunOS) from 2005 till 2013 and during 2017
using
1) XML files
2) EDI files
3) IDOC files from SAP

Quote:
$ echo "AIX CYGWIN_NT HP-UX Linux SunOS" | /usr/bin/tr " " "\n" | /usr/bin/sort -u | /usr/bin/awk '{ printf( "%s ", $0);} END { printf( "\b\n");}'
AIX CYGWIN_NT HP-UX Linux SunOS

Last edited by murugesan; 11-03-2018 at 08:38 PM. Reason: Replaced &nbsp; to >&gt;
 
Old 11-03-2018, 09:04 PM   #5
astrogeek
Moderator
 
Registered: Oct 2008
Distribution: Slackware [64]-X.{0|1|2|37|-current} ::12<=X<=15, FreeBSD_12{.0|.1}
Posts: 6,269
Blog Entries: 24

Rep: Reputation: 4196Reputation: 4196Reputation: 4196Reputation: 4196Reputation: 4196Reputation: 4196Reputation: 4196Reputation: 4196Reputation: 4196Reputation: 4196Reputation: 4196
Moved: Your post is more suitable in its own thread and has been moved accordingly to help your thread/question get the exposure it deserves.
 
Old 11-04-2018, 12:27 AM   #6
murugesan
Member
 
Registered: May 2003
Location: Bangalore ,Karnataka, India, Asia, Earth, Solar system, milky way galaxy, black hole
Distribution: murugesan openssl
Posts: 181

Original Poster
Rep: Reputation: 29
This thread related to
https://www.linuxquestions.org/quest...22#post4857122
since
2nd link searching at google given above url:
https://www.google.co.in/search?ei=v...ines+using+sed

Last edited by murugesan; 11-04-2018 at 12:28 AM. Reason: [url]related url[/url] without new line to exclude html related <BR>
 
Old 11-04-2018, 01:11 AM   #7
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,871
Blog Entries: 1

Rep: Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871
Hi, please clarify: do you have a question? Care to actually ask it?
 
Old 11-04-2018, 02:20 PM   #8
astrogeek
Moderator
 
Registered: Oct 2008
Distribution: Slackware [64]-X.{0|1|2|37|-current} ::12<=X<=15, FreeBSD_12{.0|.1}
Posts: 6,269
Blog Entries: 24

Rep: Reputation: 4196Reputation: 4196Reputation: 4196Reputation: 4196Reputation: 4196Reputation: 4196Reputation: 4196Reputation: 4196Reputation: 4196Reputation: 4196Reputation: 4196
Quote:
Originally Posted by murugesan View Post
This thread related to
https://www.linuxquestions.org/quest...22#post4857122
since
2nd link searching at google given above url:
https://www.google.co.in/search?ei=v...ines+using+sed
Showing up in an online word based search does not make two things related without additional context.

As you have not actually asked a question or explained why you have posted your code, there is no context for others to respond to your post.

The Programming forum is for asking questions about programming, and sharing what you have learned. Simply posting code and links is not an acceptable use of the forum.

Please review the Site FAQ for guidance in posting your questions and general forum usage. Especially, read the link in that page, How To Ask Questions The Smart Way. The more effort you put into understanding your problem and framing your questions, the better others can help!

Last edited by astrogeek; 11-04-2018 at 02:33 PM. Reason: typo
 
1 members found this post helpful.
  


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
Scripting question - feed an input file into an if statement line-by-line kmkocot Linux - Newbie 10 01-18-2010 11:49 AM
C++ input file reading "line by line" assamite Programming 2 05-31-2008 04:54 PM
php - Read file line by line and change a specific line. anrea Programming 2 01-28-2007 01:43 PM

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

All times are GMT -5. The time now is 08:31 PM.

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