text-files

How do I limit (or truncate) text file by number of lines?

别来无恙 提交于 2019-11-30 04:38:44
I would like to use a terminal/shell to truncate or otherwise limit a text file to a certain number of lines. I have a whole directory of text files, for each of which only the first ~50k lines are useful. How do I delete all lines over 50000? In-place truncation To truncate the file in-place with sed, you can do the following: sed -i '50001,$ d' filename -i means in place. d means delete. 50001,$ means the lines from 50001 to the end. You can make a backup of the file by adding an extension argument to -i , for example, .backup or .bak : sed -i.backup '50001,$ d' filename In OS-X or FreeBSD

Using a SQL Server for application logging. Pros/Cons?

随声附和 提交于 2019-11-30 03:09:29
I have a multi-user application that keeps a centralized logfile for activity. Right now, that logging is going into text files to the tune of about 10MB-50MB / day. The text files are rotated daily by the logger, and we keep the past 4 or 5 days worth. Older than that is of no interest to us. They're read rarely: either when developing the application for error messages, diagnostic messages, or when the application is in production to do triage on a user-reported problem or a bug. (This is strictly an application log. Security logging is kept elsewhere.) But when they are read, they're a pain

Use wc on all subdirectories to count the sum of lines

自古美人都是妖i 提交于 2019-11-30 02:43:07
How can I count all lines of all files in all subdirectories with wc ? cd mydir wc -l * .. 11723 total man wc suggests wc -l --files0-from=- , but I do not know how to generate the list of all files as NUL-terminated names find . -print | wc -l --files0-from=- did not work. You probably want this: find . -type f -print0 | wc -l --files0-from=- If you only want the total number of lines, you could use find . -type f -exec cat {} + | wc -l Perhaps you are looking for exec option of find . find . -type f -exec wc -l {} \; | awk '{total += $1} END {print total}' To count all lines for specific

How to configure GNU Emacs to write UNIX or DOS formatted files by default?

喜夏-厌秋 提交于 2019-11-30 01:54:42
I've had these functions in my .emacs.el file for years: (defun dos2unix () "Convert a DOS formatted text buffer to UNIX format" (interactive) (set-buffer-file-coding-system 'undecided-unix nil)) (defun unix2dos () "Convert a UNIX formatted text buffer to DOS format" (interactive) (set-buffer-file-coding-system 'undecided-dos nil)) These functions allow me to easily switch between formats, but I'm not sure how to configure Emacs to write in one particular format by default regardless of which platform I'm using. As it is now, when I run on Windows, Emacs saves in Windows format; when I run in

How to overwrite a folder if it already exists when creating it with makedirs?

本小妞迷上赌 提交于 2019-11-30 01:26:00
The following code allows me to create a directory if it does not already exist. dir = 'path_to_my_folder' if not os.path.exists(dir): os.makedirs(dir) The folder will be used by a program to write text files into that folder. But I want to start with a brand new, empty folder next time my program opens up. Is there a way to overwrite the folder (and create a new one, with the same name) if it already exists? inspectorG4dget import os import shutil dir = 'path_to_my_folder' if os.path.exists(dir): shutil.rmtree(dir) os.makedirs(dir) cybertextron import os import shutil path = 'path_to_my

How do I index and search text files in Lucene 3.0.2?

强颜欢笑 提交于 2019-11-30 00:48:54
I am newbie in Lucene, and I'm having some problems creating simple code to query a text file collection . I tried this example , but is incompatible with the new version of Lucene. UDPATE: This is my new code , but it still doesn't work yet. Lucene is a quite big topic with a lot of classes and methods to cover, and you normally cannot use it without understanding at least some basic concepts. If you need a quickly available service, use Solr instead. If you need full control of Lucene, go on reading. I will cover some core Lucene concepts and classes, that represent them. (For information on

Windows XP batch file concat

我怕爱的太早我们不能终老 提交于 2019-11-29 23:47:22
问题 I'm trying to accomplish the following ridiculous task: I have a text file containing a set of fully qualified filesnames. I want to iterate through the file and append each line to a common variable, that can be passed to a command line tool. For example, the file might be: C:\dir\test.txt C:\WINDOWS\test2.txt C:\text3.txt and I'd like to assign them to some variable 'a' such that: a = "C:\dir\test.txt C:\WINDOWS\test2.txt C:\text2.txt" A secondary question is - what is a good batch file

how to parse unicode that is read from a file in java [duplicate]

依然范特西╮ 提交于 2019-11-29 23:22:41
问题 This question already has an answer here: How to unescape a Java string literal in Java? 10 answers I have written a text file with the following contents: \u0032\u0142o\u017Cy\u0142 Then I have used FileReader und BufferedReader to read the file. public static void main(String[] args) throws Exception{ FileInputStream fr = new FileInputStream("README.TXT"); BufferedReader br = new BufferedReader(new InputStreamReader(fr,"UTF-8")); String s=""; while((s=br.readLine())!=null){ System.out

Python Multiple users append to the same file at the same time

浪子不回头ぞ 提交于 2019-11-29 22:21:19
I'm working on a python script that will be accessed via the web, so there will be multiple users trying to append to the same file at the same time. My worry is that this might cause a race condition where if multiple users wrote to the same file at the same time and it just might corrupt the file. For example: #!/usr/bin/env python g = open("/somepath/somefile.txt", "a") new_entry = "foobar" g.write(new_entry) g.close Will I have to use a lockfile for this as this operation looks risky. phihag You can use file locking : import fcntl new_entry = "foobar" with open("/somepath/somefile.txt", "a

Clearing content of text file using php

这一生的挚爱 提交于 2019-11-29 20:15:50
I have a filelist.txt file and I created a file called clear.php to clear the content of filelist. I put a button in index.html to call clear.php to clear the file. Can anyone help me out regarding what PHP code I should write in clear.php? How to code a button to call clear.php and then return back to index.html showing the result that it has been cleared? file_put_contents ("filelist.txt", ""); You can redirect by using the header() function to modify the Location header. This would truncate the file: $fh = fopen( 'filelist.txt', 'w' ); fclose($fh); In clear.php, redirect to the caller page