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
# 1  
Old 02-17-2015
Read the line from the last and display the one which is non-zero

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.

Last edited by rbatte1; 02-17-2015 at 08:50 AM.. Reason: Added CODE tags
# 2  
Old 02-17-2015
Hello hariniiyer300,

Kindly use code tags as per forum rules for inputs/commands/codes used in your posts, following may help you in same.
Code:
awk '($0 == 0 && A){print A} {A=$1}'  Input_file

Output will be as follows.
Code:
ijhk

Thanks,
R. Singh
# 3  
Old 02-17-2015
RavinderSingh13's proposal would print any line followed by a "0" line. Try instead
Code:
awk '$0 != 0 {A=$0}  END {print A}'  file
ijhk

These 2 Users Gave Thanks to RudiC For This Post:
# 4  
Old 02-17-2015
Thanks for the reply R.Singh.

I am getting the output as expected when the last line is 0. But if the last line is not zero, am not getting any output from the command. I would want to display the last line as it is, if it is not 0.

Please share your thoughts.
# 5  
Old 02-17-2015
Code:
sed '/^0*$/!h;$g;$!d' file

# 6  
Old 02-17-2015
Save testing, delete earlier:
Code:
sed '/^0*$/!h ; $!d ; g' file

# 7  
Old 02-20-2015
Thanks to everyone for the reply. The awk command by RudiC solved the problem.

Many thanks again for the prompt response Smilie
 
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