New answers tagged

0

grep -Eo '[0-9.]{10},[0-9]{2}:[0-9]{2}(:[0-9]{2})?' text.file will produce just time-stamps. By remove -o option you'll receive full lines grep -E '[0-9.]{10},[0-9]{2}:[0-9]{2}(:[0-9]{2})?' text.file If pattern [0-9.]{10} will not produce correct output it can be easy to change for more strong ([0-9]{2}\.){2}[0-9]{4} If you'd like to do both task ...


1

As Stéphane pointed out, this modifier is useful when the pattern space contains more then one line. Here are a few more examples using 'H;1h;$!d;x which accumulates all lines in the hold buffer and then on last line it exchanges buffers so the whole input is in the pattern space. So with this input: printf %s\\n 'onetwo' 'four' 'fivetwo' | sed 'H;1h;$!d;x;...


4

That's the equivalent of the m flag in the perl regexp operators or using (?m) in perl regexps or PCREs (though gsed's M flag would also remove the s perl flag, as without M, sed's . matches newline, while with perl, you need the s flag for . to match newline). They only come into play when the pattern space contain more than one line, like when using -z (...


1

:sor! n /\(^[0-9]\+$\|=.*\)/ r You were very close. You just missed a '\' before '+'.


-1

This works for me root@gateway:/home/sshuser# aws ec2 describe-instances --instance-ids i-2db0459d |grep 'STATE\|TAG' **STATE** 80 stopped **STATE**REASON Client.UserInitiatedShutdown Client.UserInitiatedShutdown: User initiated shutdown **TAGS** Name Magento-Testing root@gateway:/home/sshuser#


3

You can do this by combining both patterns in a single command-line argument: aptitude search '~N !-dbgsym$' This causes the search to look for packages with are both new and whose name doesn't match the -dbgsym$ regex. If the regex is added as a separate argument (aptitude search '~N' '!-dbgsym$'), packages match if they match either pattern.


1

