Tag Info

New answers tagged

3

tr -cd '[:space:]' < my_file | wc -m Would work. But with GNU tr, that would only work in single byte per character locales (typically, not in UTF-8 ones) or with ASCII only input in UTF-8 locales. Without the quotes around [:space:], you'd get an error message in csh, tcsh or zsh (unless the condition below is met) and in most shells, that would fail ...


0

#!/bin/bash file=`cat $1` length=`cat $1 | wc -m` // Count the charater count=0 for ((i = 0 ;i < $length;i++)) do //loop to the end of the string if [ "${file:$i:1}" == "$2" ] //Look only character by character (select some characters of the String, (:$i:1) is the range of the choosen characters then count=$((count + 1)) fi done ...


1

User FloHimself seemed curious about a TXR solution. Here is one using the embedded TXR Lisp: (defvar splits 4) (defvar name "data") (let* ((fi (open-file name "r")) ;; input stream (rc (tuples 4 (get-lines fi))) ;; lazy list of 4-tuples (sz (/ (prop (stat name) :size) splits)) ;; split size (i 1) ...


2

Splitting files on the record boundaries is actually very easy, without any code: zcat your_file.gz | split -l 10000 - output_name_ This will create output files of 10000 lines each, with names output_name_aa, output_name_ab, output_name_ac, ... With an input as large as yours, this will give you a lot of output files. Replace 10000 with any multiple of ...


1

I don't think you can do this - not reliably, and not the way you ask. The thing is, the archive's compression ratio will probably not be evenly distributed from head to tail - the compression algorithm will apply better to some parts than others. That's just how it works. And so you can't factor your split on the size of the compressed file. What's more, ...


2

This is not meant to be serious answer! I've been just toying with flex and this will most probably not work on a input file with ~50Gb (if at all, on larger input data than my test file): This works for me on a ~1Gb file input.txt: Given the flex input file splitter.l: %{ #include <stdio.h> extern FILE* yyin; extern FILE* yyout; int input_size = ...


2

From what I gather after checking the google-sphere, and further testing a 7.8 GiB .gz file, it seems that the metadata of the original uncompressed file's size is not accurate (wrong) for large .gz files (greater than 4GiB (maybe 2GiB for some versions of gzip). Re. my test of gzip's metadata: * The compressed.gz file is 7.8 GiB ( 8353115038 bytes) * ...


1

Here's a solution in Python that makes one pass over the input file writing the output files as it goes along. A feature about using wc -l is that you are assuming each of the records here are the same size. That may be true here, but the solution below works even when that is not the case. It is basically using wc -c or the number of bytes in the file. In ...


1

Using sed By selecting the lines starting with .lruno := 72 and continuing to the next clear, this produces the output you request: $ sed -n '/.lruno := 72/,/clear/p' file .lruno := 72 .infno := 1 .tid.noel := 101 .tid.info := 64 .tid.setnr := 1225 .typeidm := 1 .sourcetable := 2 writedb clear .lruno := 72 .infno := 205 .tid.noel := 101 .tid.info := 76 ...


0

Using sed: < inputfile sed '1,6d; $d' > outputfile < inputfile: redirects the content of inputfile to sed's stdin > outputfile: redirects the content of sed's stdout to outputfile 1,6d: deletes all lines from the 1st to the 6th inclusive $d: deletes the last line Sample output: ~/tmp$ cat inputfile $BQ { VOL @home } database daba relation ...


2

What about using sort? awk '{ print length($0) "\t" $0 }' input.txt | sort -n | head -n 1 | cut -f2-


2

Assuming blank lines are not considered the shortest line and that blank lines might exist, the following pure AWK will work: awk ' { len = length; a[$0] = len } !len { next } !min { min = len } len < min { min = len } END { for (i in a) if (min == a[i]) print i } ' ...


2

It occurred to me that the whole thing is possible in one sed expression. It ain't pretty: $ sed '1h;s/.*/&\n&/;G;:l;s/\n[^\n]\([^\n]*\)\n[^\n]/\n\1\n/;tl;/\n\n/{s/\n.*//;x};${x;p};d' testfile 4for $ Breaking this down: 1h # save line 1 in the hold buffer (shortest line so far) s/.*/&\n&/ # duplicate the line with a ...


3

Borrowing some of @mikeserv's ideas: < testfile sed 'h;s/./:/g;s/.*/expr length "&"/e;G;s/\n/\t/' | \ sort -n | \ sed -n '1s/^[0-9]+*\t//p' The first sed does the following: h saves the original line to the hold buffer Replace every character in the line with : - this is to remove any danger of code injection Replace the whole line with expr ...


2

Another perl solution: store the lines in a hash-of-arrays, the hash key being the line length. Then, print out the lines with the minimum key. perl -MList::Util=min -ne ' push @{$lines{ length() }}, $_; } END { print @{$lines{ min keys %lines }}; ' sample 4for


2

To get just the first shortest line: f=file; sed -n "/^$(sed 's/./1/g' $f | sort -ns | sed 's/././g;q')$/{p;q}" $f To get all the shortest lints, just change {p;q} to p Another method (somewhat unusual) is to have sort do the actual sort by length. It is relatively slow even with short lines, and becomes dramatically slower as the line length ...


7

Python comes out fairly concise, and the code Does What It Says On The Tin: python -c "import sys; print min(sys.stdin, key=len)," The final comma is obscure, I admit. It prevents the print statement adding an additional linebreak.


5

tr -c \\n 1 <testfile | #first transform every [^\n] char to a 1 grep -nF '' | #next get line numbers paste -d: - testfile | #then paste it together with itself sort -t: -nk2,2 #then sort on second field ...and the winner is... line 2, it would seem. 2:1111:4for 4:11111:five! 1:1111111:seven/7 3:11111111:8 eight? But the ...


7

A Perl way. Note that if there are many lines of the same, shortest length, this approach will only print one of them: perl -lne '$m||=$_; $m=$_ if length()<length($m); END{print $m}' file Explanation perl -lne : -n means "read the input file line by line", -l causes trailing newlines to be removed from each input line and a newline to be added to ...


13

With sqlite3: sqlite3 <<EOT CREATE TABLE file(line); .import "data.txt" file SELECT line FROM file ORDER BY length(line) LIMIT 1; EOT


7

Here a pure zsh solution (it prints all lines with the minimal length, from file): IFS=$'\n'; print -l ${(M)$(<file):#${~${(o@)$(<file)//?/?}[1]}} Example input: seven/7 4for 8 eight? five! four Output is: 4for four I think it needs a short explanation :-) First, we set the internal field separator to newline: IFS=$'\n'; So far so good, ...


8

I always love solutions with pure shell scripting (no exec!). #!/bin/bash min= while IFS= read -r a; do if [ -z "$min" ] || [ "${#a}" -lt "${#min}" ]; then min="$a" fi done echo "$min"


15

Here's a variant of an awk solution for printing the first found minimum line: awk ' NR==1 || length<len {len=length; line=$0} END {print line} ' which can simply be extended by one condition to print all minimum lines: awk ' length==len {line=line ORS $0} NR==1 || length<len {len=length; line=$0} END {print line}' '


5

With POSIX awk: awk 'FNR==1{l=$0;next};length<length(l){l=$0};END{print l}' file


5

Try: awk '{ print length, $0 }' testfile | sort -n | cut -d" " -f2- | head -1 The idea is to use awk to print the length of each line first. This will appear as: echo "This is a line of text" | awk '{print length, $0}' 22 This is a line of text Then, use the character count to sort the lines by sort, cut to get rid of the count and head to keep the ...



Top 50 recent answers are included