Take 10 latest line data


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Take 10 latest line data
# 1  
Old 05-30-2015
Take 10 latest line data

Good day for us.
I want to ask what is the manner to count total of spesific character or string in 10 latest line. I mean from Latestline - 10 line until Latest line.
Example :
If the latest line of my file is 455th line, I just want to count total of spesific string from line 446th to 455th.

I have file that contains of character.

LatestTest.log
Code:
1=1
2=0
1=0
4=0
4=1
4=1
3=0
4=1
2=0
1=1
1=0
1=0
2=1
2=1

Total of line my LatestTest.log frequently changed (added) and I just want to take 10 latest data line.

for Example:
I want to take total of string (2=1) in 10 latest line.

Thank you..
# 2  
Old 05-30-2015
Your request is a little vague...

To get the number of the last ten lines of the file LatestTest.log that have entire lines that exactly match the string2=1, try:
Code:
tail LatestTest.log|grep -Fcx '2=1'

To get the number of the last ten lines of the file LatestTest.log that contain the string2=, try:
Code:
tail LatestTest.log|grep -Fc '2='

To get the number of times the string 2 appears in the last ten lines of the file LatestTest.log, try:
Code:
tail LatestTest.log|awk '{c+=gsub("2", "")}END{print c}'

If you want to process the last number lines of a file instead of the last ten lines, change tail filename to tail -n number filename.
# 3  
Old 05-30-2015
Succes

Thank you Mr Cragun..
Your code works well.
# 4  
Old 06-03-2015
Ask another cases

Hallo all,
Im sorry if I come again to this thread.
I want to ask how is the manner if I want to count spesific character from certain line to certain line.
For example in case :
This is my file looks like.

SpaceState.log
Code:
1
1
1
1
1
1
0
0
1
1
1
1
0
1
1
1
1
1
0
0
0
0
0
0
0
0
0
0
1
1

Actually my file contain of thousands line.
How to count for "1" character from 10th line to 20th line.
Thanks..
# 5  
Old 06-03-2015
Try
Code:
awk 'NR==10,NR==20 {CNT[$1]++} NR==20 {exit} END {print CNT[1]}' file
8

# 6  
Old 06-03-2015
Try:
Code:
awk 'NR<10{next} $1==1{c++} NR==20{print c; exit}' file


--
or
Code:
tail +10 file | head -10 | grep -Fcx 1

--
or
Code:
awk 'NR<b{next} $1==n{c++} NR==e{print c; exit}' b=10 e=20 n=1  file


--
note: the second option will not work correctly if lines may contain spaces or tabs..
--
On Solaris use /usr/xpg4/bin/awk, /usr/xpg4/bin/grep, /usr/xpg4/bin/tail

Last edited by Scrutinizer; 06-03-2015 at 05:03 PM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Mapping a data in a file and delete line in source file if data does not exist.

Hi Guys, Please help me with my problem here: I have a source file: 1212 23232 343434 ASAS1 4 3212 23232 343434 ASAS2 4 3234 23232 343434 QWQW1 4 1134 23232 343434 QWQW2 4 3212 23232 343434 QWQW3 4 and a mapping... (4 Replies)
Discussion started by: kokoro
4 Replies

2. Linux

How to Keep your core System and personal Data safe while updating to latest distro?

Hi everyone, Almost everything is in the title! Which partitions do you keep? Which partitions do you reformat, while doing a clean install? Personaly, I never format /var and /home partitions when I update to latest linux distribution. It has been working quite ok up to now, but I was... (3 Replies)
Discussion started by: freddie50
3 Replies

3. Shell Programming and Scripting

Help in adding a data after a particular line of data in a file.

Hi.. I'm into a bump after trying to solve this prob.. i've a file with contents like below. <blankline> 'pgmId' : 'UNIX', 'pgmData' : 'textfile', 'author' : 'admin', ....... Now i'm trying to insert a new data after pgmId. so the final output will be... (7 Replies)
Discussion started by: arjun_arippa
7 Replies

4. Shell Programming and Scripting

Delete duplicate data and pertain the latest month data.

Hi I have a file with following records It contains three months of data, some data is duplicated,i need to access the latest data from the duplicate ones. for e.g; i have foll data "200","0","","11722","-63","","","","11722","JUL","09" "200","0","","11722","-63","","","","11722","JUL","09"... (10 Replies)
Discussion started by: vee_789
10 Replies

5. Shell Programming and Scripting

Formatting a text file to get data in exact line by line

I have my data something like this SERIAL FIRSTOCCURRENCE NETPROTOCOL 1947430693 07/01/2009 05:16:40 FR SERIAL FIRSTOCCURRENCE NETPROTOCOL 1947430746 07/01/2009 05:18:05 FR I want the output as follows.... (1 Reply)
Discussion started by: rdhanek
1 Replies

6. UNIX for Dummies Questions & Answers

To check if the latest version of given GDG base has data

Hi All , I am trying to run a shell script through a JCL . The requirement is I have a gdg base name and I need to create a script that will just check if the latest version of that gdg has data or not . If it doesnt have data RC 4 need to be returned . One more thing which is bothering me is i... (1 Reply)
Discussion started by: mavesum
1 Replies

7. Shell Programming and Scripting

To check if the latest version of given GDG base has data

Hi All , I am trying to run a shell script through a JCL . The requirement is I have a gdg base name and I need to create a script that will just check if the latest version of that gdg has data or not . If it doesnt have data RC 4 need to be returned . One more thing which is bothering me is i... (3 Replies)
Discussion started by: mavesum
3 Replies

8. Shell Programming and Scripting

how to get the data from line number 1 to line number 100 of a file

Hi Everybody, I am trying to write a script that will get some perticuler data from a file and redirect to a file. My Question is, I have a Very huge file,In that file I have my required data is started from 25th line and it will ends in 100th line. I know the line numbers, I need to get all... (9 Replies)
Discussion started by: Anji
9 Replies

9. Shell Programming and Scripting

compare data line by line from a file

Hi there How can I compare data line by line from a file? I need to compare the second value with the fourth to know if they are different. If those values are different, I require to send my first value to the output until the complete file has been read. This is my file: 0 FALSE... (1 Reply)
Discussion started by: loperam
1 Replies

10. UNIX for Dummies Questions & Answers

get data from next line

Hi using awk I know I can get all the lines with gold from a file called "gold.txt" like the one below: gold 1 1986 USA American Eagle gold 1 1908 Austria-Hungary Franz Josef 100 Korona silver 10 1981 USA ingot gold 1 ... (5 Replies)
Discussion started by: whamchaxed
5 Replies
Login or Register to Ask a Question