Tag Info

Hot answers tagged

5

The escaped ( has special meaning in sed: it used for back-references. To match a literal (, simply use it without the backslash: /VALUES ([0-9]/d! If you're attempting to match \(, then escape the \ instead: \\( Escaping the (space) makes no difference.


4

If your input doesn't contain <, > nor + characters, you could do: sed ' s/[[:alnum:]]\{1,\}/<&>/g;:1 s/\(<\([^>]*\)>.*\)<\2>/\1+\2+/;t1 s/[<>]//g' If it may, you can always escape them: sed ' s/:/::/g;s/</:{/g;s/>/:}/g s/[[:alnum:]]\{1,\}/<&>/g;:1 ...


3

By default, sed use Basic Regular Expression (BRE). In BRE, \( and \) are used for defining subexpression: A subexpression can be defined within a BRE by enclosing it between the character pairs "(" and ")". Such a subexpression shall match whatever it would have matched without the "(" and ")", except that anchoring within subexpressions is ...


3

You have to sort using names first. Note: 'uniq' does not detect repeated lines unless they are adjacent. You may want to sort the input first, or use `sort -u' without `uniq'. You can use the -t/-k options, to sort these fields: sort -t',' -k 3 marathon that sort regarding the 3rd field with the comma as separator. Then you can print ...


3

You can't test a file with a regex nor a glob like this. You have to iterate over the files : for file in /Applications/xml[0-9].pl; do if [ -f "$file" ]; then ...


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.


3

> sed '/^AUTH/{s/^AUTH: //;s/\b\([[:alpha:]]\)\([[:alpha:]]*\)\b/\u\1\L\2/g;s/^/AUTH: /;}' file TITLE: Average title AUTH: Superman AFF: Something AUTH: The New One AFF: Berlin AUTH: Mars-Mensch AFF: Planet Mars


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

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

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

If you've got a list of decimal integer numbers as sequences of 1 or more decimal digits, the first one not being 0 except for the number 0 itself, with no +/- sign, one per line, then you could use: grep -xE '3[7-9]|[4-9][0-9]|1[0-9]{2}|200'


2

If you don't insist on using sed then you could consider using dirname: S="dir/subdir/othersubdir/file.txt" P=$(dirname $S) echo $P dir/subdir/othersubdir S="dir/file.txt" P=$(dirname $S) echo $P dir


2

\> is the (zero length) regexp for the end of the word so C\> will probably not match last names that start with a 'C'. Maybe you should try \<C instead. [[:alpha:]] matches exactly one character so this is also very unlikely to match a real name. You should append a multiplier like * or + (only in ERE?).


2

Just use one sed expression (needs GNU sed): sed -r -i -e '/(SEM|AFF|CON)/s/([:,]\s*)the\s+/\1/ig' * The search pattern at the begin of the sed command restricts the replacement to the lines which begins with the selected categories. The i flag for the replace command (s//) makes the pattern case-insensitive, the g flag allows more than on replacement in ...


2

sed 's/^.*/s\/&\/\//' pattern > sed-pattern-file sed -f sed-pattern-file myinitialfile.txt > mycleanfile.txt


2

I would use the following: sed -r '/(SEM|AFF|CON)/ s/([:,] *)[Tt]he */\1/g' file Add -i option to change file in place.


2

Gnu Awk only started including interval expressions (your {4} qualifying the [0-9]) in 4.0: Interval expressions were not traditionally available in awk. They were added as part of the POSIX standard to make awk and egrep consistent with each other. Initially, because old programs may use ‘{’ and ‘}’ in regexp constants, gawk did not match interval ...


2

Read the man page of unzip. It doesn't talk about regular expressions, just about the two special characters * and ?.


2

sed -e '/^AUTH:\([^[:alpha:]]*\)/!b' -e 'h;s//\1/;x;s/// s/\([[:alpha:]]\)\([[:alpha:]]*[^[:alpha:]]*\)/\1/g;x;s//\ \2/g y/ABCDEFGHIJKLMNOPQRSTUVWXYZ/abcdefghijklmnopqrstuvwxyz/;G;:l s/\n\(.*\n\)\(.\)/\2\1/;tl s/\(.*\)\n/AUTH:\1/ '<<\IN TITLE: Average title AUTH: SUPERMAN ...


1

Try this way: egrep -rl "^(SEM|CON|AFF)\: (t|T)he" * | xargs sed -r -i 's/(^(SEM|CON|AFF):\s)((t|T)he[ ]*)/\1/g'


1

Another simple, alternative(for someone who dont want/know awk) script will be: #!/bin/bash sort -t',' -k 3 marathon | cut -d',' -f 3 | uniq -d if someone wants to print whole line instead of just names: #!/bin/bash sort -t',' -k 3 marathon | cut -d',' -f 3 | uniq -d | grep -f - marathon in over scripts: sort takes third field to sort, using , as ...


1

Here is sed command: $ P="dir/subdir/othersubdir/file.txt" $ sed -r 's/^(.*)\/.*\.txt$/\1/' <<< $P dir/subdir/othersubdir In above sed command we capture anything.* from beginning^ of variable P that ends$ with/*.txt, which it's known as a captured group with\1 as its beck-reference because used a pair of parentheses around it(.*), then in ...


1

ed is 'the standard text editor'. It's not really ideal to use for day-to-day editing, but it is readily scriptable. ed file.c <<'EOF' 1,/\*\//d i /* Copy right text bla bla bla * some license text bla bla bla * All rights reserved xyz xyz */ . w q EOF The first command deletes from the first line, up to and including the first line containing a ...


1

To negate regular expressions is not easy. You could use negative lookbehinds: $ grep -C4 -P '(?<!call).*fn1' test.txt 5-even more main code 6-call fn2 7-still more main code 8- 9:function fn1 10-call fn3 11-fn1 code 12-more fn1 code This grep uses Perl-style regular expressions (-P) to look for any instance of fun not preceded by call. And you can ...


1

Here is another approach : this uses a few seds: an='[:alnum:]' esc=$(printf '\033\[') sed "/[${an}]/!d;=;a\ } s/.*/ & /;s/[^${an}]\{1,\}/ /g s| \([${an}"']\{1,\}\) | \ s/\\([^+'"${an}"']\\)\\(\1\\)\\([^+'"${an}"']\\)/\\1+\\2+\\3/2|g ' <text | sed '/^ /!N;s/\n */{/' | sed -e 's/.*/ & /;s/+/ & /g' \ -f - \ -e "s/ //;s/ ...


1

bash expands the regular expression before evaluating the condition. If your directory has three files by the name /Applications/xml0.pl /Applications/xml1.pl /Applications/xml2.pl your if statement looks to shell as if [ -f /Applications/xml0.pl /Applications/xml1.pl /Applications/xml2.pl ]] which is syntactically incorrect. You will need to specify ...


1

%% does glob matching, not regex. That means ${foo%% } will remove the longest trailing string matching a single space character, which of course is just a single space character, and ${foo%% *} will remove the longest trailing string starting with a space character. You'll probably be better off using awk to split the string into fields.


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



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