Hot answers tagged

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.


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


5

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


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.


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


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


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


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


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


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

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


3

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


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


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


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.


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


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

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.


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


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.


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


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


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


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.


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


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


1

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



Only top voted, non community-wiki answers of a minimum length are eligible