Tag Info

New answers tagged

1

With recent versions of GNU sed: sed -E '/^AUTH:/!b;s/([^\w:])(\w+)/\1\L\u\2/g' With older versions: sed -r '/^AUTH:/!b;s/([^[:alnum:]:])([[:alnum:]]+)/\1\L\u\2/g'


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


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

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


1

This line strikes me as having two problems: grep "$2" | awk '/[0-9]{4},[0-9]{4}.*/' dcmResults.txt >> ~/export/"$1"/tagResults.txt You can do the whole thing in grep: grep -e "$2" -e '([0-9]\{4\},[0-9]\{4\})' dcmResults.txt >> ~/export/"$1"/tagResults.txt Where is grep getting its input, and where is the output of grep going to? Since awk ...


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


1

As a variation on the answer already here - it is easier still, probably, to pull the script directly out of a pipe: sed 's|.*|s/&//g|' <patterns | sef -f - infile >outfile That way you needn't alter the patterns file - or any other - directly and can instead modify it in-stream to suit your needs.


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.


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'


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


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


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


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


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


0

This is exactly the reason. You should not escape the parenthesis in this case. Single quotes ' already tells the shell to not bother about the string contents, so it is passed literally to sed. Escaping the parenthesis is telling sed to expect the ending \) as a delimiter for a sub-regex.


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.


0

If you are using zsh life is much simpler, just use :h modifier: $ var='dir/subdir/othersubdir/file.txt' $ echo "$var:h" dir/subdir/othersubdir


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


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


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


0

A little messy, but this python will work: #!/usr/bin/python import sys filename = sys.argv[1] fo=open(filename,'r+') fr=fo.read() frs=fr.replace('/* --------------------------------------------------\n Copyright 2014 Author name\n\n All rights reserved\n ----------------------------------------------------*/','/* ...


0

for file in "**/*".{c,h}; do perl -p0e 's/\/\*.*?\*\// \ # Your new license here \/* Copy right text bla bla bla * some license text bla bla bla * All rights reserved xyz xyz *\\ /s' $file done Explanation -0 sets the line separator to null -p apply the script given by -e to each line and print that line The regexp modifier: ...


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


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


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


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


0

Following the solution giving by http://stackoverflow.com/questions/369758/how-to-trim-whitespace-from-bash-variable I fixed my script this way: echo "8:X${BASH_REMATCH[1]%"${BASH_REMATCH[1]##*[![:space:]]}"}X" eval "$1='${BASH_REMATCH[1]%"${BASH_REMATCH[1]##*[![:space:]]}"}'" This is a double string manipulation and the first/inner part removes ...


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.


0

Generate these numbers and use them as a pattern list: grep -xf <(printf "%s\n" {37..200}) So, for example printf "%s\n" {1..1000..26} | grep -xf <(printf "%s\n" {37..200}) returns 53 79 105 131 157 183


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'


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


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'



Top 50 recent answers are included