text-processing

Select random lines from a file [duplicate]

随声附和 提交于 2019-11-26 10:07:53
问题 This question already has an answer here: What's an easy way to read random line from a file in Unix command line? 13 answers In a Bash script, I want to pick out N random lines from input file and output to another file. How can this be done? 回答1: Use shuf with the -n option as shown below, to get N random lines: shuf -n N input > output 回答2: Sort the file randomly and pick first 100 lines: $ sort -R input | head -n 100 >output 来源: https://stackoverflow.com/questions/9245638/select-random

How can I sum values in column based on the value in another column?

我是研究僧i 提交于 2019-11-26 09:51:16
问题 I have a text file which is: ABC 50 DEF 70 XYZ 20 DEF 100 MNP 60 ABC 30 I want an output which sums up individual values and shows them as a result. For example, total of all ABC values in the file are (50 + 30 = 80) and DEF is (100 + 70 = 170). So the output should sum up all unique 1st column names as - ABC 80 DEF 170 XYZ 20 MNP 60 Any help will be greatly appreciated. Thanks 回答1: $ awk '{a[$1]+=$2}END{for(i in a) print i,a[i]}' file ABC 80 XYZ 20 MNP 60 DEF 170 回答2: $ perl -lane \ '$sum{$F

Python: How to loop through blocks of lines

大憨熊 提交于 2019-11-26 09:49:49
问题 How to go through blocks of lines separated by an empty line? The file looks like the following: ID: 1 Name: X FamilyN: Y Age: 20 ID: 2 Name: H FamilyN: F Age: 23 ID: 3 Name: S FamilyN: Y Age: 13 ID: 4 Name: M FamilyN: Z Age: 25 I want to loop through the blocks and grab the fields Name, Family name and Age in a list of 3 columns: Y X 20 F H 23 Y S 13 Z M 25 回答1: Here's another way, using itertools.groupby. The function groupy iterates through lines of the file and calls isa_group_separator

How to replace ${} placeholders in a text file?

纵饮孤独 提交于 2019-11-26 04:05:25
问题 I want to pipe the output of a \"template\" file into MySQL, the file having variables like ${dbName} interspersed. What is the command line utility to replace these instances and dump the output to standard output? 回答1: Sed! Given template.txt: The number is ${i} The word is ${word} we just have to say: sed -e "s/\${i}/1/" -e "s/\${word}/dog/" template.txt Thanks to Jonathan Leffler for the tip to pass multiple -e arguments to the same sed invocation. 回答2: Update Here is a solution from

How can I extract a predetermined range of lines from a text file on Unix?

你。 提交于 2019-11-26 00:49:18
问题 I have a ~23000 line SQL dump containing several databases worth of data. I need to extract a certain section of this file (i.e. the data for a single database) and place it in a new file. I know both the start and end line numbers of the data that I want. Does anyone know a Unix command (or series of commands) to extract all lines from a file between say line 16224 and 16482 and then redirect them into a new file? 回答1: sed -n '16224,16482p;16483q' filename > newfile From the sed manual: p -

How can I extract a predetermined range of lines from a text file on Unix?

浪子不回头ぞ 提交于 2019-11-25 23:04:59
I have a ~23000 line SQL dump containing several databases worth of data. I need to extract a certain section of this file (i.e. the data for a single database) and place it in a new file. I know both the start and end line numbers of the data that I want. Does anyone know a Unix command (or series of commands) to extract all lines from a file between say line 16224 and 16482 and then redirect them into a new file? boxxar sed -n '16224,16482p;16483q' filename > newfile From the sed manual : p - Print out the pattern space (to the standard output). This command is usually only used in

How to use sed to replace only the first occurrence in a file?

*爱你&永不变心* 提交于 2019-11-25 22:47:17
问题 I would like to update a large number of C++ source files with an extra include directive before any existing #includes. For this sort of task, I normally use a small bash script with sed to re-write the file. How do I get sed to replace just the first occurrence of a string in a file rather than replacing every occurrence? If I use sed s/#include/#include \"newfile.h\"\\n#include/ it replaces all #includes. Alternative suggestions to achieve the same thing are also welcome. 回答1: # sed script