New answers tagged

1

You could simplify the whole thing if you use the // format for gsub: $ echo "ABC(T)" | awk '{gsub(/ABC\(T\)/,"ABC/G")}; print $0' ABC/G Then, you could simplify further by using print with no arguments (which is the same as print $0) or the 1 shorthand for printing (the default awk action for expressions that evaluate to true, such as 1; is to print the ...


1

You were very close. You just needed two \. That is because one of set is for the text itself passing through BASH, and the other would be the actual \ which is being parsed by awk. This seems to work for me: echo 'ABC(T)' | awk ' {gsub("ABC\\(T\\)","ABC/G")}; Print $0' "$FILENAME" And gives: ABC/G


5

Because when you use just *net* (without any quoting or escaping), it will be expanded by the shell as the (existing) net file/directory in the current directory before the find command run. So the command becomes: find . -name net As you can see it is just matching net, so usbnet.ko will not be matched. Also note that, without quoting and escaping, if ...


1

One way to do it is to use the end match marker: %s/\n\ze\D/\t/ Another way to do it is to using negative lookahead: %s/\n\d\@!/\t/ They are not exactly equivalent, the second will also replace the last newline in a file.


3

You're almost there, but your regular expression is replacing both the newline and the non-digit with a tab. How about replacing every newline followed by a non-digit with a tab and that same non-digit? %s/\n\(\D\)/\t\1/g The escaping is a bit messy, but basically you have a grouping around the \D that will capture whatever the non-digit is. This is then ...


-2

I hope this will help you :%s#\n.\D#\t#g


1

The file(1) manpage only tells you how to run the command. For a description of the magic patterns, see magic(5). However, the section on regex isn't especially detailed. A wide range of examples of its use can be found in the pattern files that come with it: https://github.com/file/file/tree/master/magic/Magdir Your main problem was that the caret ...


2

One solution was due to @JigglyNaga - escape the caret. Snippet below is now part of my .magic file. 0 string Project\040Units: >2 regex \^Id, PhotoModeler 3D export table 0 string Project\040Units: >2 regex \^Object\040Point\040ID, PhotoModeler 2D export table


0

There are two (three) erase commands: one that's part of the util-linux package that's installed on every non-embedded Linux system, and one (two variants actually) based on Perl. See What's with all the renames? The util-linux command is very basic, but you're in the rare situation where it can do what you want. Replace the first space by space-dash-...


1

perl -l0 -ne 'print for /\\subimport\{\}\{(.*?)\}/g' file.tex Would print the filenames inside those \subimport{}{...} functions NUL-delimited. You can pipe that to xargs -0 grep -l gastric -- to find which of those files contain gastric.


0

In the end, I use the search in directory by the script here with a redirection to Vim. It would be great to get something like that work in Geany IDE directly.


1

$ rename 's/^(\d\d)\s*/$1 - /' *.mp3 This will rename all MP3 files that has a double digit at the start of their file names, inserting space-dash-space after the digits. So 01 Track name.mp3 will become 01 - Track name.mp3 Judging from your own attempts, all filenames start with the digit zero, and you appear to want to insert a dash directly after the ...


2

