New answers tagged

0 votes
Accepted

How to jump to the file and line of the grep -nr output?

Moved from the question to an answer I came up with this solution. It's not elegant and short (and also assumes that only one occurrence is found, but could be expanded on this), but does the job. res=...
-1 votes

Identifying genes from a list of genes

You can also use grep command in linux,by giving command like egrep -wi "complete genelist (1) with inducing pipe among genes" "file name containg all genes(2)".Here genelist(1) is ...
user avatar
13 votes
Accepted

tr command unable to process colour output piped from grep

To output matches in colour, grep writes colouring escape sequences before and after the match. Those are instructions to the terminals to change their background and/or foreground colour. It's ...
user avatar
1 vote

tr command unable to process colour output piped from grep

I fixed it by changing GREP_OPTIONS from --color=always to --color=auto I suppose tr and colour coded text don't go well together
user avatar
  • 53
1 vote

return a list using grep (or alternative)

You could do something like this, in GNU awk: gawk -F'[[:blank:]]*=[[:blank:]]*' -v key=Exec ' BEGINFILE {value = "0"} $1 == key {value = $2; nextfile} ENDFILE {split(value,v,/[[:...
user avatar
  • 72.1k
0 votes
Accepted

Extracting text with expressions from a text file using grep

This should do it for you: :~$ cat event.log 18.05.2022 13:54:52 [ INFO]: Starting Component 'OWN_FUNDS_RULES' (5/15) 18.05.2022 14:28:22 [ INFO]: Finished Component 'OWN_FUNDS_RULES_CONSOLIDATION' (6/...
user avatar
2 votes

Looping through 'find' files and 'grep' to file character set result (standard input)

You're piping data into grep's stdin, so grep does not have a filename to print. I'm assuming 2022* are the filenames, not directories. find . -type f -name '2022*' -exec sh -c ' for file; do ...
user avatar
5 votes

Looping through 'find' files and 'grep' to file character set result (standard input)

file is already printing the filename, you have to cut it after a simple grep. If your files are into the same directory: file -i * | grep -v 'charset.*ascii' | cut -d: -f1 And if you match files ...
user avatar
  • 6,180
4 votes
Accepted

-bash: syntax error near unexpected token `(' when using lookahead and lookback?

The issue that the error message addresses is not the single quotes but the parentheses. Unquoted parentheses are special to the shell, and their meaning depends on their placement on the command ...
user avatar
  • 267k
0 votes

How to remove line with specific string from file

If that is the exact contents of the file, then here is an easy solution with GNU sed: sed '/^\/home\/user1\/$/d' file ^ marks the beginning of the line and $ marks the end of the line so it will ...
user avatar
  • 9,473
0 votes

How to remove line with specific string from file

If sed is an option $ sed -n '\|\(/\([^/]*/\)\{2\}\)\<|p' input_file /home/user1/dir1 /home/user1/dir2 /home/user1/dir1/dir11
user avatar
  • 1,092
1 vote
Accepted

How to remove line with specific string from file

Use anchors to mark the beginning and end of the line: ^ for the beginning and $ for the end. So it would go like this: $ grep -v '^/home/user1/$' file /home/user1/dir1 /home/user1/dir2 /home/user1/...
user avatar
1 vote

Find how many words in a file contain a number in them?

I suspect you're not getting any matches because your shell is interpreting the brackets using its own syntactic rules. They need to be in quotes. But even if it weren't for that problem, yours would ...
user avatar
  • 7,117
0 votes

grepping a variable and adding 1 to it

A bunch of partial answers: How to handle a number with leading zero(s), v1: newvar=$(printf '%s + 1\n' "$var" | bc) or, equivalently, newvar=$(bc <<< "$var + 1") ...
user avatar
4 votes
Accepted

Do grep and cp treat "recursive" the same?

Your grep command, grep -R "find me" *.php ... contains a filename globbing pattern, *.php. This will be expanded by the shell before the shell executes grep, so the actual command at ...
user avatar
  • 267k
0 votes

Command line - save sub-strings conditionally

Using sed $ sed s'~\([0-9]\)/[^:]*:\(.*\)~echo \2 >> \1~e' file.txt $ cat 1 20020711 20020731
user avatar
  • 1,092
5 votes
Accepted

Command line - save sub-strings conditionally

With awk: awk -F'[:/]' '{print $NF > $1}' file We split the row using both / and : as separators. The last field ($NF) is what to print, and the first field ($1) is the output filename. After ...
user avatar
  • 6,180
1 vote
Accepted

Linux - count words with more than a certain length of characters

After you mentioned your professor, I'm a bit worried we're doing your homework for you, but you can try: egrep -wo '[[:alnum:]]{5,}' filename.txt | wc -w This looks for spans of 5 or more ...
user avatar
  • 7,117
2 votes
Accepted

Return content between corresponding brackets

What you've offered isn't valid JSON. Bracketing the expression, fixing up the other errors, and adding a counter-example: { "text": [ { "string1": ["...
user avatar
  • 89k
0 votes

grepping a variable and adding 1 to it

I can achieve it but don't use grep function, code below: #!/bin/bash echo "enter number" read number newnumber=$((number + 1 + 10000)) # 10000 = the largest value echo ${newnumber#*1}
user avatar
  • 9
0 votes
Accepted

How to add column based in the second line match using SED or another command?

How about the simple awk command: $ awk -F "1 - |2 - Response:" 'NF>1 {printf "%s",$2} END{print ""}' input_file URL Template: https://www.test.com <200 OK,{Server=[...
user avatar
  • 2,229
-1 votes

How to add column based in the second line match using SED or another command?

sed '/^$/d' filename| awk 'NR==2{gsub(/.*Response:/,"",$0)}1'|sed "1s/.*-//g"|perl -pne "s/\n/ /g" output URL Template: https://www.test.com <200 OK,{Server=[nginx],...
user avatar
0 votes

How to add column based in the second line match using SED or another command?

Using sed $ sed '/^1/{:a;N;s/^[^A-Z]*\(.*\)\n[^<]*\(.*\)/\1 \2/;ba}' input_file URL Template: https://www.test.com <200 OK,{Server=[nginx], Date=[Wed, 11 May 2022 01:05:06 GMT], Content-Type=[...
user avatar
  • 1,092
1 vote
Accepted

Extract field and number of occurrences per line

Try this: $ awk -v FS="', '" '{print $4 " |"NF-1 " " NR}' file If you have blank lines in file, you may want to use: $ awk -v FS="', '" 'NF >= 1 {print $4 &...
user avatar
  • 2,229
0 votes

How to ignore all text containing special character more than once?

sed -n '/.*_.*_.*/!p' file.txt output apple_ig dog_ig orange_ig goat_ig
user avatar
0 votes

How to ignore all text containing special character more than once?

GNU sed we try to replace the second occurrence of _ and breakout on failureT, otherwise deleting the pattern space d. sed 's/_/&/2;T;d' file POSIX sed same as above but inverting the logic, ...
user avatar
  • 5,333
5 votes

How to ignore all text containing special character more than once?

$ grep -v '_.*_' file apple_ig dog_ig orange_ig goat_ig
user avatar
  • 21.9k
0 votes

How to ignore all text containing special character more than once?

Using sed $ sed -n '/^[^_]*_[[:alpha:]]\+$/p' input_file apple_ig dog_ig orange_ig goat_ig
user avatar
  • 1,092
3 votes
Accepted

How to ignore all text containing special character more than once?

With awk, to print only lines having maximum one underscore. $ awk -F_ 'NF<=2' file apple_ig dog_ig orange_ig goat_ig
user avatar
  • 6,180
6 votes

bash + how to compare arg to one of the disks in linux machine

Since lsblk allows for JSON output, it seems natural to use jq with it to figure out whether the given argument is a disk or not: #!/bin/sh if lsblk -J | jq -e --arg name "$1" '....
user avatar
  • 267k
1 vote

How to grep for specific format

Try this grep '00+0530-..00+0530' myfile.txt Dots (.) are inserted in the search string for any character. You do not need cat.
user avatar
  • 1,286
2 votes
Accepted

How to grep for specific format

Assuming the substrings with four digits are timestamps, it appears you want to exclude the lines that contain timestamps from a quarter past the hour or quarter to the hour. To do this, use grep like ...
user avatar
  • 267k
5 votes

bash + how to compare arg to one of the disks in linux machine

One possibility is to have two if-checks. At first, check if you can use $1 (the first argument) (it is not zero and is not --help) and then compare it to your lsblk-Output. For example: #!/bin/env ...
user avatar
  • 61
8 votes
Accepted

bash + how to compare arg to one of the disks in linux machine

You don't seem to be using most of the components of that command. All you need is: lsblk -lnb | awk '$NF=="disk"{print $1}' Then, to avoid the error message when no argument has been given,...
user avatar
  • 215k
3 votes

Count the number of words ends with specific character using Bash

elinks -no-numbering -no-references -dump https://matt.might.net/articles/what-cs-majors-should-know/ | grep -Po '\w+s\b' | wc -l (with GNU grep or compatible) Gives me: 595 elinks retrieves the ...
user avatar
0 votes

Grep and Cut command in linux

Using Raku (formerly known as Perl_6) raku -ne '.put if .split(",")[8] >= 1;' Sample Input: ABC,XYZ,RTY,CREAM,FRANCE,170019,ST REMY CREME,3035540005229,0.75,1,15,26.99,10 ABC,RDS,...
user avatar
  • 1,098
1 vote

How can I add space after filename of grep output?

If all you need is a list of filenames containing text that matches a pattern, such as "zorro", and you intend to use those filenames with another program, then you can do something like the ...
user avatar
  • 66.6k
2 votes
Accepted

How can I add space after filename of grep output?

My first though went to awk awk -F: -vOFS=: '{$1 = $1 " "; print}' But that's doing more work than needed I think, splitting and re-joining the line. The sed alternative is quite concise ...
user avatar
2 votes

sed or other equivalent for grep with vfF

Another way is to create dynamically the EREs, to start matching at the 225th position, with some help from sed, and grep for these EREs: grep -vEf <(sed 's/.*/^.{224}&/' stem.txt) source.txt ...
user avatar
  • 6,180
4 votes
Accepted

sed or other equivalent for grep with vfF

With awk, making sure you only look for those strings wherever they're meant to be. awk '!source {stem[$0]; next} ! (substr($0, 225, 13) in stem) ' stem.txt source=1 source.txt > ...
user avatar
2 votes

Grep inserting file names from working directory

Try this example: $ mkdir -p test_dir && cd test_dir/ $ touch file1 file2 $ time_trigger='<spec>H 1 * * *</spec>' $ echo "$time_trigger" <spec>H 1 * * *</spec&...
user avatar
  • 6,180
2 votes
Accepted

How to substact or sum 12 depending on the last and following line using awk?

Just keep a rolling buffer of 3 lines and examine that: $ cat tst.awk BEGIN { FS=OFS="," } { nxt = $0 prt() } END { prt() } function prt() { if ( cur ~ /Lumenal/ ) { ...
user avatar
  • 21.9k
2 votes

How to substact or sum 12 depending on the last and following line using awk?

I worked this one out before you changed the column separators to commas. First job was to change the multiple spaces to tabs in my test file: $ cat indata ##sequence-region Q75T13 1 641 Q75T13 ...
user avatar
  • 789
3 votes

How to substact or sum 12 depending on the last and following line using awk?

Try the following command: root@u2004:~# cat test ##sequence-region Q75T13 1 641 Q75T13,UniProtKB,Chain,1,641,.,.,.,ID Q75T13,UniProtKB,Topological domain,1,60,.,.,.,Note=Cytoplasmic Q75T13,UniProtKB,...
user avatar
0 votes
Accepted

I'm trying to understand how to shorten regex

Well, [...] is a bracket group, it matches a single character that's any of the ones listed inside, obeying ranges (in some way that depends on the locale). [0-9\.] matches any digit from zero to nine,...
user avatar
  • 115k
-2 votes

How to extract rows in a file based on common information in another file?

You can easily achieve the result as: while read FIRST do cat a.txt | grep "$FIRST" >> resut.txt done < b.txt let say your files are a.txt and b.txt and you want to get result in ...
user avatar
  • 105
0 votes
Accepted

How to extract rows in a file based on common information in another file?

Key -f for grep is what you need. $ grep -f file_with_patterns file_to_scan > result_file
user avatar
  • 1,188
1 vote

How to remember, which order grep and find takes the parameters?

grep and find I'm stumbling through, which order I should type the commands: When I should write the path and when I should write the flags, if it needs double quotations or not, etc. etc. The ...
user avatar
  • 28.8k
1 vote
Accepted

Matching lines in file, with 2 lists of strings in order

Assuming you have GNU Awk available (for the FPAT internal variable), the following program should work: awk 'BEGIN{FPAT="host ([[:digit:]]+.){3}[[:digit:]]+ [^ ]+"} t=="src"{...
user avatar
  • 18.4k
2 votes
Accepted

how to grep the results (plural) of another command

If your shell offers "process substitution", try command2 | grep -f <(command1) If not, you can also pass the list of regexps on the command line using command substitution: command2 | ...
user avatar
  • 8,222

Top 50 recent answers are included