Tag Info

Hot answers tagged

18

Quote the argument to grep, thus ls -a | grep '^\.' The reason for this is that the shell handles \. and turns it back into plain ., which grep then treats as a single-character wildcard. If in doubt, always quote a string that contains (or might contain) a character that's special to the shell.


11

You need to put the grep regex inside quotes. ls -a | grep '^\.' Note: Don't parse the output of ls command.


10

Use this: sed 's/|END|/\n/g' test.txt What you attempted doesn't work because sed uses basic regular expressions, and your sed implementation has a \| operator meaning “or” (a common extension to BRE), so what you wrote replaces (empty string or END or empty string) by a newline.


9

How about using cut? If you'd like to print the 2nd pattern echo "$above_string" | cut -f2 -d "?" Second column onward echo "$above_string" | cut -f2- -d "?"


9

Bash can match regular expressions with the =~ operator in [[ ... ]]: #! /bin/bash line='attempting to create a 512^3 level (with Dirichlet BC) using a 16^3 grid of 32^3 boxes and 800 tasks...' num='([0-9^]+)' nonum='[^0-9^]+' if [[ $line =~ $num$nonum$num$nonum$num$nonum$num ]] ; then level=${BASH_REMATCH[1]} grid=${BASH_REMATCH[2]} ...


9

