Tag Info

New answers tagged

0

According to explanation in comments: sed " # for lines which starts with if /^if\b/{ /==/! { # add logic statement to first alphanums after ( s/\((\w\+\)/\1 == 1b'1 / # add logic statement to second alphanums after & if it is present s/\(&\!\?(\?\w\+\)/\1 == 1b'1 / # if ! sign in section replace 1 by 0 ...


0

Maybe something like: perl -0777 -pe 's{\bif\s*\K\((([^()]++|\((?1)\))*)(?=\))}{ $& =~ s{([&(])\s*(!?)\s*\(?(\w+\b)\)?(?!\s*(==|'\''))}{ "$1$3 == 1'\''b".(0+!!$2)}gers}ges'


2

sed -n '/-\{10,777\}/,/^\s*Table:/p' LaTeX.doc If you wants newline after each table: sed -n '/^\s*Table:/G;/-\{10,777\}/,/^\s*Table:/p' LaTeX.doc or sed '/-\{10,777\}/,/^\s*Table:/! d;/^\s*Table:/G' LaTeX.doc


2

I'll edit this if you update the question but I think you're looking for something like this: perl -007lne '@F=(/-{7,}.*?Table:.*?\n(?=\n)/gsm); print join "\n", @F' file.tex Explanation -007 : slurp the entire file -lne : add a new line to each print call, process the input file, and run the script given by -e. @F=(/pattern/gsm) : save all matches of ...


5

grep is a program that searches for regular expressions. The first argument for grep is the pattern to look for. In scripts and functions $1 is a reference to the first argument passed to that script or function. The ^ prepended to the argument is a standard regular expressions modifier that matches the beginning of a line -- this way you can ensure that ...


6

You have three tools that can do regular expressions. These all assume that $in contains na-examplename-01. grep $ printf "%s\n" "$in" | ./grep -E '^[a-z]{2,3}-[a-z]+[0-9]*-[0-9]+$' na-examplename-01 sed $ printf "%s\n" "$in" | ./sed -n '/^[a-z]\{2,3\}-[a-z]\+[0-9]*-[0-9]\+$/p' na-examplename-01 awk $ printf "%s\n" "$in" | ./awk ...


4

awk sounds like a good candidate: input='whatever even spaces and newlines xxx-blah12-0' # should not match input='na-examplename-01' # should match if LC_ALL=C awk 'BEGIN{exit(!(ARGV[1] ~ /^[a-z]{2,3}-[a-z]+[0-9]*-[0-9]+$/))}' "$input" then echo it matches else echo >&2 it does not match fi


4

You could use grep in extended regex mode like this: echo na-examplename-01 | grep -E '^[a-z]{2,3}-[a-z]+[0-9]*-[0-9]+$' You should use the interval parameter to make this more easy to read. [a-z][a-z]|[a-z][a-z][a-z] would be [a-z]{2,3}. [a-z]+ is the same as [a-z][a-z]* For the grep snytax, take a look at ...


0

This is a lot easier with xml2: echo '<KeyValue key="EMPTY_SEARCH_CRITERIA" type="String" value="CS_CODE" comment="Configured empty search criteria."/>' | \ xml2 | \ sed '/@value=/s/=.*/=SN_CODE,CS_ID/' | \ 2xml


0

If you particularly want change the CS_CODE with SN_CODE,CS_ID you can use: 's/CS_CODE/SN_CODE,CS_ID/g' If the values differ you can use 's/value=\S*\S/value="SN_CODE,CS_ID"/g'


0

sed is not the brightest choice for html (or any other programming language) processing, but anyhow: sed -e 's/\(value\)="[^"]*"/\1="SN_CODE,CS_ID"/' Two perhaps not obvious points: with \( and \) we group a pattern to use it later on with \1 [^"]* is used to match any char but a "


1

You are referring to regex back-references. Please check these two references: http://stackoverflow.com/questions/4609949/what-does-1-in-sed-do http://www.gnu.org/software/grep/manual/html_node/Back_002dreferences-and-Subexpressions.html And see the output of grep '\([0-9]\)\1' /etc/services which will give you a resultset of lines where a digit is ...


3

Your problem comes from the .*. If you only match every character that is not a ' or a " it will work: sed -ri "s/\[ ([0-9]+|(\x27|\x22)[^\x27\x22]*(\x27|\x22)) \]/[\1]/g" file.php Even better (to take possible " or ' into account): sed -ri "s/\[ ([0-9]+|(\x27|\x22)[^\2]*(\2)) \]/[\1]/g" file.php


8

