Tag Info

Hot answers tagged

27

You can use another character instead of slash / as delimiter to substitution command. Example using #: :%s#/a/b/f/g/d/g#/s/g/w/d/g/r#


7

Perhaps: sed 's/\(\(^\|:\)123@example\.com:\)\([^:]\+\)/\1foo/' given there is no escaped delimiters in the value. sed 's/\(\(^\|:\)123@example\.com:\)\([^:]\+\)/\1foo/' | | | | | | | | | | | | | | | +----- H. End of sub. | | | | | | ...


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.


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


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


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


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*\(.*\)'`


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


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


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


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


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.


2

Maybe you can use sed Example 1 – sed @ delimiter: Substitute /opt/omni/lbin to /opt/tools/bin When you substitute a path name which has /, you can use @ as a delimiter instead of /. In the sed example below, in the last line of the input file, /opt/omni/lbin was changed to /opt/tools/bin. $ sed 's@/opt/omni/lbin@/opt/tools/bin@g' path.txt ...


2

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


2

To escape slashes, you can use any character other than a forward slash to separate regular expressions. E.g. (foo1/ -> foo2/): sed "s@foo1/@foo2/@" sed "s|foo1/|foo2/|" To escape brackets (this work also for slashes) you've to put a backslash character before the delimiting character causes the character to be treated literally. E.g. (foo[] = -> ;foo[] ...


2

If you are using bash 4 or later, you can take advantage of the command_not_found_handle hook, since a bare IP address is extremely unlikely to be a valid command name. Add this to your .bashrc file: command_not_found_handle () { if [[ $1 =~ [0-9][0-9][0-9]\.[0-9][0-9][0-9]\.[0-9][0-9][0-9]\.[0-9][0-9][0-9] ]]; then ssh user@$1 else ...


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_~' ...


2

There is no pattern matching in aliases. So if you want to login to 192.168.0.2 without typing ssh user@ before that you have to specify: alias 10.32.44.225="ssh user@10.32.44.225" for each and every IP address you are likely to use. Of course you can write a program to write those lines out for the range that you need. Or do something like: for i in ...


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


2

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


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'


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


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.


2

If someone was looking for awk solution ;) awk -F':' '/^123@example.com/ {$2="NEWPASSWORDHERE"}1' OFS=':' infile 123@example.com:NEWPASSWORDHERE::: abc@example.com:{SHA512-CRYPT}$6$0vthg.LubtSCxRRK$MdTKNQ2Vk8ZW3XQXNXStt9rfr6fNa‌​XqPvZ0o9WJ8mW8y9ozE1pi8dYM8oQzwWa8ESGzEmJO6yT/tgi3ZEqAiE0::: I used awk with its -F option to setting colon(:) as field ...


2

You can just alternate addresses here - sed '/^123@example.com:{SHA512-CRYPT}/s/{[^:]*/REPLACE/ Here the s///ubstitution command is a function of the /regxp/ address and so the s///ubstitution is not even attempted unless the line first matches its parent address. Or just w/ a single s///: sed 's/^\(123@example.com:\){SHA512-CRYPT}[^:]*/\1REPLACE/ Or ...


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.



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