BASH logging to file, limit lines


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting BASH logging to file, limit lines
# 1  
Old 08-30-2017
Code BASH logging to file, limit lines

BASH Gurus: Anyone know how to append continuous output command appending to a file, but limit that file to no more than 20 lines? The program I have running is simply monitoring my UDP port 53 for incoming packets endlessly. I just need to keep this file from going over 20 lines. Once the file reaches 21 lines, the 1st line of the file should be removed and so on.

I have used sed, which does this perfectly:
Code:
sed "1d" log.txt

Here is the command below, when ran, it continuously logs the incoming packets
Code:
netcat -ul -p 53 >> log.txt

I just don't know how to combine these in one command (piping would be great). Open to any ideas.

First post on the forums, thanks and hello to all! Smilie
# 2  
Old 08-30-2017
Assuming that log.txt contains 20 lines of text when you start the following script:
Code:
netcat -ul -p 53 | while IFS='' read -r line
do	sed 1d log.txt > _log.txt
	{ cat _log.txt; printf '%s\n' "$line"; } > log.txt
done

might come close to doing what you want.
This User Gave Thanks to Don Cragun For This Post:
# 3  
Old 08-30-2017
Quote:
Originally Posted by Don Cragun
Assuming that log.txt contains 20 lines of text when you start the following script:
Code:
netcat -ul -p 53 | while IFS='' read -r line
do	sed 1d log.txt > _log.txt
	{ cat _log.txt; printf '%s\n' "$line"; } > log.txt
done

might come close to doing what you want.
I am very grateful for your reply, that did it perfectly! I simply used a
Code:
LINECOUNT=$(wc -l < "log.txt")

to grab the number of lines in the file and put on space in any remaining. Easy enough to parse out the spaces and display data

I cannot thank you enough Don, many, MANY thanks! SmilieSmilieSmilie
# 4  
Old 08-31-2017
While Don Cragun's fine proposal works brilliantly, it preserves the line count on hand, be it 8 or 800. Try piping into this (shamelessly stolen from Don Cragun) to make log.txt exactly 20 lines long, no matter what its size be:
Code:
while IFS='' read -r line
  do    tail -19 log.txt > _log.txt
        { cat _log.txt
          printf '%s\n' "$line"
        } > log.txt
  done

This User Gave Thanks to RudiC For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Limit the number of characters in bash output

Hi, I need some help with this: I'm making a script which does a couple of things with image files. The script is supposed to echo the number of each image it is processing like this: Processing image1.jpgThe problem is with images with very long filenames, so I want to know how to limit the... (5 Replies)
Discussion started by: Shadow_Reaper
5 Replies

2. UNIX for Advanced & Expert Users

Limit on amount of lines less can handle

Is there a limit on the amount of lines less can handle? I grepped for something that is pretty common in my files so I tried to filter it through less so it was easier to look at. So I then used a redirect ">" to create a file. It created a file that was 103K long. So that makes me think there is... (6 Replies)
Discussion started by: cokedude
6 Replies

3. Shell Programming and Scripting

Bash script to send lines of file to new file based on Regex

I have a file that looks like this: cat includes CORP-CRASHTEST-BU e:\crashplan\ CORP-TEST /usr/openv/java /usr/openv/logs /usr/openv/man CORP-LABS_TEST /usr/openv/java /usr/openv/logs /usr/openv/man What I want to do is make three new files with just those selections. So the three... (4 Replies)
Discussion started by: newbie2010
4 Replies

4. Shell Programming and Scripting

Reading from a file with limit number of lines

Hi, I am trying to pull data from a txt file which has 51 lines Example AK AR AL AZ CA CO CT Now i want to send above data as input to script but i want to run them twice at a time in a nohup like nohup Script_name AK & (3 Replies)
Discussion started by: krux_rap
3 Replies

5. Shell Programming and Scripting

Getting Same Lines from File(BASH)

hey all, ./test #!/bin/bash for line in $(cat data1) do echo $line done data1 ello there nobody says a word Output after running the file : hello (4 Replies)
Discussion started by: eawedat
4 Replies

6. UNIX for Dummies Questions & Answers

Limit reoccurrance of characters in lines

Hey guys and gals, Working on a script to limit the reoccurrance of characters in a line. sed "/\(.\).*\1/d" -i file.txt sed "/\(.\).*\1.*\1/d" -i file.txt sed "/\(.\).*\1.*\1.*1/d" -i file.txt .. To limit character reoccurance with 1x, 2x, 3x etc. However I would like to be able to... (2 Replies)
Discussion started by: TAPE
2 Replies

7. Shell Programming and Scripting

bash logging al $() command lines

I have been doing a lot more bash on LINUX RedHat and Ubuntu lately, and one thing keeps cropping up intermittently. If I do a $( some-commands ) Command Substitution, the some-commands are logged onto my screen each time they are evaluated. Did I turn on some odd option? It seems to happen just... (13 Replies)
Discussion started by: DGPickett
13 Replies

8. UNIX for Dummies Questions & Answers

Limit the number of characters in a bash output

I have a script that outputs the weather on two lines. If possibly I would like to set a character limit on them Currently it outputs something like but I would like to limit the lines so appends an ellipsis if nescessary: This is the script #! /bin/bash curl -s --connect-timeout... (2 Replies)
Discussion started by: Light_
2 Replies

9. Shell Programming and Scripting

Logging ALL standard out of a bash script to a log file, but still show on screen

Is it possible to store all standard-out of a bash script and the binaries it calls in a log file AND still display the stdout on screen? I know this is possible to store ALL stdout/stderr of a script to a single log file like: exec 1>&${logFile} exec 2>&1 But running a script with the... (3 Replies)
Discussion started by: ckmehta
3 Replies

10. Shell Programming and Scripting

bash telnet session logging

I'm looking at allowing remote telnet into my server. like any security-minded administrator, I want to log what my users type on the telnet session. I'm using the script command to generate transcripts of the users session. I have /etc/profile set to automatically start the script command... (2 Replies)
Discussion started by: ramnet
2 Replies
Login or Register to Ask a Question