Inside [...], backslash is not special. [\[] matches both backslash and [1. If you want to include the ] character in the set, you have to make sure it's first: []X] matches ] or X while [X]] would match X followed by ] (and [X\]] would match X or \ followed by ]). To exclude it, it has to be right after ^: [^]X] is any character but ] or X. So, in your ...


0

grep searches lines by default and can therefore normally not be used to find the character between lines. When I search for newline characters I usually replace the newline character (assuming UNIX line breaks here) with a different character that I know not to exist in the text, like so: cat file.html | sed 's/\n/%\n/g' | grep '<h2>%' | tr --delete ...


3

You need two things to match the line breaks (hence multiple lines) using grep : -z option of newer GNU grep, it will cause the lines to be separated by ASCII NUL rather than line breaks (?s) is called DOTALL modifier (with grep -P), it will cause the grep to match the line breaks (LF/CR) by . (dot) So in your case the following should work: grep -aPoz ...


0

You can use sed to replace only the lines matching the particular pattern. sed -e '/typeId="ProxyService"/s/xmlns:imp="http:\/\/www.bea.com\/wli\/config\/importexport"/xmlns:imp="http:\/\/www.bea.com\/wli\/config\/importexport2"/' new.xml This will replace your namespace only in the lines matching "typeId="ProxyService" . I've just added a 2 to your ...


1

Try grep -o 'myregex.*stuff' file and for the second question grep -o 'myregex.*stuff' file | sort | uniq. The -o grep switch will print only the matches instead of printing the whole line that matches the regex.


2

Try this way: :%s:\${ARRAY1\[@\]}:$1:g


3

:%s/\${ARRAY1\[@\]}/$1/ worked for me. Apparently, you must escape [ and ] but not { and }. I always use / instead of : as seperation, but %s:\${ARRAY1\[@\]}:$1:g works as well.


4

Some more choices. I have saved your example text in file for simplicity. grep and PCREs: $ grep -oP '(GRAPE|FRUIT)=\K.*?(?=,)' file purple yes violet affirmative To get them on the same line, just parse. For example $ grep -oP '(GRAPE|FRUIT)=\K.*?(?=,)' | paste -d" " - - – purple yes violet affirmative sed $ sed ...


2

Using Awk: awk -v RS="," -F= '/GRAPE/||/FRUIT/ {printf "%s ", $2}' Changes the record separator from a new line to , and the field separator from a space to a =, then match lines that contain the pattern GRAPE or FRUIT and print the second matching field on the same line separated by a space. Result: purple yes


4

Different tools and versions thereof support different variants of regular expressions. The documentation of each will tell you what they support. Standards exist so that one can rely on a minimum set of features that are available across all conforming applications. For instance, all modern implementations of sed and grep implement basic regular ...


1

I suspect that grep and sed are deciding differently when to apply the [] and when to expand the \w. In perl regex \w means any word character, and [] define a group to apply any of the characters within as a match. If you "expand" the \w before the [] it will be a character class of all the word characters. If, instead you do [] first you will have a ...


3

You are correct - \w is part of PCRE - perl compatible regular expressions. It's not part of the 'standard' regex though. http://www.regular-expressions.info/posix.html Some versions of sed may support it, but I'd suggest the easiest way is to just use perl in sed mode by specifying the -p flag. (Along with the -e). (More detail in perlrun) But you don't ...


1

With sed which doesn't support non-greedy *, you'd need to use tricks like: sed 's/_/_u/g;s/|/_p/g;s/<!!!>/|/g s/===[^|]*|//g s/|/<!!!>/g;s/_p/|/g;s/_u/_/g' Or with some sed implementations: sed 's/<!!!>/\ /g; s/===[^\n]*\n//g; s/\n/<!!!>/g' To support multi-line matching (as per your edit), with recent versions of GNU ...


2

With perl regular expressions, you can do non-greedy matches: perl -pe 's/===.*?<!!!>//g' file Edit 1: If you want to insert some text use that: perl -pe 's/===.*?<!!!>/Text you want to insert/g' file Edit 2: If it has to handle multiline comments as well use that: perl -pe 'BEGIN{undef $/;} s|===.*?</!!!>|insert|gs' file We have ...


0

choroba is mostly right ... but he's misleading about the double-quotes: that's not a problem for sed, but it is for the shell. You only need to escape * [ ] \ . and the delimiter (by default /). In your case, I believe this would work best: sed '7s%<<\([^>]*\)>>%<\1>%' file If it works, use the -i option to replace it in the file. ...


0

You can use: grep -Po "(?<=Version: )([0-9]|\.)*(?=\s|$)" If you have MIME-Version: 1.0 instead of MIME-Version: 1.0\n: grep -Po "(^|\s)+(Version: )\K([0-9]|\.)*(?=\s|$)" Explanation : -P stands for PCRE, -o for taking only the matched portion of the line (^|\s)+(Version: ) will match Version at the start or one or more whitespaces, the \K will ...


1

What you have done wrong is you're trying to parse XML with a regular expression. This is a bad idea - it sometimes works, but it makes brittle code. XML has a specification that allows for linefeeds, whitespace and tag nesting - all things that regex handle very badly. The real answer is 'use a parser'. I would suggest perl and XML::Twig as options - ...


1

You have to backslash the slashes that don't work as separators, too, or use a different separator: sed "7 s%\<<Filevalue=.*/>\>%<Filevalue="true"/>%" Also, you can't include double quotes in double quotes without backslashing them, or switch to single quotes. Moreover, a word can't start with a <, so \<< never matches.


0

you can use something like grep -w Version: /home/test/public_html/wp-content/plugins/plugin_name |grep -v -- -Version|awk '{print $NF}'


1

There might be a better solution than this but you can use the below command. sed /hostname.*$/s//"hostname=int1"/g /home/path/of/your/original/file > /tmp/hello.$$ cat /tmp/hello.$$ > /home/path/of/your/original/file Hope it helps.


3

sed uses regular expressions. These are different from patterns ("globs") that the shell uses. Notice that the following doesn't work: $ echo hostname=abc | sed "s/\<hostname=*\>/hostname=int1/" hostname=int1=abc But, the following does: $ echo hostname=abc | sed "s/\<hostname=.*\>/hostname=int1/" hostname=int1 You need a . before the *. ...


0

Ok i have figured out how to achieve this by doing the following: location / { if ($arg_test ~ "true") { set $gotoserver 1; } if ($uri ~* "(queries|results)") { set $gotoserver "${gotoserver}1" ; } if ($gotoserver = 11) { proxy_pass http://myserver.org ; } }


0

Give file with text, try using pr and process substitutions syntax as below: $ pr -mt <(grep -i "^[a-z]" file.txt) <(grep -i "^[0-9]" file.txt) AAAA 1234 BBBB 5678 CCCC 9012 DDDD 3456 EEEE 7890 You can adjust width by -w9 or remove spaces by sed "s/ //g".


0

An optimization I would add to cuonglm's suggestions, if only to make the regex easier to maintain: m/^(?:[-+]?\d+\.\d+\s+){2}/ This way, you can concentrate any further optimizations to the regex in one place. Also, if you know that the numbers are proper decimal numbers (i.e. your data doesn't contain more than one decimal dot in a string of digits): ...


2

Base on your output, I offer some points to improve your regex: You can match explicit at beginning of line, using ^ If you don't use the match groups, then you don't need parentheses or using non-capturing group ?: \s includes \t, so you only need \s. The final regex can be m/^(?:[+-]?\d+\.\d+)\s+(?:[+-]?\d+\.\d+)/


1

it matches lines that begin with two optionally signed numbers having decimal parts, with said numbers separted by ascii whitespace. it seems optimal and clear as it stands.


2

sed '/match_string1/{ :1 N /\n.*match_string2/s/\n/; / t1 P D }' When script met line with match_string1 it add next line to pattern and check if in that added line there is match_string2 if so they substitute newline sign by ; and add next line to check. If there is not match_string2 (so substitution ...


1

When you have an and of 2 constraints it is easier to work one at the time. Example: grep '^....t$' | grep '\.' or perl -nlE '/^....t/ and /\./ and say '


-1

I have written a programme to solve this fromto You can use it like this: cat file | fromto -f R -T S Where R is the regex you want to grab from, and S is the regex you want to grab to. It will print out all lines between those two. -f means "include the 'from' line", -F would mean "don't include the 'from' line". Likewise with -t/-T. The question was ...


2

with perl: # perl -ne 'print if ( /R/ .. /S/ ); last if /S/' It does print the S-Matching regexp, tho'


4

With sed: sed -n '/R/,$!d; /S/q; p' Example: $ seq 20 | sed -n '/6/,$!d; /1/q; p' 6 7 8 9


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 ...


0

In POSIX system, you can use expr: $ a=a $ expr "$a" - 0 >/dev/null 2>&1 $ [ "$?" -lt 2 ] && echo Integer || echo Not Integer


0

You can use the -eq test on the string, with itself: $ dash -c 'a="a"; if [ "$a" -eq "$a" ] ; then echo number; else echo not a number; fi' dash: 1: [: Illegal number: a not a number $ dash -c 'a="0xa"; if [ "$a" -eq "$a" ] ; then echo number; else echo not a number; fi' dash: 1: [: Illegal number: 0xa not a number $ dash -c 'a="-1"; if [ "$a" -eq "$a" ] ; ...


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



Top 50 recent answers are included