Actually, the regexp ([0-9]+:.{1,}state UP.{1,}(\n.{1,}?){1,}inet )\K([0-9]{1,3}\.?){4} which you first presented, works, with an appropriate environment and Perl: $ perl -l -0 -ne 'print $& if /([0-9]+:.{1,}state UP.{1,}(\n.{1,}?){1,}inet )\K([0-9]{1,3}\.?){4}/' < output_of_your_ip_addr 192.168.1.100 -0 reads the file in full (instead of line-...


2

grep may not be the best option here, though with a PCRE enabled grep and -o one can grab the IP addresses with a zero-width lookbehind match to find inet and then match the IP following that. ip addr | grep -Po '(?<=inet )[^/]+' For the multi-line problem of "get inet addr of devices that are up", something like ip link | awk -F: '/state UP/{print $2}...


3

Solution using Parameter Expansion for i in *.mp3 ; do mv "$i" "${i#${i%%[!0-9 ]*}}" ; done ${i%%[!0-9 ]*} deletes from first non-digit/non-space character till end. Remaining characters are passed to ${i# which then deletes them from beginning of i variable resulting in desired file name without starting digits and space


6

I don't see why that error would occur. In fact, I am reasonably certain there were more lines to the error than you show; for one thing, there's no actual error message. However, that regular expression won't actually match either of your example files. You are using [a-z]+\.mp3 which will only match lower case letters and, since you're matching all the ...


0

Some sed implementations have support for that. ssed has a PCRE mode: ssed -R 's/AB.*?AC/XXX/g' AT&T ast sed has conjunction and negation when using augmented regexps: sed -A 's/AB(.*&(.*AC.*)!)AC/XXX/g' Portably, you can use this technique: replace the end string (here AC) with a single character that doesn't occur in either the beginning or ...


1

No, sed regexes don't have non-greedy matching. You can match all text up to the first occurrence of AC by using “anything not containing AC” followed by AC, which does the same as Perl's .*?AC. The thing is, “anything not containing AC” cannot be expressed easily as a regular expression: there is always a regular expression that recognizes the negation of ...


0

You may sanitize and validate the email address in PHP like this: $sanitized_email = filter_var($email, FILTER_SANITIZE_EMAIL); if (filter_var($sanitized_email, FILTER_VALIDATE_EMAIL)) { echo "This sanitized email address is considered valid.\n"; echo "Before: $email\n"; echo "After: $sanitized_email\n"; } else { echo "This sanitized ...


0

You could do something like sed '/AB.*AC/{s/AB/\ &/;h;s/.*\n/\ /;s/AC/\ &/;s/AB.*\nAC/XXX/;H;x;s/\n.*\n//;}' this should be quite portable though with ancient seds you might want to write it as: sed '/AB.*AC/{ # if line matches this pattern s/AB/\ # add a newline before the first AB &/ h ...


0

In your case you can just negate closing char this way: echo 'ssABteAstACABnnACss' | sed 's/AB[^C]*AC/XXX/'


3

Sed regexes match the longest match. Sed has no equivalent of non-greedy. Obviously what we want to do is match AB, followed by any amount of anything other than AC, followed by AC Unfortunately, sed can’t do #2 — at least not for a multi-character regular expression.  Of course, for a single-character regular expression such as @ (or even [123]), we ...


4

In your program.sed you should have: s/the/zee/g; s/The/Zee/g Or: s/the/zee/g s/The/Zee/g


3

You need to include the tab literally into the regex. You can use the $'' quotes for that: regex=$'^\t+'


10

RH[A-ZA-Z] is a regular expression that includes a single character class that repeats the same set of characters twice. It matches RH followed by any character from A to Z. It places no restrictions on the fourth character, it doesn't even require there to be a fourth character. Obviously, that doesn't do what you want. Try this: RH[A-Z][A-Z] or (with ...


0

In your pattern . match any character, not only literal dot. You need to escape it with backslash to prevent that. Your other solution on the other hand removes all dots from the filenames, i.e. a.b.sh becomes ab. I propose the following alternative: find . -name '*.sh' | sed 's,^./,,g' | sed 's/.sh$//' Notice ^ which matches the beginning of the line and ...


1

I have done it like this and it works find . -name '*.sh' -print | sed 's/[.]//g' | sed 's/[/]//g' | sed 's/sh//' and it works.


1

So, I figured it out. It seems like this does the trick: sed -n '/\^C/,/\^C/{/\^C/!{p}}' input Apparently you can run sed between two matched regexes, so I just put the same regex as beginning and end /\^C/,/\^C/ and then printed the lines, skipping the last line containing that same pattern. I don't know if this is the best method, but it seems to work.


2

something like this should work, assuming source data is in file test.txt, this uses grep to exclude the lines that contain the '^C': sed -n '/\^C/,/\^C/p' test.txt|grep -v '\^C' Output from your example: This is the banner This is the MOTD banner


4

As per the comments, there are various reasons why this may not match, but look "invisible". One common failure mode is the file is in DOS format. In your case, you have hidden whitespace (space or TAB characters) at the end of the line. Commands such as tr ' ' ! < filename or cat -e filename can help expose these characters.


0

With perl. Not particularly readable, awk approach far better. perl -ne 'if(/^Chunk [0-9]+$/&&!exists($seen{$_})){$seen{$_}++;chomp;$a=$_;$_=<>;$_=<>;print "$a $_"}'


2

With GNU awk: gawk -v 'RS=Chunk [0-9]+\n' -v ORS= ' {$0=lastRT $0} NR>1 && !seen[$0]++ {lastRT = RT}'


1

If I understand you correctly, you are looking for something like: $ awk '/server *{/{c=1; print;next} c&&/{/{c++} c&&/}/{c--} c' file server { # php/fastcgi listen 80; server_name domain1.com www.domain1.com; access_log logs/domain1.access.log main; root html; location ~ \.php$ { fastcgi_pass ...


4

I don't think this will be possible using simple tools like cut. Or, at least, not easily. Here's a Perl solution: $ perl -lane '$k=join " ",grep{/hello/}@F; print "$F[1] $k" if $k' file ID23 hello1 ID47 hello2 ID49 hello3 hello4 Which you could simplify by using grep first: $ grep hello file | perl -lane 'print "$F[1] ", join(" ", grep{/hello/}@F)' ...


6

If one space at the end of the line doesn't hurt you much: $ awk '{for(i=1;i<=NF;i++) if(i==2 || $i~"hello") printf $i" ";print ""}' file ID23 hello1 ID47 hello2 ID49 hello3 hello4 ID53 This doesn't assume anything about the position of the "hello" string.


6

The short answer is that you can not use regular expressions to search the shell history. According to POSIX (the standard for Unix-like operating systems), you should be able to search using regular shell pattern matching (as used for filename globbing and with case statements). This feature is referred to as non-incremental search but it currently does not ...



Top 50 recent answers are included