Print first 20 lines from all .log files


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Print first 20 lines from all .log files
# 1  
Old 04-29-2013
Print first 20 lines from all .log files

RHEL 5.8

In a directory I have more than 200 files with the extension .log

Using head command I want to print first 20 lines of each log file.
For each .log file, it should print like below

Code:
Printing first 20 lines of : GRP_error_April29.log


Apr 29 04:02:05 raptor03b syslogd 1.4.1: restart.
Apr 29 04:05:38 raptor03b cf3[5259]:   !! Authentication dialogue with 10.79.16.158 failed
Apr 29 04:05:39 raptor03b cf3[5259]:  R:  !! Failed to pull policy from policy server
Apr 29 04:05:39 raptor03b cf3[5259]:  R:  !! Did not start the scheduler
Apr 29 04:05:39 raptor03b cf3[5298]:  There is no readable input file at promises.cf
Apr 29 04:05:39 raptor03b cf3[5298]:   !!! System error for stat: "No such file or directory"
Apr 29 04:05:39 raptor03b cf3[5298]:  cf-agent was not able to get confirmation of promises from cf-promises, so going to failsafe
Apr 29 04:10:38 raptor03b cf3[7715]:   !! Authentication dialogue with 10.193.60.158 failed
Apr 29 04:10:38 raptor03b cf3[7715]:  R:  !! Failed to pull policy from policy server
Apr 29 04:10:39 raptor03b cf3[7715]:  R:  !! Did not start the scheduler
Apr 29 04:10:39 raptor03b cf3[7760]:  There is no readable input file at promises.cf
Apr 29 04:10:39 raptor03b cf3[7760]:   !!! System error for stat: "No such file or directory"
Apr 29 04:10:39 raptor03b cf3[7760]:  cf-agent was not able to get confirmation of promises from cf-promises, so going to failsafe
Apr 29 04:15:38 raptor03b cf3[10314]:   !! Authentication dialogue with 10.193.60.158 failed
Apr 29 04:15:39 raptor03b cf3[10314]:  R:  !! Failed to pull policy from policy server
Apr 29 04:15:39 raptor03b cf3[10314]:  R:  !! Did not start the scheduler
Apr 29 04:15:39 raptor03b cf3[10393]:  There is no readable input file at promises.cf
Apr 29 04:15:39 raptor03b cf3[10393]:   !!! System error for stat: "No such file or directory"
Apr 29 04:15:39 raptor03b cf3[10393]:  cf-agent was not able to get confirmation of promises from cf-promises, so going to failsafe
Apr 29 04:20:38 raptor03b cf3[13343]:   !! Authentication dialogue with 10.79.16.158 failed

Any idea how I can do this ?
# 2  
Old 04-29-2013
Code:
#!/bin/bash
for f in /directory/*.log
 do
 echo "print the 20 first line of :$f"
 echo "---"
 head -n 20 $f
 echo ""
done

This User Gave Thanks to Jotne For This Post:
# 3  
Old 04-29-2013
I dont think we need for loop there..
Code:
head -20 *

should work..
This User Gave Thanks to vidyadhar85 For This Post:
# 4  
Old 04-29-2013
Without the loop , I can't get the custom string
Printing first 20 lines of : <fileName>

printed
# 5  
Old 04-29-2013
Code:
awk 'FNR == 1 { print "Printing first 20 lines of: " FILENAME } { print } FNR == 20 { nextfile }' *.log

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

How to print lines from a files with specific start and end patterns and pick only the last lines?

Hi, I need to print lines which are matching with start pattern "SELECT" and END PATTERN ";" and only select the last "select" statement including the ";" . I have attached sample input file and the desired input should be as: INPUT FORMAT: SELECT ABCD, DEFGH, DFGHJ, JKLMN, AXCVB,... (5 Replies)
Discussion started by: nani2019
5 Replies

2. UNIX for Beginners Questions & Answers

Print number of lines for files in directory, also print number of unique lines

I have a directory of files, I can show the number of lines in each file and order them from lowest to highest with: wc -l *|sort 15263 Image.txt 16401 reference.txt 40459 richtexteditor.txt How can I also print the number of unique lines in each file? 15263 1401 Image.txt 16401... (15 Replies)
Discussion started by: spacegoose
15 Replies

3. Shell Programming and Scripting

Print n lines from top and n lines from bottom of all files with .log extenstion

Oracle Linux 6.4 In a directory I have more than 300 files with the extension .log I want the first 5 and last 5 lines of these .log files to be printed on screen with each file's name. Expected output : Printing first 5 and last 5 lines of FX_WT_Feb8_2014.log !! Authentication... (7 Replies)
Discussion started by: kraljic
7 Replies

4. Shell Programming and Scripting

Copy/print all lines between pattern is found in .log files

Hi, I have a folder with multiple (< 33) .log files. And I have to copy the lines between two patterns from all the .log files to a new file. (script file with a loop?) Thanks in advance. 1.log ... .. xx1> begin ... .. .. >>> Total: 2 Alarms .. .. (17 Replies)
Discussion started by: AK47
17 Replies

5. Shell Programming and Scripting

Look up between 2 files and print matching lines

Hi, I have 2 large log files in .gz format file 1 contains abcde 12345 23456 . . . . . . . . 09123 (8 Replies)
Discussion started by: aravindj80
8 Replies

6. Shell Programming and Scripting

AWK print lines into multiple files

Hi, i have an input text file like this: Student 1 maths science = Student 2 maths science = Student 3 maths science i would like to print each student information into separate files, each student id is separated by "=". (1 Reply)
Discussion started by: saint2006
1 Replies

7. Shell Programming and Scripting

print lines AFTER lines cointaining a regexp (or print every first and fourth line)

Hi all, This should be very easy but I can't figure it out... I have a file that looks like this: @SRR057408.1 FW8Y5CK02R652T length=34 AGCAGTGGTATCAACGCAGAGTAAGCAGTGGTAT +SRR057408.1 FW8Y5CK02R652T length=34 FIIHFF6666?=:88@@@BBD:::?@ABBAAA>8 @SRR057408.2 FW8Y5CK02TBMHV length=52... (1 Reply)
Discussion started by: kmkocot
1 Replies

8. Shell Programming and Scripting

how to print the certain lines in a file to different files

Hi All, File that I have: <ct> <name>group <value>1 <value>2 <value>3 </ct>-->file The output that I needed is <ct> <name>group <value>1 -->file1 <ct> <name>group <value>2 -->file2 (6 Replies)
Discussion started by: natalie23
6 Replies

9. Shell Programming and Scripting

Compare two files and print the two lines with difference

I have two files like this: #FILE 1 ABCD 4322 26485 JMTJ 5311 97248 XMPJ 4321 58978 #FILE 2 ABCD 4321 26485 JMTJ 5311 97248 XMPJ 4321 68978 What to do: Compare the two files and find those lines that doesn't match. And have a new file like this: #FILE 3 "from file 1" ABCD 4322 26485... (11 Replies)
Discussion started by: kingpeejay
11 Replies

10. Shell Programming and Scripting

print the first 5 lines in which 2 files differs

it is a simple problem, but I am totally new to shell programming There are 2 files. I have to print the 5 five lines which are different. I appreciate any help. 10x. (2 Replies)
Discussion started by: atticus
2 Replies
Login or Register to Ask a Question