Tag Info

New answers tagged

0

Python In python i would do like the below, #!/usr/bin/python import re import sys file = sys.argv[1] with open(file, 'r') as f: for line in f: print ' '.join(re.findall(r'(?<!\d)\d{4}(?!\d)', line)) Save the above script as script.py and then run it by applying the below command on the terminal. python script.py file Example: $ cat ...


0

First of all, you can use almost the exact same regular expression and syntax as you did with sed. Just change & to $& for Perl: echo "WARN ERROR foo" | perl -pe 's#WARN#\x1b[33m$&#; s#ERROR#\x1b[31m$&#; s#foo#\x1b[32m$&#' Or, using your original Perl approach, just remove the .* on either side of the pattern you want to ...


1

Here's an example that highlights from ERROR to the end of the line, the whole line containing WARN, and foo but nothing surrounding it. Only the first matching rule is applied (e.g. WARN: ERROR foo sets ERROR foo in red), tune as you see fit. perl -pe 's/ERROR.*/\e[31m$&\e[0m/ || s/.*WARN.*/\e[33m$&\e[0m/ || s/foo/\e[32m$&\e[0m' An ...


1

With a single s/// you can do this in a POSIX sed - but there are a lot of backslashes: sed 's/[^0-9]*\([0-9]\{5,\}\)*[^0-9]*\([0-9]\{4\}\)*.\{0,1\}/ \2/g ' <<\IN 92828 Hello 8473 World War 1 1914-1918 Hello 8391 World War 2 1939-1945 IN OUTPUT 8473 1914 1918 8391 1939 1945 It globally gobbles all of the sequences with 5 or more digits first - ...


0

with gawk echo '9228 Hello 8473 World War 1 1914-1918 Hello 8391 World War 2 1939-1945' | awk --re-interval -v RS='\\y[[:digit:]]{4}\\y' '{printf "%s ", RT}END{print ""}' 9228 8473 1914 1918 8391 1939 1945


4

You can use perl. perl -nle'print join " ", /(?<![0-9])[0-9]{4}(?![0-9])/g' This also works for multi-line input, so if you have: 9228 Hello 8473 World War 1 1914-1918 Hello 8391 World War 2 1939-1945 You'll get the following returned: 9228 8473 1914 1918 8391 1939 1945 (Add -0777 if you want the numbers on the same line.)


2

With perl: $ perl -Tnle 'BEGIN {$, = " "} print $_ =~ /\b\d{4}\b/g' file 9228 8473 1914 1918 8391 1939 1945


3

POSIXly: < file tr -cs 0-9 '[\n*]' | grep -xE '.{4}' | paste -sd ' ' -


7

I can answer with grep command: Input file: 9228 Hello 8473 World War 1 1914-1918 Hello 8391 World War 2 1939-1945 Command: grep -Eo '\<[0-9]{4}\>' file |tr '\n' ' ' Return any number with length=4. -E switches to extended regex -o print only the matching part Output: 9228 8473 1914 1918 8391 1939 1945 Update answer: Input file: 9228 ...


2

grep '\(^\|[^b]\)bb\([^b]\|$\)' or grep -E '(^|[^b])bb([^b]|$)' That is: search for an occurrence of bb that is preceded by either the beginning of the line or a character different from b, and that is followed by either a character different from b or the end of the line.


3

I guess the most straight-forward way is: grep '^[^b]*bb[^b]*$' file1 Btw, for commands like grep that accept a file name argument it's more efficient to do grep '^[^b]*bb[^b]*$' file1 or grep '^[^b]*bb[^b]*$' < file1 (the latter working if no file argument is supported, too) than cat file1 | grep '^[^b]*bb[^b]*$' and often more flexible.


1

\w match "word" symbols (letters, digits and underscore) but in your example there is / after com which is not :alnum: so your pattern match nothing == empty output. You can add / to pattern and look what is happend: grep -oP 'com/\K\w+' FYR -P option is experimental and can do which not expected in more systems, so you can do your task in other way: ...


2

one can use awk too: awk 'NR==FNR{a[$0]=$0}NR>FNR{if($1==a[$1])print $0}' pattern_file big_file output: denovo1 xxx yyyy oggugu ddddd denovo22 hhhh yyyy kkkk iiii


3

You probably want the -wflag - from man grep -w, --word-regexp Select only those lines containing matches that form whole words. The test is that the matching substring must either be at the beginning of the line, or preceded by a non-word constituent character. Similarly, it must be either at ...


0

It is a bad idea to validate a number range based on regex, but if you still insist below regex can be used. It does not match if the numbers have comma in it. You can remove comma before doing this regex match. ^(:?(?=[1])(10{0,11})|(?=[^0])(\d{1,12})|0)\.[0-9]{1,2}$ You can check it out in regex101.


1

Test file content: start cmd:> cat file 999,999,999,999.99 999999999999.99 9,999,999,999,999.99 9999999999999.99 0 0.00 00 00.00 grep -E '^(0|0\.[0-9]{1,2}|'\ '[1-9][0-9]{0,2}(,?[0-9][0-9][0-9]){0,3}(\.[0-9]{1,2})?|'\ '[1-9][0-9]{0,2}(,[0-9][0-9][0-9])*(\.[0-9]{1,2})?)$' file 999,999,999,999.99 999999999999.99 9,999,999,999,999.99 0 0.00


1

In awk , i would do like this, $ echo 'NISHA =\455' | awk '{gsub(/\\/," ")}1' NISHA = 455


1

If you don't want sed solutions, then try with following commands: $ echo "NISHA =\455"| awk -F'\' '{print $1 $2}' $ echo "NISHA =\455"| tr '\\' ' ' $ echo "NISHA =\455"| tr -d '\\' $ echo "NISHA =\455"| cut -c 1-7,9-11


1

sed 's/\\//' Place a \ before \ like you do with most special characters.


2

You can either replace the backslash by a space as you show in the example result: sed 's/\\/ /g' or you can remove it as you show in your code: sed 's/\\//g' Special characters There are possible problems with escaping of backslash to cancel its special meaning. Backslash is a special character used for escaping both in a shell and in regular ...


2

You have to escape the backslash. Try this: sed 's/\\//g'


1

With GNU sed: sed 's/.*DEBUG \(\w*\).*/\1/' | uniq -c 4 FtpsFile 1 JobQueue With grep: grep -Po 'DEBUG \K\w+' | uniq -c 4 FtpsFile 1 JobQueue With awk: awk '$6=="DEBUG"{print $7}' | uniq -c 4 FtpsFile 1 JobQueue The last one can be done in pure awk, but for a sake of similarity I piped it to uniq.


0

You can use awk(instead of sed) to avoid looking at fields before the one you are interested in, then cut the section you want to see out: [hunter@apollo: ~]$ cat filename.log | awk -F, '{ print $6 }' | cut -c 1-15 | uniq -c | sort -rn | head -10 4 DEBUG FtpsFile 1 DEBUG JobQueue (Note: you were also sorting twice, which seems unneeded) ...


0

Quick fix - I added the following cut command to single out that field: [host:~]$ cat logfile | cut -d" " -f7 | sort | uniq -c | sort -rn | head -10 4 FtpsFile 1 JobQueue In my eagerness to K.I.S.S., this doesn't apply to classes with a space in the name.


0

The problem is the \w. I only found this out myself when attempting to figure out what's going on here, but apparently, things like \s or \w which are already character classes cannot be used inside other character classes with ERE (they can with PCREs). To illustrate: $ echo foo- | sed -r 's/[\w-]+/bar/' foobar $ echo 'w\-foo' | sed -r 's/[\w-]+/bar/' ...


2

Yet another possibility using extended regexp (-r): sed -r 's/(mysql)(_query\()/\1i\2$link,/g'


6

You don't need to capture anything: sed 's/mysql_query(/mysqli_query($link, /g' file Don't use -i while you're testing, add that when you're satisfied with the results.


1

You don't need a capture group around mysql_query, because you're not copying that into the replacement. You only need to capture the query argument, since that varies in each call. sed -ri 's/mysql_query\s*\((\$\w+)\)/mysqli_query($link, \1)/g'


1

You should probably realize that there's no single type of regular expressions. There are at least basic regular expressions or BRE (sometimes only RE), extended regular expressions or ERE and perl compatible regular expressions or PCRE. All those languages use slightly different syntax. Current versions of GNU grep support all three and the BRE are ...


0

The first line works if you leave out the ^. Maybe it worked but it didn't work the way you assumed? I doubt that grep's behaviour has changed in such an important point. echo does not translate escape sequences by default. You need the -e for that. Similar with the shell. You need $'...' to make the shell use escape sequences.


3

ANSI-C Quoting According to the Bash manual, this is called ANSI-C quoting. The manual says: Words of the form $'string' are treated specially. The word expands to string, with backslash-escaped characters replaced as specified by the ANSI C standard. In practice, this means that '\t' will not be expanded into a tab character, but $'\t' will. The ...


1

The following regular expression should work: -regex ".*/$x[^0-9].*" It contains the number right after the slash followed by a non-number.


0

Pythonish three liner, untested: python -c 'import sys for l in sys.readlines(): print("| ").join([f.strip() for f in l.strip().split("|")])'


3

If you already have the variable TEST just print it with all letter removed printf "%.0f\n" ${TEST//[a-z]/} or printf "%g\n" ${TEST//[a-z]/} Do not use %d or echo-command becouse numbers with leading 0 is understood as octal


4

POSIXly: test='rev00000010' number=${test#"${test%%[1-9]*}"} Would remove every thing to the left of the left-most non-zero digit. Bournely/universally: number=`expr "x$test" : 'xrev0*\(.*\)'`


2

A few more options (though I also recommend you use Parameter Expansion as suggested by @choroba): Use sed or perl to replace everything but the last two characters with the last two characters. This effectively deletes everything except the last two. $ sed -r 's/.*(..)/\1/' <<<$TEST 10 $ perl -pe 's/.*(..)/\1/' <<<$TEST 10 Set awk's ...


6

The commands you passed to sed mean: if a line matches the regex, delete it. That's not what you want. echo "$TEST" | sed 's/rev0*//' This means: on each line, remove rev followed by any number of zeroes. Also, you don't need sed for such a simple thing. Just use bash and its parameter expansion: shopt -s extglob # Turn on extended globbing. ...


1

Not exactly what OP asks for - but I think it's relevant, so I'll cross post Recommendation for Regex editor? #549107 - Ask Ubuntu: Here's my attempt at a visual regex GUI tool, called visRegexTester.py: http://sourceforge.net/p/sdaaubckp/code/HEAD/tree/single-scripts/visRegexTester.py It has a "live preview" ("Auto rerun") if you type and change the ...


3

I have no idea what wget you're talking about but I am guessing that you want to download the file. If so, yes, you can download it and parse it with no intermediate temp file: $ value=$(wget -O - http://example.com/file.html | grep -oP 'strValue="\K[^"]+') $ echo $value 57


4

You can extract a value in your example with grep and assign it to the variable in the following way $ x=$(wget -0 - 'http://foo/bar.html' | grep -Po '<value.*strValue="\K[[:digit:]]*') $ echo $x 57 Explanation: $(): command substitution grep -P: grep with Perl regexp enable grep -o: grep shows only matched part of the line \K: do not show in the ...


-2

cat input | grep -o strValue=".*" | sed 's/strValue=//g' | sed 's/"//g'


2

If you really must do it in pure bash: $ foo="cjkuni-ukai-fonts-0.2.20080216.1-35.el6.noarch" $ [[ $foo =~ [0-9.]+-[0-9]* ]] && echo $BASH_REMATCH 0.2.20080216.1-35 If you're OK with a sed solution: $ sed 's/.*-\([0-9.]*-[0-9]*\).*/\1/' <<<$foo 0.2.20080216.1-35 If you have access to GNU grep, you could also do: $ grep -oE -- ...


2

You already have a great sed approach so here's a Perl way: $ perl -00ne 'print if $.>1' file The -00 turns on "paragraph mode" where a line is defined by \n\n. Then, we print only if the current line number ($.) is >1.


2

You should be able to delete lines between addresses defined by 1 (the first line of the file) and /^$/ (a regular expression defining an empty line) sed '1,/^$/ d' file


2

rename works this way because it can move files between directories. Like mv, it acts on the whole path, not just on the last component. If you only want to modify the last component, you can anchor your regexp at (\A|?<=/), and make sure that it doesn't match any / and only matches at the last /. rename 's~(\A|?<=/)(?=[^/]*)\z~assi_~' ...


4

Try this: sed -r 's/.*(http[^"]*)".*/\1/g' On Mac OSX, try: sed -E 's/.*(http[^"]*)".*/\1/g' Notes There are several items to note about this sed command: sed 's/^.*(http.*)".*$/\1/g' The ^ is unnecessary. sed's regular expressions are always greedy. That means that, if a regex that begins with .* matches at all, it will always match from the ...


1

Using awk: echo '<a href="http://unix.stackexchange.com/users/20661/">Unix & Linux' \ | awk -F\" '{print $2}' http://unix.stackexchange.com/users/20661/


2

Just a lack of escaping: sed 's/^.*\(http.*\)".*$/\1/g' (I can never remember which ones expect () and which ones expect \(\) either.)


0

This can be done entirely within the shell, with a case construct. string='Fox juMPed the rock' case "$string" in *[:lower:]*) echo "The string contains lowercase letters.";; esac In ksh, you can use typeset -u to convert a string to uppercase. It doesn't matter whether the original was all-uppercase or not. string='Fox juMPed the rock' typeset -u ...


0

One idea that comes to mind is to take the @domain.com portions of the log file and simply use md5sum or sha1sum to mask the original values with a MD5 or SHA1 string instead. This demonstrates the mechanism. $ echo -n abc.com | md5sum 929ba26f492f86d4a9d66a080849865a - $ echo -n 123.com | md5sum cbff36039c3d0212b3e34c23dcde1456 - The same mechanism ...



Top 50 recent answers are included