Read the line from the last and display the one which is non-zero


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Read the line from the last and display the one which is non-zero
# 8  
Old 02-21-2015
Quote:
Originally Posted by hariniiyer300
Hi All,

My requirement is this. Am trying to find a one liner unix command, which will check the last line of the file for 0. If it is zero, it would pick the previous line, else it should display the last line.

Eg:
Code:
abcd
efgh
ijhk
0
0

Output expected -- ijhk

Please share your ideas.

Thanks.
I am glad that you have code that does what you want. But, it does not do what you requested. You said that if the last line is 0, it should print the previous line. The previous line is the next to the last line of the file (which also contains 0; not ijhk which is the next to the next to the last line).

Code that seems to do what you requested, even though it is not what you apparently want, is:
Code:
tail -n 2 file | (
	read -r prev
	read -r last
	if [ "$last" = '0' ]
	then	printf '%s\n' "$prev"
	else	printf '%s\n' "$last"
	fi
)

or as a 1-liner:
Code:
tail -n2 file|(read -r prev;read -r last;[ "$last" = '0' ]&&printf '%s\n' "$prev"||printf '%s\n' "$last")

If your system has a really old version of the tail (and it doesn't support a -n option, change the:
Code:
tail -n2 file

in either script to:
Code:
tail -2 file

Note that either of these scripts will give you an empty line of output if your input file doesn't contain at least two lines. Your problem statement didn't say anything about what should happen if there are less than two lines in your input file.
# 9  
Old 02-23-2015
Quote:
Originally Posted by Don Cragun
I am glad that you have code that does what you want. But, it does not do what you requested. You said that if the last line is 0, it should print the previous line. The previous line is the next to the last line of the file (which also contains 0; not ijhk which is the next to the next to the last line).

Code that seems to do what you requested, even though it is not what you apparently want, is:
Code:
tail -n 2 file | (
    read -r prev
    read -r last
    if [ "$last" = '0' ]
    then    printf '%s\n' "$prev"
    else    printf '%s\n' "$last"
    fi
)

or as a 1-liner:
Code:
tail -n2 file|(read -r prev;read -r last;[ "$last" = '0' ]&&printf '%s\n' "$prev"||printf '%s\n' "$last")

If your system has a really old version of the tail (and it doesn't support a -n option, change the:
Code:
tail -n2 file

in either script to:
Code:
tail -2 file

Note that either of these scripts will give you an empty line of output if your input file doesn't contain at least two lines. Your problem statement didn't say anything about what should happen if there are less than two lines in your input file.

Many thanks Mr. Don Cragun. My current requirement is if the line before the last line is also 0 , then I would want to print the line which has non -zero value in it. And as noted that that code might not work for less than 2 lines. But I will definitely have the first line loaded with value.

- Meenakshi
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to read file line by line and compare subset of 1st line with 2nd?

Hi all, I have a log file say Test.log that gets updated continuously and it has data in pipe separated format. A sample log file would look like: <date1>|<data1>|<url1>|<result1> <date2>|<data2>|<url2>|<result2> <date3>|<data3>|<url3>|<result3> <date4>|<data4>|<url4>|<result4> What I... (3 Replies)
Discussion started by: pat_pramod
3 Replies

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

3. UNIX for Dummies Questions & Answers

To find and display the middle line in a file using single line command.

Hi all, How can i display the middle line of a file using a single line command? (6 Replies)
Discussion started by: Lakme Pemmaiah
6 Replies

4. Shell Programming and Scripting

a little help with using AWK to display whats being read in

I am making a script that reads in the model of a car and then searches a file and displays the make model and price of anything matching the input provided. here is what I have so far #!/bin/sh echo Please enter a car model: read model if test $? -eq 0 then grep $model /home/cars awk... (4 Replies)
Discussion started by: subway69
4 Replies

5. Shell Programming and Scripting

how to read the contents of two files line by line and compare the line by line?

Hi All, I'm trying to figure out which are the trusted-ips and which are not using a script file.. I have a file named 'ip-list.txt' which contains some ip addresses and another file named 'trusted-ip-list.txt' which also contains some ip addresses. I want to read a line from... (4 Replies)
Discussion started by: mjavalkar
4 Replies

6. Emergency UNIX and Linux Support

Grab total page read from webalizer display in html

Hi, I need a way to grab the total combines since inception, total pages read from webalizer on my centos server or any other location (as long as since inception) and display the result live on my website So with each visit it would be increasing, or perhaps live (ajax) not sure But can... (0 Replies)
Discussion started by: lawstudent
0 Replies

7. Shell Programming and Scripting

Read File and Display The Count of a particular field

Hi Mates, I require help in the following: I have the following file snmp.txt Wed Mar 2 16:02:39 SGT 2011 Class : mmTrapBladeS origin : 10.0.0.0 hostname : 10.0.0.2 msg : IBM Blade Alert: Calendar Index : 10.0.0.2-IBMBLADE Fri Mar 4 07:10:54 SGT 2011 Class : mmTrapBladeS... (2 Replies)
Discussion started by: dbashyam
2 Replies

8. Shell Programming and Scripting

bash: read file line by line (lines have '\0') - not full line has read???

I am using the while-loop to read a file. The file has lines with null-terminated strings (words, actually.) What I have by that reading - just a first word up to '\0'! I need to have whole string up to 'new line' - (LF, 10#10, 16#A) What I am doing wrong? #make file 'grb' with... (6 Replies)
Discussion started by: alex_5161
6 Replies

9. Shell Programming and Scripting

Display mutiple line in single line

Hi All, I had a file called Input.txt, i need to group up in a single line as 1=ttt and the no of lines may vary bewteen the 1=ttt cat Input.txt 1=ttt,2=xxxxxx, 3=4545 44545, 4=66667 7777, 5=77723 1=ttt, 2=xxxxxx, 3=34436 66 3545, 4=66666, 5=ffffff, 6=uuuuuuu 1=ttt, 2=xxxxxx,... (4 Replies)
Discussion started by: manosubsulo
4 Replies

10. Shell Programming and Scripting

How to read variable with no display

I'll be reading user name and password from the person while running a shell script so that he is authenticated. The challenge here is to read the password variable without displaying on screen. Is there a way? I presently do it displaying it on the screen as echo " please enter your... (9 Replies)
Discussion started by: dayanandra
9 Replies
Login or Register to Ask a Question