Select random lines from a file
Use shuf with the -n option as shown below, to get N random lines: shuf -n N input > output
Use shuf with the -n option as shown below, to get N random lines: shuf -n N input > output
The delimiter can be a regular expression. awk -F'[/=]’ ‘{print $3 “\t” $5 “\t” $8}’ file Produces: tc0001 tomcat7.1 demo.example.com tc0001 tomcat7.2 quest.example.com tc0001 tomcat7.5 www.example.com
The text handling capabilities of MySQL aren’t good enough for what you want. A stored function is an option, but will probably be slow. Your best bet to process the data within MySQL is to add a user defined function. If you’re going to build a newer version of MySQL anyway, you could also add … Read more
Update Here is a solution from yottatsa on a similar question that only does replacement for variables like $VAR or ${VAR}, and is a brief one-liner i=32 word=foo envsubst < template.txt Of course if i and word are in your environment, then it is just envsubst < template.txt On my Mac it looks like it … Read more
you have to open the file in append mode, which can be achieved by using the FileWriter(String fileName, boolean append) constructor. output = new BufferedWriter(new FileWriter(my_file_name, true)); should do the trick
The with statement is excellent for automatically opening and closing files. with open(‘myfile’,’rw’) as file: for line in file: if not line.isspace(): file.write(line)
# If you want to edit the file in-place sed -i -e ‘s/^/prefix/’ file # If you want to create a new file sed -e ‘s/^/prefix/’ file > file.new If prefix contains /, you can use any other character not in prefix, or escape the /, so the sed command becomes ‘s#^#/opt/workdir#’ # or ‘s/^/\/opt\/workdir/’
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 conjunction with the -n command-line option. n – If auto-print is not disabled, print the pattern space, then, regardless, replace the pattern space with the next line … Read more
A sed script that will only replace the first occurrence of “Apple” by “Banana” Example Input: Output: Apple Banana Apple Apple Orange Orange Apple Apple This is the simple script: Editor’s note: works with GNU sed only. sed ‘0,/Apple/{s/Apple/Banana/}’ input_filename The first two parameters 0 and /Apple/ are the range specifier. The s/Apple/Banana/ is what … Read more
With GNU grep: grep ‘[[:alpha:]]’ file or GNU sed: sed ‘/[[:alpha:]]/!d’ file Output: 0hjjAby68xp H5e