New answers tagged

1

POSIX (remove echo from echo rmdir to actually erase the files) : for dir in [0-9][0-9][0-9][0-9]_[0-9][0-9][0-9][0-9]/; do a="$(expr "$dir" : '\(.*\)_\1/')" ${a:+false} || echo rmdir "$dir" done Or: for d in [0-9][0-9][0-9][0-9]_[0-9][0-9][0-9][0-9]/; do expr "$d" : '\(.*\)_\1/' >/dev/null && echo rmdir "$d" done Or: set -- ...


1

Using perl perl -e 'opendir my($dh),".";rmdir for grep { -d && /^(\d+)_\1$/} readdir $dh' If the directory is not empty perl -e 'use File::Path; opendir my($dh),"."; for $i(grep{ -d && /^(\d*)_\1/} readdir $dh){rmtree $i}'


0

Try this: find -type d -regextype posix-extended -regex '\./([0-9]{4})_\1' -delete regex type setting is needed for the {4}.


6

Non-recursive With ksh93 (on OS/X available as ksh): rmdir {4}(\d)_\1 (beware it could delete a directory called {4}(\d)_\1 if there's no file matching that pattern). With zsh (on OS/X available as zsh): setopt extendedglob rmdir [0-9](#c4)_[0-9]##(/e:'[[ ${REPLY%_*} = ${REPLY#*_} ]]':) (that one also has the benefit of only considering files of type ...


0

So far I've already solved the merging problem sudo nethogs wlan0 | sed -r "s/\x1B\[([0-9]{1,3}((;[0-9]{1,3})*)?)?[m|K]//g" Now I need to merge a time stamp


0

To monitor wlan0 sudo nethogs wlan0 You can monitor network bandwidth of both eth0 and eth1 interfaces nethogs [option] eth0 eth1 options : -d delay for refresh rate. For example, to set 5 seconds as your refresh rate, then type the command as. sudo nethogs -d 5 -h display available commands usage. -p sniff in promiscious mode (not ...


1

An awk solution: awk '$0!~/.*[[:alpha:]][[:digit:]]+$/ && $0!~/^[[:digit:]]+[[:alpha:]]+/' words.txt 789 hello he11o 88888


1

To actually edit the source file and create a new file with the discards is a bit trickier. I would do this $ cat file 789 hello 1hello 112121hello3323 he11o hello9 88888 $ perl -i -lne 'if (/^\d+\D|\D\d+$/) {warn "$_\n"} else {print}' file 2>file_nums $ cat file 789 hello he11o 88888 $ cat file_nums 1hello 112121hello3323 hello9 The matched lines ...


2

GNU grep grep -vP '^\d+\D|\D\d+$' produces 789 hello he11o 88888


0

An alternative solution in AWK: awk -v FS="," '/^[0-9]/{line=$0;getline; line=line" "$1", "$2 ;print line}' file


2

You can use sed: sed -ne '/^[0-9][0-9]*\.[0-6]/ { N; s/\n/ /; s/^\([^,]*,[^,]*\),.*$/\1/; p; }' < data This processes a file called data, suppressing printing unless asked for (-n) and executing the sed program in quotes. That program selects lines starting with one or more digits, a ., and a digit 0-6, and then runs the part in {} for those lines. ...


2

Here is an example -- .* means 0 or more of any character. In the 2nd case it matches 0 of any character followed by one of the non excluded characters. ls 1 a =a ++a a.0 a_1 B b0 find . . ./B ./=a ./b0 ./a_1 ./a ./1 ./a.0 ./++a find . -regex '.*[^-_./0-9a-zA-Z].*' ./=a ./++a


2

.* is followed by [^-_./0-9a-zA-Z] - and it is not optional. So something other than one of those characters (the alphabets, digits, -, _, . and /) should appear once in the filename. Any path which consists solely of these characters will not be printed. Therefore these commands need not output the same results.


0

I created a perl script to do exactly this, you can input a regex to sort a file by the first capture. then you can set a flag to do either string or numerical comparison. just toss this code sample into a .pl file. it's pretty simple and the logic really just sits on lines 20-37. #! /usr/bin/perl # Created by pete Nixon use Getopt::Long; use strict; use ...


1

why do you use the Pipe? :%s/.*/\<a href=\"&\"\>&\<\/a\>/g (mark all command strings with \ )


4

:%s:.*:<a href="&">&</a>: Same as in ed/sed/perl... Another less ex and more vim-like way would be: if you know how to do it once for a line, record it as a macro and then run :%normal @m where m is that macro. Like (in normal mode): qmS<a href="<Ctrl-R>""><Ctrl-R>"</a><Esc>q to record the macro.


0

Rather than applying grep to the output of ls which is not post-processable reliably anyway, I'd rather filter on the list of files passes to ls, like: ls -lFhd --color -- .*(/) In zsh. Or as your own approach suggests you only want the directories last modified between 6 months ago and now and not the ones with special permissions: ls -lFhd --color -- ...


6

Parsing ls is often a bad idea. Often, but not always. Here's another suggestion for you, which collects the required directories together before passing the set to ls. find .* -maxdepth 0 -type d \( -name '.[^.]' -o -name '.??*' \) -exec ls -ld --color=always {} + It's been pointed out that the original code actually limits the list of directories to ...


0

Ok I finally figured out how to list all hidden dirs while preserving the Colors and not including dirs like "hello.world": ls -lhAF1 --color | grep -E "^d[rwx-]{9}.*[0-9]{2}:[0-9]{2} "$'\x1b'"\[([0-9]{1,2}m?)(;[0-9]{1,2}m?)?\."


2

The reason is that ls always colorizes its output even if it is connected to a terminal. From man ls: --color[=WHEN] colorize the output. WHEN defaults to 'always' or can be 'never' or 'auto'. More info below Many other tools such as grep do not retain colors when standard output is terminal but for some reasons ls was ...


6

--color adds escape sequences for the color. You can see this if you redirect the output (of ls --color) to a file. This is what it looks like: drwxr-xr-x 6 root root 4.0K Jan 9 08:23 ^[[01;34m.cabal^[[0m/ To account for this, try this instead: ls -lhAF1 --color | grep -E '^d.*[0-9]{2}:[0-9]{2} .*\.'


0

If only one integer will change (as in your example) you could do this: echo -e "1\n2\n" |xargs -n1 -i -P0 scp file.txt my-remote-vm-1:/tmp/conf-{}-ver-2


1

Run scp once, then copy it locally on the remote server. $ scp file.txt my-remote-vm-1:/tmp $ ssh my-remote-vm-1 'for i in /tmp/conf-[0-9]-ver-[0-9]; do cp /tmp/file.txt "$i"; done' $ ssh my-remote-vm-1 rm /tmp/file.txt


1

Postfix provides a way to do this without resolving to manual filtering: sudo postconf mail_name=SomeRandomMTA From the postfix docs: mail_name (default: Postfix) The mail system name that is displayed in Received: headers, in the SMTP greeting banner, and in bounced mail.


0

Hiding this is at best a trivial obstacle for a would-be cracker, and can be a stumbling block for some downstream recipient trying to troubleshoot mail problems. Don't do this.


1

Perl can read the whole file with -0777, the /s modifier makes . match newlines, too: perl -0777 -pe 's/<!--\\.*?on //gs' *? is a "frugal asterisk", which means "repeat zero or more times, but match the shortest string possible".


2

POSIXly: $ sed -e '/<!--/{ $!N s/.*on // }' <in >out


2

The following sed command should do what you want: sed '/^<!--/{N; s/.*on *//}' inputfile First we search for the regex <!-- at the beginning of the line, than we use the N command to append the next line to it and delete (substitute with nothing, actually) everything till and with "on". There are people claiming that whenever you use a capital ...


-1

With the util-linux rename, no, this one only does basic string replacement. With the Perl-based rename, yes, see choroba's answer. With zsh's zmv: autoload -U zmv # put this in your ~/.zshrc zmv '(*)_(*).(*)' '${2}_$1.$3' zmv -w '*_*.*' '${2}_$1.$3' The two zmv invocations above are equivalent. To act in subdirectories as well, you can use zmv -w ...


3

If you have the rename implementation with Perl regexes (as on Debian/Ubuntu/…, or prename on Arch Linux), you need $1 instead of \1. Also, no backslashes on capturing parentheses: rename 's/(.*)_(.*)/$2_$1/' *_* If not, you have to implement it yourself. #! /bin/bash for file in *_* ; do left=${file%_*} right=${file##*_} mv "$file" ...


0

The easiest way to drop in a shell variable value to sed and not worry about how your backslash-escaping will need to change for the rest of your sed script, is to stuff everything into single quotes except the variable, and put that in double quotes. All of the following code examples assume: VALUE='foo \([a-z]\+\)' The following broken code fails because ...


5

Collation elements are usually referenced in the context of sorting. In many languages, collation (sorting like in a dictionary) is not only done per-character. For instance, in Czech, ch doesn't sort between cg and ci like it would in English, but is considered as a whole for sorting. It is a collating element (we can't refer to a character here, character ...


0

This is usefull when non-english (non-ascii) characters are in use. The example ch you mention is a digraph, i.e. some languages have a letter in their alphabet that is/can be represented by two letters in an English alphabet. When you use [.ch.] in a regexp, you basically say: "I expect a non-English input sequence with the digraph ch. I want my regexp to ...


3

Check your single quotes. Single quotes don't magically nest. alias sll 'ls -l \!* | grep -oE '\''[^ ]+$'\'' | xargs ls -ld --' That's still flawed for several reasons: Because of [^ ], that won't work for file or link target names that contain spaces. as you're treating that list as a list of lines, that won't work with file/link target names that ...


3

You could use grep with -A. Something like: $ grep -A 13 '^\[2\]' inputfile.txt The -A specifies the number of lines you want to include after the match. But I think it would be better to use sed in this case: $ sed -n '/^\[2\]/,/^$/p' inputfile.txt This will print everything between [2] and an empty line. The same using awk: $ awk -v RS='' -v ...


-1

/\.(jpe?g|png|gif|bmp)$/i; Use it.


0

With any POSIX sed: $ sed -e'/hello/{' -e:1 -e'$!N;s/hello/world/2;t2' -eb1 -e\} -e:2 -en\;b2 <file hello world hello hello hello hello After the first match /hello/, we run into a loop. Inside loop :1, we read each Next line to the pattern space, doing substitute command for 2nd occurrence only. We test if the substitution success or not. If yes, we ...


0

I'm surprised no one suggested the very simple solution of: sed -i.bak -E 's/\\\t/ /' file.txt That should do the trick. You need to escape the escape (hence the 3 \s) to allow sed to understand that you are trying to use a \t character in the regular expression when everything is substituted...


1

I'm assuming your string variable is one long line without newlines. I get a sed error complaining about an unknown option to the s command. That's because your string contains slashes, which is the delimiter for the s command. Using bash parameter substitution to esacape the slashes in the string works. $ cat file this is a _PLACEHOLDER_ here $ ...


1

Pass the variable doublequoted as an argument to Perl, it can handle special characters in variables in the replacement: perl -i~ -pe 'BEGIN { $replace = shift } s/_PLACEHOLDER_/$replace/g ' "$text" "$file"



Top 50 recent answers are included