Hot answers tagged

24

If you browse through the less man page, you'll notice less has an INPUT PREPROCESSOR feature. echo $LESSOPEN to view the location of this preprocessor, and use less/vim/cat to view its contents. On my machine this preprocessor is /usr/bin/lesspipe.sh and it includes the following for rpms: *.rpm) rpm -qpivl --changelog -- "$1"; handle_exit_status $? ...


17

You can use cut to split files with columns on a specific delimiter: cut -d: -f6 /etc/passwd Or -f1,6 for columns (fields) 1 and 6.


13

Use the \ character to escape the * to make it a normal character. grep '^\*\*' test.out Also note the single quote ' and not double quote " to prevent the shell expanding things


10

From the character of the wine's output I conclude that it is some kind of error message, therefore probably is redirected to stderr, not stdout. In that case pipe doesn't transmit the message and next command (while) has nothing on its input. To overcome the problem you need to e.g. redirect wine's stderr to stdout by adding 2>&1 in front of the pipe ...


9

Grep is really not the tool for parsing out data this way, grep is more for pattern matching and you're trying to do text-processing. You would want to use awk. awk -F":" '$7 == "/bin/false" {print "User: "$1 "Home Dir: "$6}' /etc/passwd awk The command -F":" Sets the data delimiter to : $7 == "/bin/false" Checks if the 7th data column is /bin/false {...


8

Save this to a file, let's say patterns.awk, then call awk -f patterns.awk patterns data, where patterns is your first file, and data the second: NR == FNR { prefix[NR] = $0; pattern[NR] = $2; count++; next; } { for (i = 1; i <= count; i++) { if (index($1, pattern[i]) > 0) { print prefix[i] " " $0; ...


7

As you wanted to check the line which starts with ** and ends with ), you can combine two grep operation like this, grep '^*\*' test.out | grep ')$' Or with single grep command like this, grep -E '^\*\*.*\)$' test.out Explanation ^\*\* : match line which starts with ** .* : match everything after ** \)$ : match line which also has ) at the end of ...


5

This is a very simple use of grep with regular expressions. This will do it: grep -E '^[g0-9]' file.txt I'm not sure what part you need help with, but I recommend you read man grep and learn about regular expressions. Note that you must use -E to enable "real" regular expressions (which are all you need to worry about), and protect your regexp with ...


4

On Linux you can do: filetypes=( $(grep -Po 'FILETYPE=\K.*' ${CONF_FILE} | tr ':' ' ') ) Or, more idiomatically: IFS=":" filetypes=( $(grep -Po 'GPGDIRLIST=\K.*' file)) Note that I used lower case letters for the array name, that's usually safer since environmental variables are usually capitalized. The way to save the output of a command as an array ...


4

If I would like to grep through this meta data less showing me, how can I accomplish this? very simply; if you want to grep on "Version" for example: less your.rpm | grep "Version" Note that less is using the rpm command; so better skip using less; and use rpm commands; like: rpm -qip /path/to/uninstalled/rpm rpm -qi installed.rpm


4