The following detect integers, positive or negative, and work under dash and are POSIX: Option 1 echo "$1" | grep -Eq '^[+-]?[0-9]+$' && echo "It's an integer" Option 2 case "${1#[+-]}" in ''|*[!0-9]*) echo "Not an integer" ;; *) echo "Integer" ;; esac Or, with a little use of the : (nop) command: ! case ${1#[+-]} in ...


9

GNU grep's -w will only consider the 26+26+10+1 (ASCII letters, digits and underscore) as word constituents. You can't change that, even by using a different locale (even in a locale where é is considered a letter, Stéphane would still be St and phane separated by the non-word é). You can however implement that logic by hand: grep -E ...


7

Many, many ways. Here are a few: GNU Grep $ echo 3.14.37-1-lts | grep -oP '^[^-]*' 3.14.37 sed $ echo 3.14.37-1-lts | sed 's/^\([^-]*\).*/\1/' 3.14.37 Perl $ echo 3.14.37-1-lts | perl -lne '/^(.*?)-/ && print $1 3.14.37 or $ echo 3.14.37-1-lts | perl -lpe 's/^(.*?)-.*/$1/' 3.14.37 or $ echo 3.14.37-1-lts | perl -F- -lane 'print $F[0]' ...


7

The following worked fine for me: $ sed 's/|END|/\ /g' foobar T|somthing|something T|something2|something2 Notice that I just put a backslash followed by the enter key.


5

You can use awk: $ awk -F'\\|END\\|' '{$1=$1}1' OFS='\n' file T|somthing|something T|something2|something2 -F'\\|END\\|' set field separator to |END| OFS='\n' set ouput field separator to newline $1=$1 cause awk reconstruct $0 with OFS as field separator 1 is a true value, causeawk print the whole input line


5

Whether dash, bash, ksh, zsh, POSIX sh, or posh ("a reimplementation of the Bourne shell" sh) ; the case construct is the most widely available and reliable: case $1 in (*[!0-9]*|"") false ;; (*) true ;; esac


4

Using Awk to print the second and third records separated by newlines: awk -F"?" '{printf "%s\n%s\n", $2,$3}' Elvis August 16 Leonard Nimoy February 27 If you want to swap out the record, you can set it as a variable: awk -v record=2 -F"?" '{print $record}' Elvis August 16


4

echo $above_string | grep -oP "^([^?]*\?){2}\K[^?]*" Change 2 to the n - 1 value in order to obtain the nth string. This assumes that you want the nth string in that line. You have n - 1 strings with no ? ending with a literal '?' (\? since it's a special character in perl regex). Then with \K you state you are not interested in the previous contents, ...


4

Try standard regexps (instead of perl regexps). This will print matching lines: awk '/\[[[:digit:]]+\]/ { print }' maillog To extract and print the matching value inside the brackets: awk 'match($0,/\[[[:digit:]]+\]/) { print substr($0,RSTART+1,RLENGTH-2)}' maillog


4

In Vim, you could limit your substitution to the lines that contain NX: :g/NX/s/N1/NX/ Preceding the substitution with /NX/ makes Vim perform it only on the next line that contains NX (using ranges), and using :g makes it run on all lines that match NX.


4

The best solution for this is to use awk: $ ldd /usr/bin/ppdhtml | awk '/ => / { print $1 }' | head -n1 libcupsppdc.so.1 To do this using grep, you will need to use the lookahead and lookbehind features of PCRE: $ ldd /usr/bin/ppdhtml | grep -Po '(?<=\t).+(?= => )' | head -n1 libcupsppdc.so.1 The lookahead and lookbehind features affect that ...


3

First, there's a problem: you didn't mention encoding in your question, which makes me inclined to believe that you aren't aware of how it factors into this. Character encodings determine the meaning of individual bytes. This means we need to start with a small history lesson, and the answer to whether or not this is easy is already "no". Your example ...


3

Another possibly awk command and using its RS option would be: awk '$1=$1' RS="\|END\|" file Will print those records (based on awk's Record Separator) which are not empty( has at least one field) to prevent printing empty lines. Tested on this input: T|somthing|something|END|T|something2|something2|END| Test|END| |END| Gives this output: ...


3

Perl solution: perl -ne '$id = "6d87de8e-1276-4496-b49d-dd4cd375cbe4"; print if $match = (/IDA:$id/ .. /IDA:(?!$id)/) and $match !~ /E0$/ ' *.log Explanation: /regex1/ .. /regex2/ returns true for lines between the matches. IDA:(?!$id) meand IDA: not followed by $id. the last line in a range is denoted by the E0 suffix ...


3

With sed you can do: sed '/\n/P;//d;s/[^?]*/\n&\n/[num];D' ...where you would replace the [num] above with some number representing the desired occurrence. If the numbered occurrence you specify does not exist, as is demonstrated in the following example, sed will simply print nothing at all. echo ,2,3 | sed '/\n/P;//d;s/[^,]*/\n&\n/4;D' Above ...


2

Another approach: awk -F'[][]' 'NR>1{print $2}' maillog 7445 15674


2

sed You can use sed for this, but it is not advisable, e.g. here is a zero-based solution that uses a quantifier to select the desired field: n=1 sed 's/\([^?]*? *\)\{'$n'\}//; s/?.*//' <<<"$above_string" Output: Elvis August 16


2

You have to use \s instead of \w (which match a word character) to match any single character considered whitespace, include [\t\n\f\r ]: ldd /usr/bin/ffmpeg | grep -oP "^([a-z0-9.-]|\s)*" or: ldd /usr/bin/ffmpeg | grep -oP "^\s*[a-z0-9.-]*"


2

Another way with sed that doesn't print empty lines: sed 's/|END|/\ /g;/^$/!P;D' infile e.g. input: T|one|two|END|T|three|four|END| T|five|six|END|T|seven|eight|END| T|nine|ten|END|T|eleven|twelve|END| output: T|one|two T|three|four T|five|six T|seven|eight T|nine|ten T|eleven|twelve same thing with ed: ed -s infile <<'IN' 1,$j s/|END|/\ /g ...


2

It is possible to enable the /g flag on :s substitutions by default by setting set gdefault in .vimrc. If this is set using :%s/PATTERN/gn will cause Vim to count occurrences of chars or words once per line and ignore all other occurrences. Either use :%s/PATTERN/n in this case or remove set gdefault from .vimrc.


2

The search in less uses regular expressions. To accomplish the search you are looking for, simply replace the shell wildcard ? with .. /x 1..


2

You have to use a negative lookaround: perl -pe 's|\[-((?!-\]).)*-\] {\+(\S+… https://t.co/.*)\+}|\2|' <<<$text The problem is the first occurence of [- matches. Then the non-greediness of the pattern doesn't take the desired effect, it doesn't matter how non-greedy it is. With a negative lookaround you can match everthing except of the string ...


2

I’ve simplified your input string to qABxBCzABxBCDEFw, where A represents [ B represents - C represents ] D represents {\+ E represents the text between the +s (including the URL) F represents \+} Lower case letters represent everything else. So here are some substitute commands run against the input: Command ...


2

You should not believe them if they tell you it cannot be done. You should believe them, however, if they tell you it's not easy. sed '\|*/|!{ s|/\*|\n&| #if ! */ repl 1st /* w/ \n/* h; s|foo|bar|g;/\n/!b #hold; repl all foo/bar; if ! \n branch G; s|\n.*\n||;:n #Get; clear difference; :new label n; ...


2

If this is output from a program / script you've written and the text is formulaic (i.e. follows this pattern exactly) you can just use cut. #!/bin/bash $STRING='attempting to create a 512^3 level (with Dirichlet BC) using a 16^3 grid of 32^3 boxes and 800 tasks...' level=$(echo $STRING | cut -d' ' -f5 -) grid=$(echo $STRING | cut -d' ' -f12 -) ...



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