Assuming the perl rename command: You're quite close with the last command. rename 's/(0.) /$1 - /' *.mp3 would work. There's no need to escape the space, they have no special meaning in regular expressions (they do in file names, but that doesn't matter here), and you need parentheses around the part you want to reuse.


1

Does it have to use the rename command? $ ls 01 Track name.mp3 02 Track name.mp3 03 Track name.mp3 $ for a in *.mp3 > do > mv -i "$a" "${a%% *} - ${a#* }" > done $ ls 01 - Track name.mp3 02 - Track name.mp3 03 - Track name.mp3


1

That's the Perl rename, I suppose. Perhaps something like this would work: rename 's/^(\d+) ([^-])/$1 - $2/' [0-9]*.mp3 Match anything starting with numbers, then a space, then something other than a dash. Replace with the numbers, a dash, and the next character. (The rest of the name is not touched.) Explicitly checking for the dash here so repeated ...


0

The regular expression match /^(.*) - (.*) - (.*) part([0-9]+)/ stuffs the name, episode indicator, title and part number in groups which you can then use as $1 through $4 in the replacement text. rename 's/^(.*) - (.*) - (.*) part([0-9]+)/$1 - $2.$4 - $3/' * Anything after the part1 part, such as a file extension, is left unchanged. If you want to ...


3

Since you're not actually changing the "cast" line: sed '/cast \$recv \$UE_CAPABILITY_ENQUIRY/{a\ set trans_id 1 n;d}' file As Kusalananda comments, this command: when one of the wanted "cast" lines is found: append the new line take the next line from the file (the unwanted "set" line) and delete it In hindsight, this does not ...


0

sed has an unbuffered mode, so the following should work as well: … | sed -u -r '/%[^\n]*\n/d'


1

Edited as the user needs: cat document.tex | cut -d'{' -f3 | cut -d'}' -f1 | while read file grep -i 'gastric' "$file" &>/dev/null && echo "$file contains gastric" done


3

grep is defined to match, and retain or discard, lines, so it has to read the whole line before doing the match; that can't accomplish what you want. First you need to verify if command does this echoing of input chars one by one to a pipe. Standard C programs (and sometimes other programs using C stdio) by default use line-buffering when stdout is an '...


3

With sed: sed 's/.*\\\([^,]*,\)/&\1/' file Output: F1309-042543,07/14/09 01:39:25,N/A,C:\windows\system32\netsh.exe,netsh.exe,N/A,True F1309-042543,11/21/10 03:24:02,N/A,C:\Windows\System32\networkexplorer.dll,networkexplorer.dll,N/A,False F1309-042543,07/14/09 01:38:53,N/A,C:\windows\system32\scrnsave.scr,scrnsave.scr,N/A,True F1309-042543,11/21/10 ...


0

It is not 100% clearly for me because your expected results are looking identical to the file contents, but let me give a try. If you want only the file name in this list: cat thebigfile.csv | rev | cut -d'\' -f1 | rev | cut -d',' -f1 The result will be: netsh.exe networkexplorer.dll scrnsave.scr WISPTIS.EXE SafeNet High Assurance Client (x64) 2.12....


0

Start of line is done with ^ and end of line with $. The characters \n is not recognized. And end of line is never matched (by default). So You should use something like grep -v "^%.*$".


0

A possible solution using sed: sed -n '/[A-Z]/{/[a-z]/{/[0-9]/{/[#?!@$%^&*-]/p}}}'


1

The error you got due to escaping slash /, which make / is not delimiter for address pattern anymore. Now, even you don't escape /, then your regex won't work. sed only support BRE, and ERE in some implementation (and will become standard in next POSIX version). BRE or ERE don't support lookahead feature, which you used in your regex (?=...). To archive ...


1

Don't use sed for this; use an inverse grep. That way you can use the PCRE-style expressions you're trying to shoehorn into sed (which doesn't speak PCRE): grep -v -P '^(?=.*pattern1)(?=.*pattern2)'


1

use bash: $ txt="32.2 MB" $ echo ${txt// /} 32.2MB


3

You ain't quoting any of your sed expressions, that is the main culprit. put quotes around it like sed ' '. Or Simply you can get that by following tr expression, tr -d '[:space:]' <<< "312.2 MB" 312.2MB tr -d ' ' <<< "123.34 KB" 123.34KB tr -d '[:blank:]' <<< "487.1 GB" 487.1GB If you are insisting on sed, you can do that by, ...


5

The problem is quoting. Because you don't quote your sed command, the parenthesis \(...\) was interpreted by the shell before passing to sed. So sed treated them as literal parenthesis instead of escaped parenthesis, no back-reference affected. You need: echo "312.2 MB" | sed 's/\([0-9]\)[[:space:]]\([GMK]\)/\1\2/g' to make back-reference affected, and ...


2

Depending on your use case (e.g.testing multiple values), you might find the "case" syntax a better fit. case $(uname -r) in 4.4*) echo 4.4 based kernel found ;; 3.10*) echo 3.10 based kernel found ;; *) echo unknown kernel found esac


1

Another way: case $(uname -r) in (4.4.*) echo yes;; esac


1