This sed script prints the line number of the line matching /^};/ in the range of lines from /xkb_symbols "dvorak" {/ to the next /^};/ (which will be the same }; as the one we get the line number for): /xkb_symbols "dvorak" {/,/^};/{ /^};/= } If you need both start and end line numbers: /xkb_symbols "dvorak" {/,/^};/{ /xkb_symbols "...


4

I was able to put the answer together with help from this question. The program "wc" program counts newlines, words and byte counts. The "-l" option specifies that the number of lines is desired. For my application, the following worked nicely to count the number of instances of "somePattern": $grep -r "somePattern" | wc -l


4

You can do it without the grep: df --output=target,size /mnt/xyz | awk ' NR==2 { print $2 } ' df accepts as argument the mount point; you can tell to awk too to print both the second line only (NR==2) , and the 2nd argument, $2. Or better yet, cut the target as you are not outputting it, and it becomes: df --output=size /mnt/xyz | awk ' NR==2 ' When I ...


3

grep is defined to match, and retain or discard, lines, so it has to read the whole line before doing the match; that can't accomplish what you want. First you need to verify if command does this echoing of input chars one by one to a pipe. Standard C programs (and sometimes other programs using C stdio) by default use line-buffering when stdout is an '...


3

pdfunite $(sed 's/$/_*.pdf/' filenames.txt) output.pdf So if filenames.txt contains CSAI_isotig00407:342-556 CSAI_isotig00408:342-556 That command will effectively do pdfunite CSAI_isotig00407:342-556_*.pdf CSAI_isotig00408:342-556_*.pdf output.pdf


3

You can use the options together like: grep -vf exclude.txt file.txt Example: $ cat exclude.txt 3 $ cat file.txt 1 3 4 1 4 3 1 2 $ grep -vf exclude.txt file.txt 1 4 1 4 1 2


3

When you run the command: grep -r <pattern> <directory> it prints all the filenames as <directory>/<filename>. If you put a slash at the end of <directory>, it will be included in that, so you end up with a double slash. The code could have been smart enough to notice when the original directory name ends with / and omit it ...


3

Give grep with Perl Compatible REgexp module a try: to remove two-letters combinations: pcregrep -Mv '>.*\n([ACGT])\1*([ACGT])\2*(\1|\2)*$' file output: >NB501013:9:HJJ75BGXX:4:21602:19346:16945/2 CTCGTCGCATCACAAAGGGAT >NB501013:9:HJJ75BGXX:3:11407:17650:13229/2 CCGCGGGCCGGTGCGGGGGTTTTTTTGTTTTTTTGGTTACAACGGGTGGG >NB501013:9:HJJ75BGXX:3:...


3

#! /usr/bin/awk -f /"dvorak"/ {dvorak++}; /{/ && dvorak {b++} ; /}/ && dvorak {b--} ; dvorak && b == 0 && NR > 1 { print NR; exit } $ ./find-dvorak.awk /usr/share/X11/xkb/symbols/us 248 This uses a counter (b) which gets incremented every time it sees an open-curly-bracket { and decremented whenever it sees a ...


3

Using sed sed -i 's+^mynetworks.*+& 0.0.0.0/0+' log.txt Using awk awk '/^mynetworks/ {$0=$0" 0.0.0.0/0"} 1' log.txt or awk '{if ($1 ~ /^mynetworks/) print $0, "0.0.0.0/0"; else print $0}' log.txt Using bash while read -r line ; do [[ $line == mynetworks* ]] && line+=" 0.0.0.0/0" echo "$line" done < log.txt


3

It's not the shell None of the answers so far has touched on the real problem. It would be helpful to explain why it does not work as you expect. grep -i "^**" test.out Because you have quoted the pattern to grep, * is not expanded by the shell. It is passed to grep as-is. This is explained in the manual page[1] for bash[2]: Enclosing characters in ...


2

It's quite a job for sed: $ printf 'foo\nbar\n' | sed -n '$!N;/\nbar$/P;D' foo


2

In an attempt to give this Q a proper answer, based - on - the - comments (heeding Sobrique's note that parsing XML should really be done with an XML parser): perl -CSD -lne 'print for /\w{63,}/g' input-file-here


2

If you are looking to match only part of the string on a given column, you can use advice from http://stackoverflow.com/questions/17001849/awk-partly-string-match-if-column-partly-matches some_command | awk '$6 ~ /string/'


2

You can use tac to reverse the lines of file and delete 3 lines before matching pattern including line which contains the matching pattern using sed, like this: tac filename | sed '/0 hits/I,+3 d' | tac and if you wanna edit file in place you can use -i option in sed command like, tac filename | sed -i '/0 hits/I,+3 d' filename | tac


2

I'd do something like (assuming GNU tools): find /mnt/SAN/documents -type f -print0 | awk -F / ' NR == FNR{check[$0]; next} $NF in check {print "found:", $0; delete check[$NF]} END { for (i in check) print "Not found:", i }' filename.list RS='\0' - Which would find one occurrence for each filename in filename.list. Or to report all ...


2

First of all, google for regex sites. They will help you a lot. For example, try regexone. Secondly, to test or help with understanding regexes, refer to regex101.com - this can help you a lot, since it explains what happens step by step; you can also test regexes. Now as for expression: [aeiou] matches any of characters in brackets(here: a, e, i, o or ...


2

The regular expressions used by tools like grep can understand boolean OR (|): Basic Regular Expressions grep '^g\|^[0-9]' file Extended Regular Expressions grep -E '^(g|[0-9])' file Perl Compatible Regular Expressions grep -P '^(g|\d)' file Alternatively, you could use other tools: sed sed -n '/^g/p;/^[0-9]/p' file or sed -En '/^(g|[0-9])/p' ...


2

The grep utility doesn't let you do this out of the box, so in this case you will have to resort to a loop. Something on the lines of this will do the trick: i=0 while read -r pattern; do outfile="data-$(( i++ )).out" fgrep -w "$pattern" data.in >$outfile if [ ! -s $outfile ]; then rm $outfile fi done <patterns.in This also ...


2

Use the RANDOM variable of your shell to get a random number, but seed the random number generator with today's date first (if the script hasn't been used since midnight). Then pick that line out of the file. In other words (Bash below)... wotd_data="wotd_data.txt" stamp="$HOME/.wotd-stamp" stamp_random="$HOME/.wotd-random" date_now=$( date +"%Y%m%d" ) ...



Only top voted, non community-wiki answers of a minimum length are eligible