Cat until certain nb occurence in bash

bash count occurrences of string in file
awk
grep
linux count occurrences of string in line
sed
awk count number of occurrences
grep regex
bash for loop

Yet another awk:

$ awk -v n=4 '/@/&&!n--{exit}1' file

Output:

@SRR9110374.1 1/1
GAGTATAAAGAAGAAAGTAAATCTCGGTTCGTCTCTTCATCGAGAGAAATGTCGACGAGAAAAAAAAAACAAGGGCTCATTTAAAGCCTTTCAAATCCT
+
BBBBBFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFBFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF
@SRR9110374.2 2/1
ATATGGAACAAGTTAAAAAAAATAAAAAGCAAAGAAATAATGTTTTGTCATCGAAAGTGTCGACATAAAAACAGGTTGGCATCTGGCCTGGTATCTCA
+
BBBBBFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF<BFFFFFFF<FFFFFFFFFFF
@SRR9110374.3 3/1
NTATAACCGTATCAAAGAAGTTTACCCCGAGAGAAGCACGCAGTTTCCCACAGGTAATTTTCTCACAAGCGAGAGAAACATCATACCGCAATCAGGAAC
+
#<<BBFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFBFFFFFFFFFFFFFF
@SRR9110374.4 4/1
GATAAAGAATATAGCTATGTATAGCCGGGATATATTAAGTGATTGAAATATCTCTTAGAAATCCATAGAATAGTAGTGTATCGAATAGGAGGAAGCGAAA
+
BBBBBFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF

Explained:

$ awk -v n=4 '    # -v variable=value is the way to introduce values to awk from the shell
/@/ && !n-- {     # when @ met (n+1)th time
    exit          # ... exit
}1' file          # output

Sort and count number of occurrence of lines, In the particular case were the lines you are sorting are numbers, you need use wolf@linux:~$ cat file | sort | uniq -c | sort -r 4 d.php 3 a.php 2 b.php 1 c.php� root:/tmp# cat file Some text begin Some text goes here. end Some more text root:/tmp# grep -Pzo "^begin\$(.| )*^end$" file grep: ein nicht geschütztes ^ oder $ wird mit -Pz nicht unterstützt The proposed solutions search only for pattern which start at the beginning of the line if I interpret the proposed solution correctly.


Could you please try following.

Nb_occurence=4
awk -v nb_occur="$Nb_occurence" '
BEGIN{
  occur=0
}
/@/{
  occur++
}
occur>nb_occur{
  exit
}
occur
' Input_file


Ps: The real file is very huge so i I should get a method adapted to that it would be nice.

To make Input_file reading faster:

To FASTER your processing of Input_file I have used exit so once your mentioned number of occurrences are done with reading it will ASAP come out of Input_file, since we need NOT to read it further and thus it should be FASTER than your solution.

grep – show lines until certain pattern, You can use sed for that: sed -n '/if/,/endif/p' myfile. -n don't print anything until we ask for it; /if/ find the first line with this; , keep going until. I have a file with contents x a x b x c I want to grep the last occurrence, x c when I try sed -n "/x/,/b/p" file it lists all the lines, beginning x to c.


You should rewrite your awk on this way:

awk -v occurence=$Nb_occurence 'BEGIN{ found=0} /@/{found=found+1} {if ( found < occurence ) print }' file

And you do not need cat, awk can read the file

How to count occurrence of a specific character in a string or file , Using bash shell we can now count an occurrence of any given character. For example let's The sed command first separates any given string to multiple lines. Where each line cat /etc/services | sed -e 's/\(.\)/\n/g' | grep l� I've tried using cat ${filename} cut -c1-50 but I'm getting far more than the first 50 characters? That may be due to cut looking for lines (not 100% sure), while this text file could be one long string-- it really depends. Is there a utility out there I can pipe into to get the first X characters from a cat command?


Text Processing Commands, bash$ cat items alphabet blocks building blocks cables bash$ cat prices $1.00/ dozen (Note that before #+ continuing, if -n option had not been passed, sed would have To list a specific line of a text file, pipe the output of head to tail -n 1 . grep -c txt *.sgml # (number of occurrences of "txt" in "*.sgml" files) # grep -cz . The following command will replace the last occurrence of the searching pattern, ‘Python ‘ by the text, ‘Bash’. Here, ‘$’ symbol is used to match the last occurrence of the pattern. $ cat python. txt $ sed -e '$s/Python/Bash/' python. txt


sed, a stream editor, For example, to replace all occurrences of ' hello ' to ' world ' in the file input.txt : input.txt > output.txt sed 's/hello/world/' < input.txt > output.txt cat input.txt | sed Use -n to suppress output, and the p command to print specific lines. Any characters following the e command until the end of the line will be sent to the shell. Explained:-n disables default behavior of sed of printing each line after executing its script on it, -e indicated a script to sed, /TERMINATE/,$ is an address (line) range selection meaning the first line matching the TERMINATE regular expression (like grep) to the end of the file ($), and p is the print command which prints the current line.


[PDF] grep, awk and sed – three VERY useful command-line utilities Matt , Grep will repeat this process until the file runs out of lines. way you could track down a particular string more easily, if you needed to open the file sed can be used at the command-line, or within a shell script, to edit a file non-interactively. NB sed is line-oriented, so if you wish to change every occurrence on each line,. This command will print output of ls at an interval of 2 sec. Use Ctrl+C to stop the process. There is few drawbacks of watch. It can not use any aliased commands. If the output of any command is quite long, scrolling does not work properly. There is some trouble to set maximum time interval beyond certain value.