Tag Info

New answers tagged

0

The next awk statement will skip the current line, that is useful if you have to match multiple blocks in your script. awk ' /^#/ {next} / pattern 1 / { } / pattern 2 / { } ' filename


1

Using grep: grep -vE "^#" or grep -E "^[^#]"


-1

sed 's/#.*//' This gets rid of comments, even if they don't start at the first column.


1

awk -F: '/^[^#]/ { print $2 }' /etc/oratab | uniq


6

This searches for any TYPE=sometext on stdin (-Pis for Perl-like regex, where \w+ is "one or more non-whitespace characters) and prints only the matching parts of the input (-o). It then sorts it (required for using uniq), which then prints for every TYPE=something how often it occured (-c).


1

Let's use AWK! $ function wordfrequency() { awk 'BEGIN { FS="[^a-zA-Z]+" } { for (i=1; i<=NF; i++) { word = tolower($i) words[word]++ } } END { for (w in words) printf("%3d %s\n", words[w], w) } ' | sort -rn } $ cat your_file.txt | wordfrequency This lists the frequency of each word occurring in the provided file. I know it's not what you asked for, ...


1

You could try running lsof with the PID but chances are the file was opened at run time by the application but then not kept open; lsof -p 15020 | grep app.js Alternatively you could strace the application and look through the output for the full path to the file; strace -p 15020


0

This should get all the words from all the files, sort them and get unique words, than iterate through those words and count how many files it occurs in. # find all words from all files within the directory grep -o -h -E '\w+' directory/*|sort -u | \ while read word; do # iterate through each word and find how many files it occurs c=`grep -l ...


2

If you have bash 4 mapfile -t array < <(grep ^# threewords | cut -c2-) Will populate array, one line per element printf "%s\n" "${array[@]}" gray speedy bee gray greedy pea


1

The splitting is done with IFS as the delimiter (which contains a space, newline and tab by default). Set the IFS to only the newline: $ IFS=$'\n' a=($(printf "1 2\n2 3\n")) $ echo ${a[0]} 1 2 $ echo ${a[1]} 2 3 This will change IFS for the shell, so best save it before and restore it: OLD_IFS="$IFS" IFS=$'\n' array=($(grep '^#' threewords | cut -c2-)) ...


0

I know your question is about the difference in both the commands you had tried, but if you are not insistent about using awk, and for the benefit of anyone else who may stumble upon this post, another method would be to use pgrep -f. kill $(pgrep -f wget) This will kill all the process that is using wget. Use the signal that you wish to use with kill (if ...


1

For improvement, see @Hauke Laging's answer. For the difference between yours solution, the first approach does not run kill command, it just refer to four variables sudo, kill, STOP and $2. At least in gawk, when you refer to variables with no action in a statement, it do nothing. You can easily use dgawk to see that: $ dgawk -f test.awk dgawk> trace ...


3

There is a lot to be improved about your approach: On most systems (if /proc is not mounted with hidepid) you don't need root privilege for ps. There is no need for two grep instances just to get rid of the first in the process list. Do this instead: grep '[w]get' There is no use in grep filtering input for awk. awk can do that pretty well on its own: awk ...


0

First of all check the format of the dates in the log, then use: grep *here goes the date* > example1.log For example: grep "Dec 11" > example1.log Now you have a smaller file called example1.log that contains only the records of December 11.


1

Let's say you recipes folder in in /home/username/recipes Here is a simple script: #!/bin/bash count=0 dir=/home/username/recipes/ for recipe in $(ls $dir); do if [ $(cat $HOME/recipes/$recipe | grep $1) ]; then ((count++)) fi done echo "$count $1" Once you've saved the file, you have to make it executable. Do this with: chmod 740 script.sh ...


2

Use grep -c For example kaustubh@hacked:~/test$ cat a salt salt salt salt salt kaustubh@hacked:~/test$ cat b here sis salt their is salt and everywhere is salt kaustubh@hacked:~/test$ cat c salt hash hash salt sweet salt kaustubh@hacked:~/test$ cat d this is line salt Now I want count of word salt in files: grep -c salt * kaustubh@hacked:~/test$ ...


1

As a variation on the answer already here - it is easier still, probably, to pull the script directly out of a pipe: sed 's|.*|s/&//g|' <patterns | sef -f - infile >outfile That way you needn't alter the patterns file - or any other - directly and can instead modify it in-stream to suit your needs.


1

For simplistic scenarios involving fixed string search and space-separated words in my_text.txt, GNU awk might work, although output order may not match that of words_of_interest.txt awk 'NR == FNR{a[$0]; next}; $0 in a{b[$0]++}; END{for (k in b) print k, b[k]}' words_of_interest.txt RS='[[:space:]]+' my_text.txt hi 2 joe 1


2

If you previous sort your files, you could do in a simple way: $ join file2 file1 ENSG00000223116 0 0 AL157931.1 ENSG00000233440 1.71449394 50 HMGA1P6 To sort your files just do the following: sort file1.txt > file1 sort file2.txt > file2


2

sed 's/^.*/s\/&\/\//' pattern > sed-pattern-file sed -f sed-pattern-file myinitialfile.txt > mycleanfile.txt


2

I would use the following: sed -r '/(SEM|AFF|CON)/ s/([:,] *)[Tt]he */\1/g' file Add -i option to change file in place.


1

Try this way: egrep -rl "^(SEM|CON|AFF)\: (t|T)he" * | xargs sed -r -i 's/(^(SEM|CON|AFF):\s)((t|T)he[ ]*)/\1/g'


2

Just use one sed expression (needs GNU sed): sed -r -i -e '/(SEM|AFF|CON)/s/([:,]\s*)the\s+/\1/ig' * The search pattern at the begin of the sed command restricts the replacement to the lines which begins with the selected categories. The i flag for the replace command (s//) makes the pattern case-insensitive, the g flag allows more than on replacement in ...


0

I know I have not solved using sed or awk or any of shell commands, However tcl did work out well. I have kept the script simple and user friendly. elements like 4a abc etc will be taken care command script.tcl file Script #!/usr/bin/tclsh if {$argv == ""} { puts "please enter the arguement" exit ; } set tar_fl [lindex $argv 0] if ...


2

grep -E 'fatal|error|critical|failure|warning|' *.log


1

You can use: tr -dc '\-0-9\n' | sort -u -t- -nk1,1 | grep -c . ...which is, admittedly, more than a little inspired by muru's answer here. Differently, though, I use grep to count the lines rather than wc in case there are blank lines in input. His answer doesn't have a blank line problem as grep -o will only print lines with their match (as grep -c only ...


0

An awk solution awk -F'-' '{sub(/[^[:digit:]]+/, "", $1); a[$1]} END{for (k in a) ++i; print i}' file 8


1

Use extended grep to and look for four digits, telling grep to only list the matches (as opposed to the whole line, which is the default): grep -Eo '[0-9]+' <filename> Sort this list of numbers and only output unique ones: sort -u Count the number of lines: wc -l Put it all together: $ grep -Eo '[0-9]+' filename | sort -u | wc -l 8


4

With grep, filter out just the numbers: grep -Eo '[0-9]+-' file | sort -u | wc -l [0-9] Matches any character between 0 and 9 (any digit). + in extended regular expressions stands for at least one character (that's why the -E option is used with grep). So [0-9]+- matches one or more digits, followed by -. -o only prints the part that matched your ...


0

With perl, without any shell pipes (quicker) : $ perl -lne '/\d+-/ and $h{$&}++;END{print scalar keys %h}' file


1

The shell options don't inherit to forked shells, but that's what Vim does when you use the :! command: it launches a new shell. You can influence those forked shells in Vim via the 'shell' option: :set shell+=\ -O\ globstar or :set shell=/bin/bash\ -O\ globstar


1

try grep -E -o 'mg91a02\w+' where -E : extended regexp -o print only matched word \w : not a white space + one or more time


1

With Perl regular expressions: grep -Po '\w*mg91a02\w*' For example: $ grep -Po top .zshrc top top top top top top top $ grep -Po '\w*top\w*' .zshrc setopt setopt autopushd setopt setopt setopt setopt


1

grep -o 'mg91a02[^ ]*' file This means print pattern mg91a02 until not seen^ first space([^ ]) or means print everything which is not a space.


0

One of my text files was suddenly being seen as binary by grep file Tel.txt Tel.txt: ISO-8859 text Solution iconv -t UTF-8 -f ISO-8859-1 Tel.txt > TelNew.txt


2

You can use sed to filter these. This simple example assumes you know the exact start/end time: sed -n '/Dec 4 14:37:36.381651/,/Dec 5 14:32:36.391572/' filename You cannot round those time/dates to values that don't exist. For example: sed -n '/Dec 4 14:30:00.000000/,/Dec 5 14:29:59.999999/' filename wouldn't work unless the specified times were both ...


2

One solution, converting date to epoch : while read month dm hour rest; do d=$(date -d"$month $dm $hour" "+%m%d%H%M%S") echo "$d $rest" done < file | awk '$1 < 1204143737' # print all lines before this date


1

Another simple, alternative(for someone who dont want/know awk) script will be: #!/bin/bash sort -t',' -k 3 marathon | cut -d',' -f 3 | uniq -d if someone wants to print whole line instead of just names: #!/bin/bash sort -t',' -k 3 marathon | cut -d',' -f 3 | uniq -d | grep -f - marathon in over scripts: sort takes third field to sort, using , as ...


3

You have to sort using names first. Note: 'uniq' does not detect repeated lines unless they are adjacent. You may want to sort the input first, or use `sort -u' without `uniq'. You can use the -t/-k options, to sort these fields: sort -t',' -k 3 marathon that sort regarding the 3rd field with the comma as separator. Then you can print ...


1

Also this(for slightly shorter and more readability): grep -Ff <(awk '{print $1}' Cell_cycle.txt) filename.fasta


1

Use process substitution <(): fgrep -A 1 -f <(cut -d " " -f 1 Cell_cycle.txt) filename.fasta


2

\> is the (zero length) regexp for the end of the word so C\> will probably not match last names that start with a 'C'. Maybe you should try \<C instead. [[:alpha:]] matches exactly one character so this is also very unlikely to match a real name. You should append a multiplier like * or + (only in ERE?).


5

You could do it by using a simple join: join A.txt B.txt But, in order to work both files must be sorted on the join key (here the first (blank separated) field). To do it, just use sort -b filename.


3

Since you have also tagged awk: awk 'FNR == NR {a[$1] = $0; next}; {print a[$1]}' A.txt B.txt I don't think a single grep can do this, but a combination of xargs and grep: xargs -I{} grep -Fw -- {} A.txt < B.txt


1

To negate regular expressions is not easy. You could use negative lookbehinds: $ grep -C4 -P '(?<!call).*fn1' test.txt 5-even more main code 6-call fn2 7-still more main code 8- 9:function fn1 10-call fn3 11-fn1 code 12-more fn1 code This grep uses Perl-style regular expressions (-P) to look for any instance of fun not preceded by call. And you can ...


2

grep -E '^(\s*[0-9]+\s+){4,5}[0-9]+\s*$'


1

awk '{l=$0; n = gsub(/[0-9]+/, "", l)}; n == 5 || n == 6' (same principle as in Joseph's answer)


0

A way with awk that is customisable for different numbers of fields. Also whitespace does not matter. awk 'NF~/^(5|6)$/{x=0;for(i=1;i<=NF;i++)x+=($i~/^[0-9]+$/)}x==NF' file This checks the number of fields is 5 or 6 although more numbers of fields could be added if your requirements ever change. Then it sets a counter to 0 Then loops checking each ...


2

Another perl: $ perl -MList::Util=first -Tnle ' s/^\s+|\s+$//g; @e = split /\s+/; print if @e == 5 || @e == 6 and !first {/\D/} @e; ' file 10 2 12 1 13 Explanation s/^\s+|\s+$//g trim the line. @e = split /\s+/ split the line into array @e. We will print the line if: array @e contains 5 or 6 elements. And None of its ...


4

grep -E '[0-9]{5}' is looking for numbers with at least 5 digits. What you need is 5 numbers with at least one digit: grep -E '[0-9]+([^0-9]+[0-9]+){4}' [0-9]+ - a number of at least one digit [^0-9]+[0-9]+ - a number with at least one digit, preceded by at least one non-digit character. We then repeat this 4 times to get 5 numbers separated by ...



Top 50 recent answers are included