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


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Print n lines from top and n lines from bottom of all files with .log extenstion
# 1  
Old 02-11-2014
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 :

Code:
Printing first 5 and last 5 lines of FX_WT_Feb8_2014.log

 !! Authentication dialogue with 10.79.60.158 failed
   R:  !! Failed to pull policy from policy server
   R:  !! Did not start the scheduler
   There is no readable input file at promises.cf
    !!! System error for stat: "No such file or directory"
   
 
 !! Authentication dialogue with 10.79.60.158 failed
    R:  !! Failed to pull policy from policy server
    R:  !! Did not start the scheduler
    cf-agent was not able to get confirmation of promises from cf-promises, so going to failsafe
There is no readable input file at promises.cf

# 2  
Old 02-11-2014
Try the below code

Code:
#!/bin/ksh
ls *.log > list_of_log_files
for file in `cat list_of_log_files`
do
echo "$file"
head -5 $file
tail -5 $file
done
echo "Files are completed"

Please update first line (the shell type) in your script. you can find the script type by
executing echo $SHELL

Hope it helps

Last edited by Don Cragun; 02-11-2014 at 08:25 AM.. Reason: Add CODE tags.
This User Gave Thanks to kung_fu_panda For This Post:
# 3  
Old 02-11-2014
Note: Instead of
Code:
ls *.log > list_of_log_files
for file in `cat list_of_log_files`

A better option is:
Code:
for file in *.log

These 2 Users Gave Thanks to Scrutinizer For This Post:
# 4  
Old 02-11-2014
Here are a couple of ways to do it that match the requested output format a little closer:
Code:
#!/bin/ksh
n=0
for file in *.log
do      [ $((n++)) -gt 0 ] && echo
        printf "Printing first 5 and last 5 lines of %s.\n\n" "$file"
        head -n 5 "$file"
        printf "\n\n\n"
        tail -n 5 "$file"
done

If this fails because the expansion of *.log exceeds ARG_MAX limitations, you can use this instead:
Code:
n=0
ls | while IFS='' read -r file
do      case "$file" in
        (*.log) [ $((n++)) -gt 0 ] && echo
                printf "Printing first 5 and last 5 lines of %s.\n\n" "$file"
                head -n 5 "$file"
                printf "\n\n\n"
                tail -n 5 "$file";;
        esac
done

If you don't want an empty line between reports for separate files, comment out or remove the code shown in red.

Although written for ksh, both of these will also work with bash or any other shell that supports standard POSIX shell syntax.
This User Gave Thanks to Don Cragun For This Post:
# 5  
Old 02-11-2014
Quote:
Originally Posted by Don Cragun
[..]
If this fails because the expansion of *.log exceeds ARG_MAX limitations, you can use this instead:
[..]
IMO, the expansion of *.log cannot exceed ARG_MAX limitations since for .. in is shell syntax, so there is no new process to which arguments need to be passed..

Last edited by Scrutinizer; 02-11-2014 at 09:23 AM..
These 3 Users Gave Thanks to Scrutinizer For This Post:
# 6  
Old 02-11-2014
Quote:
Originally Posted by Scrutinizer
IMO, the expansion of *.log cannot exceed ARG_MAX limitations since for .. in is shell syntax, so there is no new process to which arguments need to be passed..
Yes, you are absolutely correct.

I was looking at ls *.log in kung_fu_panda's proposal (which can exceed ARG_MAX limitations). I should have gotten some sleep before I posted my response.

Thanks,
Don
# 7  
Old 02-11-2014
Out of curiosity...

[C|W]ould either find *.log > result;while read line; do ... done<result) or for found in $(find *log);do ... reach ARG_MAX limitations?
Mainly asking, would "find" be a better 'tool' than "ls"?

EDIT:
Obviously it would require ls -r to be compareable with find

Last edited by sea; 02-11-2014 at 02:43 PM.. Reason: I'm on Windows, dont blame me for small/CAPS arguments, cant know (be certain of) them all...
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. UNIX for Dummies Questions & Answers

awk - (URGENT!) Print lines sort and move lines if match found

URGENT HELP IS NEEDED!! I am looking to move matching lines (01 - 07) from File1 and 77 tab the matching string from File2, to File3.txt. I am almost done but - Currently, script is not printing lines to File3.txt in order. - Also the matching lines are not moving out of File1.txt ... (1 Reply)
Discussion started by: High-T
1 Replies

4. Shell Programming and Scripting

How to print from bottom to top?

Hi, i have a file which contains PID and wanted to execute kill command. but the thing is, when killing PID's needs to kill PID from bottom to top. Please help INPUT 21414 sh -c extract.ksh ASA 21416 /bin/ksh extract.ksh ASA 21428 /usr/bin/perl -w /var/tmp/tempperl.21416 ASA... (4 Replies)
Discussion started by: reignangel2003
4 Replies

5. Shell Programming and Scripting

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 Printing first 20 lines of : GRP_error_April29.log Apr 29 04:02:05 raptor03b syslogd 1.4.1:... (4 Replies)
Discussion started by: John K
4 Replies

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

7. Shell Programming and Scripting

Remove x lines form top and y lines form bottom using AWK?

How to remove x lines form top and y lines form bottom. This works, but like awk only cat file | head -n-y | awk 'NR>(x-1)' so remove last 3 lines and 5 firstcat file | head -n-3 | awk 'NR>4' (5 Replies)
Discussion started by: Jotne
5 Replies

8. Shell Programming and Scripting

Search for string and print top and bottom line

Hi Folks I need a one liner to parse through a log and if the string is found print the line above, the line with the string and the line below. example: The ball is green and blue Billy through the ball higer. Jane got hurt with the ball. So if I search for Billy I would need the 3... (1 Reply)
Discussion started by: bombcan
1 Replies

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

10. Shell Programming and Scripting

grep for a particular pattern and remove few lines above top and bottom of the patter

grep for a particular pattern and remove 5 lines above the pattern and 6 lines below the pattern root@server1 # cat filename Shell Programming and Scripting test1 Shell Programminsada asda dasd asd Shell Programming and Scripting Post New Thread Shell Programming and S sadsa ... (17 Replies)
Discussion started by: fed.linuxgossip
17 Replies
Login or Register to Ask a Question