Tag Info

New answers tagged

0

Sounds like you want something like: awk 'BEGIN {RS="\n\n"} /What exactly is Free/' br.txt This should print the entire paragraphs where the text is found (RS="\n\n").


1

To remove all words from a line that begin with a number with sed you can do: sed 'x;s/.*//;G s/[[:space:]][[:punct:]]\{0,1\}[0-9][^[:space:]]*//g s/\n//' ...or, if you wanted only words which do not begin with numbers printed each on a separate line: sed 'y/!\t "'"'?/\n\n\n\n\n\n/;/^[_[:alpha:]]/P;D" ...the above should do fairly well. ...


2

It depends how do you define a string (e.g. if you count punctuation characters to string or not). Nevertheless you may start from something like grep -Po '\b[^[:digit:]].*?\b' file


5

grep -v '^[0-9]' Will output all the lines that do not (-v) match lines beginning ^ with a number [0-9] For example $ cat test string string123 123string 1string2 $ grep -v '^[0-9]' test string string123 or if you want to remove all the words that begin with a digit sed 's/[[:<:]][[:digit:]][[:alnum:]_]*[[:>:]]//g' or with shortcuts and ...


3

It means that grep should search for ABC string only at the beginning of the line OR after space, moreover this string has to end with another space OR the end of the line. In other words someone wanted to search for a strings which form whole words. However this regexp has many issues, namely there could be many other characters before and after word (at ...


4

From (GNU) grep(1) man page: -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 the end of the line or followed by a non-word constituent ...


0

Try this: find /path/to/the/directory -type f -exec sed -i 's/include\(.*\)/require_once\1/' {} + This will find all the files in the given directory and replace the lines "include("path/to/file.php");" of each file to "require_once("path/to/file.php");".


0

Simply a normal grep which supports Perl-regexp parameter P will do this job. $ echo 'abc blah blah blah def blah blah blah' | grep -oPz '(?s)abc.*?def' abc blah blah blah def (?s) called DOTALL modifier which makes dot in your regex to match not only the characters but also the line breaks.


0

The grep alternative sift supports multiline matching (disclaimer: I am the author). Suppose testfile contains: <book> <title>Lorem Ipsum</title> <description>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua</description> </book> sift ...


1

It turned out that the Horde messages have changed a bit so the regular expressions in the horde3 file don't match. Here are the correct regular expressions for Horde Webmail 5.2.5: ^\w{3} [ :0-9]{11} [._[:alnum:]-]+ HORDE: \[imp\] Login success for [@._[:alnum:]-]+ \([.0-9]{7,15}\) to \{[:\/.[:alnum:]-]+\} \[pid [0-9$ ^\w{3} [ :0-9]{11} [._[:alnum:]-]+ ...


2

I strongly recommend the use of either of the programs named html2text (1) (2) instead. Parsing HTML is much harder than it looks.


6

If you are not insisting on sed, the best thing to do this would be lynx. lynx --dump <filename>.html This will output the content of the html file in the format the html code was intending to display. The only condition is that the filename should have a .html or .htm extension.


2

As long as your HTML tags are confined to a single line, the following will work: sed 's/<[^>]*>//g'


3

This should work: grep 'ing$' soi catis not needed. Try with single quotes. Single quotes prevent the shell from interpreting $ as the beginning of a variable name.


1

using grep echo -e "aaa bbb ccc ddd\n hello world"|egrep '([a-z])\1{2}' ([a-z]) remembers the first letter found. \1{2} check to see if the first letter found is repeated two more times.


1

First about sed -i: (for a better explanation of the implications of sed -i please see the excellent forum posts here. Thanks again, Don) If you're not sure how sed -i works, it's probably first worth understanding the security implications that go with it. When using that switch it is possible that a user granted certain permissions for a directory but ...


-2

sed -i "s/\s$word\s/$replace/g" "test.txt" sed also support metacharacter \s example var=world echo "hello world"|sed -r "s/\s$var(\s|$)/.../g" results hello... Note is it is necessary to put (\s|$) pattern since a line can end with a word followed by an end of line character instead of a space


3

printf 'aabbbccddd\nabcdef' | grep '\([a-z]\)\1\1' Output: aabbbccddd The bracket pair \(\) makes a backreference, which is referenced by \1


14

By default, grep uses Basic Regular Expressions, you need to escape the braces to make grep match multiple characters: grep 't\{2\}' textfile Alternatively, you can use the -E option (or -P option for GNU grep, which uses Perl Compatible Regular Expressions) making grep use Extended Regular Expressions, which can use braces without escaping them: grep -E ...


7

You need to use the -E (--extended-regexp) or -P (--perl-regexp) option (with GNU grep). According to the GNU grep(1) man page about Basic vs Extended Regular Expressions, i.e. if you choose not to use these options: In basic regular expressions the meta-characters ?, +, {, |, (, and ) lose their special meaning; instead use the backslashed versions \?, ...


7

You need to escape the braces: grep 't\{2\}' textfile Otherwise { and } are treated as literal characters.


4

You need to write your regular expression in a way that only matches whole words. With GNU sed, you can use \b which matches at word boundaries: sed -i "s/\b$word\b/$replace/g" If you know there will always be a space there, you could just add a space: sed -i "s/ $word /$replace/g" Now, there are also some issues with your script. Your if ... break ...


1

Please be noted that expression try to choice the pattern of maximum length (gready regex) match. As you see in your example (regex: symbols between parenthesis) have choiced ...is ( test.com) and (alex ) instead of ...is (test.com) and (alex). There are two ways to override such behavior: Substitute any symbol by revers match of limit or devide ...


2

The difference is exactly that you wrote. The characters in the set may occur once to multiple times with + and may also not occur at all with * .


0

Regular languages are closed under complementation, so for every regular expression, there exists a regular expression that matches exactly the inputs that the original regexp doesn't match. However, in the worst case, the smallest regexp that matches the complement language has a length that is exponential in the length of the original regexp. So while the ...


1

sed '/timetosa/d' <test ...will do it. Alternatively: sed -n '/timetosa/!p' <test Still, though (whether it's allowed or not): grep -v timetosa <test ...is going to be the most performant solution of the three - and probably by a significant margin. Thanks to @Sparhawk, I found my way to the zaproxy documentation. Based on this: URL ...


1

sed -e 's/\(match\)\([_[:alnum:]]*\)\(\(.*\)\n\)*/\1\ /2;tc' -e b -e :c -e 's//\1\2\4\2/' The above sequence will always handle only the first and second occurrence of match on a line - regardless of how many there may be on a line. It works by doing the the first s///ubstitution on the s///2cd occurrence of the pattern, then, if the substitution tests ...


1

:%s/\v(match\$(\w+).*match\$)xxx/\1\2/ \v very magical (we can use less \\)


0

You can filter the characters you don't want with tr; in your case ... | tr -d -c '[:print:][:cntrl:]' -d deletes any character matching the specified characters, -c complements the character sets (so it keeps only what matches in this case), [:print:] matches all printable characters including space, and [:cntrl:] matches control characters (such as ...


0

sed is not the right tool for this. $ perl -pe 's|"([\d,]+)"(?=[^"]*$)|$1=~y/,//dr|eg' file 0,1,,,10815197, 6,7,010202,,5589, 6,7,010202,,589, Through Python. #!/usr/bin/python3 import sys import re file = sys.argv[1] with open(file, 'r') as f: for line in f: print(re.sub(r'"([\d,]+)"(?=[^"]*$)', lambda m: m.group(1).replace(',', ''), line), ...


2

Awk will be the best for your scenario. $ awk -F'"' '{gsub(",", "", $2);print}' file.txt 0,1,,, 10815197 , 6,7,010202,, 5589 , 6,7,010202,,589, How it works -F'"' - causes AWK to use double quotes ( " ) as record separator. gsub(",","",$2) - gsub function will search and replace all occurrence of double quotes with empty string. print ...


1

I think it's easier with awk. You can try something like this: $ awk -v v='"' 'BEGIN{FS=OFS=v}{gsub(",","",$2);gsub("\"","",$0);print }' file.txt 0,1,,,10815197, 6,7,010202,,5589, 6,7,010202,,589, Basically you are telling awk that use a regular expression -v v='"' to use it as field separator. With FS=OFS=v you say that the field separator is the same ...


2

:%s/match\$\zs\(\w\+\)\(.*match\$\)xxx/\1\2\1/ Explanation match\$\zs: anchor the match at the first match$; I use \zs to start the match after that, to avoid yet another capturing group \(\w\+\): capture the text after the first occurrence of match$ \(.*match\$\): capture what's after that, up to the second occurrence of match$, and capture that as we ...


1

Try this: sed -e 's/\(match\$\)\([a-zA-Z_]\+\)\([a-zA-Z ]\+match\$\)[a-zA-Z]\+/\1\2\3\2/' < input.txt > output.txt Using an input.txt of: text match$something_here and match$xxx blablabla text match$something_else_here and match$xxx blablabla I get an output.txt of: text match$something_here and match$something_here blablabla text ...


1

Try this: sed 's/\(^X[[:digit:]]\{4\}\)[[:digit:]]*\(Y[[:digit:]]\{4\}\)[[:digit:]]*/\1\2/' If line starts with X (^X) then all digits which are not inside parenthesis \(\) are deleted.


3

A key point in muru's answer is that to get a backslash into the FS regex you need to write double backslash \\. That's because backslash is used as an escape character at two different levels. A single backslash in a string will be treated as escaping the following character, so we need to escape the backslash itself so that we get a single backslash in ...


4

If FS is longer than a single character, it is treated as a regular expression. An FS of just * is seen as a fixed string, but an FS of -*- is a regular expression, and -*- is equivalent to -+ (one or more -). So you need to make * be considered as a regular character. -\*- and -[*]- can both do this. However, the string for FS is parsed twice - once when ...


1

Within the " delimiter, you need to escape the backslash one mre time. $ echo 'a -*- b' | awk 'BEGIN {FS="-\\*-"} {print $2}' b Since we are passing a regex to the FS variable, \\ within the double quotes is parsed as single backslash and then it apply the resultant regex against the input string.


0

As you already informed by glenn jackman in comments: sed reads it's input one line at a time, so unless you're doing some programming to accumulate the lines, there won't be any newlines to match You are able to push GNU sed to treat all file as single line with -z option -z, --null-data separate lines by NUL characters So you can ...


0

If you want to replace just a word (not a full sting) you have to use some trick: sed "s/100/$(cat -E file2 |tr -d '\n')/;s/\\$/\n/g" file1 or sed -e s/100/$(tr \\n $ < file2)/ -e s/\\$/\\n/g file1 The main idea that sed is a string editor so it can't accept multiline string (that is mean with newline symbols) in most case. The trick is to strip the ...


0

If all you want to do is replace file1 with file2's contents, then any of the following will work: cat file2 > file1 cp file2 file1


1

Try to pipe otput via sed 's/\S\+ \(= \|in (\)/\1/g'


0


0

You can do multiple commands in a row using |, as a command delimiter. %s/cat/dog/g|%s/car/truck/g


2

The SwapStrings.vim plugin allows to do this elegantly: :%SwapStrings car truck :%SwapStrings cat dog More alternatives can be found here.


1

You can use the \= special replacement which allows to run vim code: For instance, to swap foo and bar: %s/foo\|bar/\=submatch(0) == "foo" ? "bar" : "foo"/g Or for your example: %s/ca[rt]/\=submatch(0) == "car" ? "truck" : "dog"/g See: :h sub-replace-expression for details.


0

:%s/foo/bar/gc This command changes foo with bar! :%s is a substitute for vim


3

sed '/\n/P;//!s/_\.[^ ("]*Text([^)]*)/\n&\n/;D' files... >results.txt ...would probably work. Run on your example data it prints: _.Text("Hello World!") _.Text("Foo") _.ActionText("Bar") All it does is attempt to enclose the first match on a line in \newlines. Whether or not it succeeds it Deletes up to the first \newline in pattern space - which ...


1

The [:foo:] bracket expression is meant to be used inside a [...] collection, like this: :g/[[:digit:]]/s/1/2/g This allows you to specify multiple (alternative; otherwise it would be concatenation) groups, and mix with other characters, e.g.: [[:space:][:digit:]abc].


2

I think your question is: I have a bash variable which contains a string; I would like to split it into two pieces, before and after the string "/ ". If so, here is a simple bash-solution: track="Judy Garland/ Somewhere Over the Rainbow" title="${track#*/ }" artist="${track%%/ *}" The # syntax means "Without the shortest prefix which matches ...". The ...



Top 50 recent answers are included