Print the last line of a file, from the CLI
$ cat file | awk 'END{print}'
Originally answered by Ventero
Last line of a file is not reading in shell script
This is due to missing line break in the last line of your input file.
You can use this loop to read everything:
while IFS= read -r line || [ -n "$line" ]; do
echo "$line"
done < "$file"
For the last line without line break, read
doesn't return a success hence [ -n "$line" ]
check is done to make sure to print it when $line
is not empty.
PS: If you don't mind changing your input file then use printf
to append a newline using:
printf '\n' >> "$file"
And then read normally:
while IFS= read -r line; do
echo "$line"
done < "$file"
How to get the last line of a file using cat command
Don't use cat
. tail
was meant for this usecase exactly:
$ tail -1 ./test.properties
How to only read the last line from a text file
Use tail
;)
line=$(tail -n 1 input_file)
echo $line
Perl code to check the last line number of a file
If you want the last line number, wait to print $.
until outside the while
loop for processing the file:
open my $fh, '<', "$qbsid_dir/$file" or die "Can't open $file: $!";
1 while (<$fh>);
print "$file -> $.\n";
close $fh;
Be sure to read: perlfaq5 - How do I count the number of lines in a file?
Windows equivalent of the 'tail' command
No exact equivalent. However there exist a native DOS command "more" that has a +n option that will start outputting the file after the nth line:
DOS Prompt:
C:\>more +2 myfile.txt
The above command will output everything after the first 2 lines.
This is actually the inverse of Unix head:
Unix console:
root@server:~$ head -2 myfile.txt
The above command will print only the first 2 lines of the file.
how to print variables after each file with awk
Action you are looking to be done should be covered with ENDFILE
, try following code once. This is a GNU awk
option, should work for your shown versioned of awk
.
awk -F"'" '/export const/ { CLSNM = $0 } ENDFILE{ print CLSNM;CLSNM="" }' *
2nd solution: without EBNDFILE
option try following, should work with any awk
version.
awk -F"'" 'FNR==1 && CLSNM{ print CLSNM; CLSNM=""} /export const/ { CLSNM = $0 } END{if(CLSNM){print CLSNM}}' *
continuously print the last line of a file Linux termin
Use the UNIX command "tail" with the -f option. That will continuously print out contents from the file to the terminal as it is added to the file.
Example:
tail -f emptyfile
You can terminate the tail process by typing Ctrl + C.
I am trying to print the last line of every file in a directory using shell command from python script
Your shell scripting is orders of magnitude too complex.
output = subprocess.check_output('tail -qn1 *', shell=True)
or if you really prefer,
os.system('tail -qn1 *')
which however does not capture the output in a Python variable.
If you have a recent-enough Python, you'll want to use subprocess.run()
instead. You can also easily let Python do the enumeration of the files to avoid the pesky shell=True
:
output = subprocess.check_output(['tail', '-qn1'] + os.listdir('.'))
As noted above, if you genuinely just want the output to be printed to the screen and not be available to Python, you can of course use os.system()
instead, though subprocess
is recommended even in the os.system()
documentation because it is much more versatile and more efficient to boot (if used correctly). If you really insist on running one tail
process per file (perhaps because your tail
doesn't support the -q
option?) you can do that too, of course:
for filename in os.listdir('.'):
os.system("tail -n 1 '%s'" % filename)
This will still work incorrectly if you have a file name which contains a single quote. There are workarounds, but avoiding a shell is vastly preferred (so back to subprocess
without shell=True
and the problem of correctly coping with escaping shell metacharacters disappears because there is no shell to escape metacharacters from).
for filename in os.listdir('.'):
print(subprocess.check_output(['tail', '-n1', filename]))
Finally, tail
doesn't particularly do anything which cannot easily be done by Python itself.
for filename in os.listdir('.'):
with open (filename, 'r') as handle:
for line in handle:
pass
# print the last one only
print(line.rstrip('\r\n'))
If you have knowledge of the expected line lengths and the files are big, maybe seek
to somewhere near the end of the file, though obviously you need to know how far from the end to seek in order to be able to read all of the last line in each of the files.
Related Topics
Self Modifying Code Always Segmentation Faults on Linux
How to Pass a Wildcard Parameter to a Bash File
Extract Lines Between Two Patterns from a File
Using "$Random" to Generate a Random String in Bash
How Does Bash Deal with Nested Quotes
How to Trim White Space from a Variable in Awk
How to Remove the Bom from a Utf-8 File
Creating a Bootable Iso Image with Custom Bootloader
What Do These Kernel Panic Errors Mean
Linux Time Command Microseconds or Better Accuracy
What Is Path //, How Is It Different from /
Linux Removing Folders Older Than 1 Year and More Than 3 Files
How to Escape Colon (:) in $Path on Unix
Udp-Broadcast on All Interfaces
Perl Command Line Multi-Line Replace
Is It Ok to Use the Same Input File as Output of a Piped Command