use [[ : ver=$(cut -d. -f1-2 <(uname -r)) [[ $ver =~ 3.2 ]] && echo "y" OR use [: ver=$(cut -d. -f1-2 <(uname -r)) [ $ver == 3.2 ] && echo "y"


3

Bash operator [ may not be what you are looking for; however, [[ does support =~. Try this: [[ $(uname -r) =~ ^4\.4 ]] && echo yes


2

That is because case doesn't use regex's but bash Pathname Expansion. You can learn more from bash man page or from Bash Reference Manual.


0

Once you have a file containing times in the form 1m59.973s (eg. the output from Mikael Kjær's answer), you can transform them into a single sum for bc(1): ( cat alltimes.log | sed 's_h_*3600+_;s_m_*60+_;s_s_+_' | tr -d '\n' ; echo 0 ) | bc That prints a number of seconds (including decimal places). Then bash (whose arithmetic expansion doesn't handle ...


1

I haven't used AdaBrowse, but sed '/-- Description:/,/-- Inputs:/!d;/-- Inputs:/d' works with your example, so you could give it a try.


1

If you only want to find directories matching a given pattern/prefix, I think you could just use find: find /target/directory -type d -name "prefix*" or, if you only want immediate subdirectories: find /target/directory -maxdepth 1 -type d -name "prefix*" Of course, there's also -regexif you need an actual regex match. (caveat: I can't remember if -...


1

The name of your variable regex will not be well chosen but consider setting the value to "$1" like regex="$1". Next step is to change the if statement from: if [ -d "$themeDirectory/$regex" && -d "$iconDirectory/$regex" ]; then to if [ -d "$themeDirectory/$regex" ] && [ -d "$iconDirectory/$regex" ]; then The script will become: ...


2

-d doesn't accept regexes, it accepts filenames. If you just want to check a simple prefix, a wildcard is enough: exists=0 shopt -s nullglob for file in "$themeDirectory"/word* "$iconDirectory"/* ; do if [[ -d $file ]] ; then exists=1 break fi done if ((exists)) ; then echo Directory exists. else echo "Directories don't exist....


2

In an attempt to give this Q a proper answer, based - on - the - comments (heeding Sobrique's note that parsing XML should really be done with an XML parser): perl -CSD -lne 'print for /\w{63,}/g' input-file-here


0

There's no need to pipe grep or sed into awk, or awk into awk here. awk can do it all, in one pass: nmcli con show eno16777984 | awk -F"/|[[:space:]]+" ' /^ipv4.address/ { if ($2 ~ /^10\.10/) { sub(/10\.10/,"10.20",$2); } else if ($2 ~ /^10\.20/) { sub(/10\.20/,"10.10",$2); }; }; 1' On lines beginning ...


4

Just add t;. sed -e 's/^10\.10\./10.20./;t;s/^10\.20\./10\.10\./' It branches to the end on success. But you should really merge all these grep, awk, sed into a single awk.


1

To exchange two strings, use a temporary 3rd string that won't be changed back the other way. sed -e 's/^10\.10\./10.foo./g; s/^10\.20\./10.10./g; s/^10\.foo\./10.20./g'


2

If you want to add text at the beginning of the line, you have to match all the characters from the beginning of the line: regsub {^.*pch_mac} $file_pointer {*&} file_pointer Here, & in the replacement part is substituted with all the text that matched the expression, i.e. all the chars from the beginning of the line to "pch_mac". See http://tcl....


1

The AlaDelta theme does not appear to be in any standard Debian package. If you installed it from a non-standard package, you could find out the package name with: grep -il aladelta /var/lib/dpkg/info/*.list Alternatively, if that results in an error message about "argument list too long" or similar: find /var/lib/dpkg/info -name '*.list' -exec grep -il ...


0


1

I'd do this with emacs: $x replace-regexp \s *=\s * = where \s *=\s * is the regexp that identifies any white space around an equals sign, and = is what it should be replace with. Generally, \sC is interpreted as the character class C, in this case, white space (could be tabs also). If you just want spaces, use * (that's a space before the *). You seem ...


3

Since neither don_crissti nor Julie Pelletier have converted the perfectly good comment into an answer, here's what Don came up with: sed 's/[[:blank:]]*=[[:blank:]]*/=/g' ... which uses regular expressions and a character class in sed to achieve the goal. The inner [:blank:] captures spaces or tabs; the outer []* says to capture zero or more of those ...



Top 50 recent answers are included