lines

How to delete the last 5 lines of a file

半城伤御伤魂 提交于 2019-12-23 06:45:45
问题 I have several commands printing text to a file using perl. During these print commands I have an if statement which should delete the last 5 lines of the file I am currently writing to if the statement is true. The number of lines to delete will always be 5. if ($exists == 0) { print(OUTPUT ???) # this should remove the last 5 lines } 回答1: You can use Tie::File: use Tie::File; tie my @array, 'Tie::File', filename or die $!; if ($exists == 0) { $#array -= 5; } You can use the same array when

How to delete the last 5 lines of a file

北城余情 提交于 2019-12-23 06:45:15
问题 I have several commands printing text to a file using perl. During these print commands I have an if statement which should delete the last 5 lines of the file I am currently writing to if the statement is true. The number of lines to delete will always be 5. if ($exists == 0) { print(OUTPUT ???) # this should remove the last 5 lines } 回答1: You can use Tie::File: use Tie::File; tie my @array, 'Tie::File', filename or die $!; if ($exists == 0) { $#array -= 5; } You can use the same array when

Separate by blank lines in bash

蓝咒 提交于 2019-12-23 02:44:15
问题 I have an input like this: Block 1: line1 line2 line3 line4 Block 2: line1 line2 Block 3: line1 line2 line3 This is an example, is there an elegant way to print Block 2 and its lines only without rely on their names? It would be like "separate the blocks by the blank line and print the second block". 回答1: try this: awk '!$0{i++;next;}i==1' yourFile considering performance, also can add exit after 2nd block was processed: awk '!$0{i++;next;}i==1;i>1{exit;}' yourFile test : kent$ cat t Block 1:

Python - Deleting the first 2 lines of a string

爷,独闯天下 提交于 2019-12-22 02:22:29
问题 I've searched many threads here on removing the first two lines of a string but I can't seem to get it to work with every solution I've tried. Here is what my string looks like: version 1.00 6992 [-4.32063, -9.1198, -106.59][0.00064, 0.99993, -0.01210][etc...] I want to remove the first two lines of this Roblox mesh file for a script I am using. How can I do that? 回答1: I don't know what your end character is, but what about something like postString = inputString.split("\n",2)[2]; The end

get the first 3 lines of a text file in php [duplicate]

一曲冷凌霜 提交于 2019-12-22 01:21:37
问题 This question already has answers here : How to read a large file line by line? (14 answers) Closed 4 years ago . I am developing a website in PHP, and I must include in the index the first 3 lines of a text file in PHP. How can I do that? <?php $file = file_get_contents("text.txt"); //echo the first 3 lines, but it's wrong echo $file; ?> 回答1: Even more simple: <?php $file_data = array_slice(file('file.txt'), 0, 3); print_r($file_data); 回答2: Open the file, read lines, close the file: // Open

How to join lines adding a separator?

爷,独闯天下 提交于 2019-12-21 17:25:52
问题 The command J joins lines. The command gJ joins lines removing spaces Is there also a command to Join lines adding a separator between the lines? Example: Input : text other text more text text What I want to do: - select these 4 lines - if there are spaces at start and/or EOL remove them - join lines adding a separator '//' between them Output : text//other text//more text//text 回答1: You can use :substitute for that, matching on \n : :%s#\s*\n\s*#//#g However, this appends the separator at

Find most frequent line in file in bash

China☆狼群 提交于 2019-12-21 17:02:19
问题 Suppose I have a file similar to as follows: Abigail 85 Kaylee 25 Kaylee 25 kaylee Brooklyn Kaylee 25 kaylee 25 I would like to find the most repeated line, the output must be just the line. I've tried sort list | uniq -c but I need clean output, just the most repeated line (in this example Kaylee 25 ). 回答1: Kaizen ~ $ sort zlist | uniq -c | sort -r | head -1| xargs | cut -d" " -f2- Kaylee 25 does this help ? 回答2: IMHO, none of these answers will sort the results correctly. The reason is that

matplotlib - control capstyle of line collection/large number of lines

ぃ、小莉子 提交于 2019-12-21 04:25:20
问题 Similarly to a previous question of mine, I'd like to control the capstyle of lines being drawn using matplotlib. However, I have an extremely large number of lines, and drawing with anything other than a line collection takes way too long. Are there any workarounds to control the capstyle of lines in a line collection in a generic way (or alternatively, super fast ways of drawing a large number of Line2D lines). For instance, I've tried using the matplotlib rc settings via: import matplotlib

How can I repeat the line in notepad++?

南楼画角 提交于 2019-12-20 09:57:10
问题 How can I repeat the line in notepad++ ? For example I have the following input: a 01 a 02 a 03 a 04 And I would like it to become: a 01 a 01 a 02 a 02 a 03 a 03 a 04 a 04 So every line should be displayed twice . 回答1: If you don't mind the semi-manual process, you can start at the first line and repeat the following key combination until you reach the end of the document (you get very fast at this) Ctrl + D Down Down This duplicates the current line, then moves down twice (to the line

Line counting C++

梦想的初衷 提交于 2019-12-20 07:12:57
问题 I'm working with this peace of that count the logical lines in a program, omitting comments and black lines. The counting line is working, but I don't know how to omit the comment lines, I try if (line == "//") { comment++; } but it only checks for lines that start with "//" and if there's text next to that it doesn't count that as a comment line :/ At the end when I know the total lines and total comment lines, I will subtract totalLines-commentLines to know the real program number lines.