Find number 9 in file - linux terminal [closed] - linux
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
I need to find how many times number "9" appears in the result of the draw
(ignoring the ordinal number and date)
65. 11.05.1958 8,17,22,27,31,21
66. 18.05.1958 1,2,8,17,28,54
67. 25.05.1958 7,16,27,33,41,23
68. 01.06.1958 1,20,41,42,43,43
69. 08.06.1952 13,14,25,29,33,47
70. 15.06.1958 17,23,29,39,41,45
71. 22.06.1958 2,14,22,44,48,49
72. 29.06.1958 3,7,13,15,16,47
73. 06.07.1958 10,11,28,38,48,49
74. 13.07.1956 9,16,21,24,27,35
75. 20.07.1958 1,7,17,18,29,32
76. 27.07.1958 19,21,42,25,36,44
77. 03.08.1958 2,4,22,31,32,43
78. 10.08.1958 4,9,16,26,27,46
79. 17.08.1958 34,35,37,38,39,45
80. 24.08.1958 17,21,27,35,41,49
81. 31.08.1958 30,31,32,9,46,49
82. 07.09.1958 10,16,23,26,30,39
83. 14.09.1958 13,16,18,19,30,35
84. 21.09.1958 9,23,26,29,31,42
85. 28.09.1958 12,16,21,28,9,49
Use awk to remove the first 2 columns, then use grep -c to find the 9s. Put a \b around the regex to make sure it doesn't track 19,29,91,92,93,etc. Assuming your output is coming from FILENAME:
awk '{ print $3 }' FILENAME | grep -c '\b9\b
Assuming the text to search is in a file named output.txt:
cut <output.txt -d ' ' -f 3 | grep -w 9 | wc -l
The cut part splits by spaces, taking the third field.
grep -w finds 9 as a word, so the for example the line with 10,16,23,26,30,39 won't get picked.
Finally, wc -l counts how many lines we have.
And just for fun, here is yet another way, this one in Perl:
perl -nle '$c+=()=/\b(9)\b/g; END {print $c}' $FILENAME
This has the difference that it allows 9 to appear multiple times, and counts them all. Because it uses /g (global) and first casts the result to an array ()=, before adding the number of elements in the array to $c.
Related
How to grep a word from a specific line till the end of file? [closed]
Closed. This question needs to be more focused. It is not currently accepting answers. Want to improve this question? Update the question so it focuses on one problem only by editing this post. Closed 2 years ago. Improve this question How to find a word for eg Find "abc" starting from line 6 till the end of file ?
You can grep the output of tail command like this. tail -n+6 file.txt | grep abc
In sed, it's easy to say "ignore lines in this address range" and then grep in the rest. sed -n -e '1,5d' -e '/abc/p' file The -n option says to not print all lines by default, we then simply delete lines 1 through 5, and then print any matching lines in the remainder. There's also a block syntax, so you can say sed -n '6,${/abc/p;}' file but the precise syntax differs slightly between dialects (I think Linux would not demand a semicolon before the closing brace?) The address range 6,$ selects lines from the sixth through the end of the file.
One solution is to use awk, checking both the text and the line number (this script contains only the rule to select lines, it uses the default action which is to print the line): awk 'NR >= 6 && /abc/' inputfile.txt The following transcript shows this in action: pax:~> cat inputfile.txt 1 abc 2 def 3 abc 4 xxx 5 yyy 6 abc 7 xyz 8 abc 9 abc pax:~> awk 'NR >= 6 && /abc/' inputfile.txt 6 abc 8 abc 9 abc
Delete (d) lines from 1 to 5 (1,5); and also delete (the other d) lines not (!) containing abc (/abc/): sed '1,5d;/abc/!d' input.txt
How can i search for an hexadecimal content in a file in a linux/unix/bash script? [closed]
Closed. This question needs details or clarity. It is not currently accepting answers. Want to improve this question? Add details and clarify the problem by editing this post. Closed 2 years ago. Improve this question I have an hexadecimal string s and a file f, i need to search the first occurence of that string in the file and save that in a variable with his offset. I thought that the right way to do that is convert the file to hex and search that with a grep. The main problem is that i saw a lot of commands(hexdump,xxd,etc.) to convert but none of them actually work. Any suggestion? My attempt was like this: xxd -plain $f > $f grep "$s" . output should be like: > offset:filename
A first approach without any error handling could look like #!/bin/bash BINFILE=$1 SEARCHSTRING=$2 HEXSTRING=$(xxd -p ${BINFILE} | tr -d "\n") echo "${HEXSTRING}" echo "Searching ${SEARCHSTRING}" OFFSET=$(grep -aob ${SEARCHSTRING} <<< ${HEXSTRING} | cut -d ":" -f 1) echo ${OFFSET}:${BINFILE} I've used xxd here because of Does hexdump respect the endianness of its system?. Please take also note that according How to find a position of a character using grep? grep will return multiple matches, not only the first one. The offset will be counted beginning from 1, not 0. To substract 1 from the variable ${OFFSET} you may use $((${OFFSET}-1)). I.e. search for the "string" ELF (HEX 454c46) in a system binary will look like ./searchHEX.sh /bin/yes 454c46 7f454c460201010000000000000000000...01000000000000000000000000000000 Searching 454c46 2:/bin/yes
I would use regex for this as well: The text file: $ cat tst.txt 1234567890x1fgg0x1cfffrr A script you can easily change/extend yourself. #! /bin/bash part="$(perl -0pe 's/^((?:(?!0(x|X)[0-9a-fA-F]+).)*)(0(x|X)[0-9a-fA-F]+)(.|\n)*/\1:\3\n/g;' tst.txt)" tmp=${part/:0x*/} tmp=${#tmp} echo ${part/*:0x/$tmp:0x} # Echoes 123456789:0x1f Regex: ^((?:(?!0x[0-9a-fA-F]+).)*) = Search for the first entry that's a hexadecimal number and create a group of it (\1). (0x[0-9a-fA-F]+) = Make a group of the hexadecimal number (\3). (.|\n)* = Whatever follows. Please note that tmp=${part/:0x*/} could cause problems if you have text like :0x before the hexadecimal number that is caught.
If the numbers from 1 to 4321 were written out, how many times would the digit '5' appear? [closed]
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers. This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers. Closed 3 years ago. Improve this question I have figured out the answer for this program, however I don't really understand what does backslush means in here. I tried to remove the backslush, then the program print all of numbers from 1 to 4321, but it did not grep how many times number 5 appear. When I put backslush, it just show the number how many time number 5 appear. here is my code: #!/bin/bash seq 4321 | \ while read -n1 digit; do echo $digit done | grep -c 5
You can do $ seq 4321 | tr -Cd 5 | wc -c 1262 which deletes all the chars but 5 and count the result
Usually back slashes when they are put after a command then it means we are telling program that line still continuing and should be considered as a same line commands execution. Usually we do so to make program cleaner(in the way one shouldn't see a LONG line which is keep continuing and shows like a non readable form). Coming to your code now: You need not to use \ after seq command since you are using | and while could be mentioned in new line. Moreover your code is not in a long line so it may not be required and should run without it too. I tested your code without \ and it worked fine for me. IMHO you need not to use while loop for this task, you could directly do: seq 4321 | awk '{sum+=gsub(/5/,"&")} END{print sum}' Try it out if this helps you(if I have got your requirement correctly), this should be faster than a loop and then using grep option.
With GNU grep you can do like this: $ seq 4321 | grep -o 5 | wc -l 1262 $ According to grep's manual: -o, --only-matching Print only the matched (non-empty) parts of a matching line, with each such part on a separate output line.
Command for printing part of a String? [closed]
Closed. This question needs to be more focused. It is not currently accepting answers. Want to improve this question? Update the question so it focuses on one problem only by editing this post. Closed 3 years ago. Improve this question I have a file name test, it contains a String James Bond 007, and i want to print only James Bond. I tried the following commands: $ strings -n 2 test $ sed -n '/James/,/Bond/p' test $ awk '{print substr($1,10)}' test
To print the first two words, you can use awk: awk '{print $1, $2}' test To print the first ten characters, you can put the file contents in a variable, then use the bash substring operation: contents=$(cat test) echo "${contents:0:10}" Or in awk: awk '{print substr($0, 1, 10)}' test Notice that $0 means the whole line, and you have to give both a starting index and length to substr(). Indexes in awk start at 1 rather than 0. In sed, /James/,/Bond/ is a line range expression; it processes all the lines starting from a line containing James until a line containing Bond. It doesn't process just part of the lines.
How to grep a line of ps afux filtering by PID [closed]
Closed. This question needs debugging details. It is not currently accepting answers. Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question. Closed 4 years ago. Improve this question I just want to know how to grep a line which consists of for example pid with number 2. I want to grep the whole line. Also, it is very important to filter only and exactly "2". Because at the moment It filters all the number which have 2 in it.
If you want to get the listing for just one particular PID, the -p option is the best way. ps -f -p 2 for example If you want grep to match a string only if it is the whole word, not part of another word, look at the -w flag, which matches words like $ echo '52' | grep 2 52 $ echo '52' | grep -w 2 $ if you want to match against only a particular field, awk might be a better answer than grep. For example, if we want to check if the second column is exactly 2 we could do ps -af | awk '$2 == 2 {print}'
You could go for something like this. If you need the details of a process and you know the pid go for this. ps afux | awk '{if($2==<pid>) print}'