Tag Info

Hot answers tagged

14

If your input only contains ASCII characters, you could use tr like: tr A-Z a-z < input or (less easy to remember and type IMO, but not limited to ASCII latin letters): tr '[:upper:]' '[:lower:]' < input if you have to use sed: sed 's/.*/\L\1/g' < input (here assuming the GNU implementation).


12

You don't need any external utility, you can do it with the shell's own string manipulation functionality. This makes it easier to avoid breaking on file names with special characters. And remember to always use double quotes around variable substitutions. mv -v -- "$i" "${i%.*.*}.${i##*.}" (Obviously this snippet assumes that the file name does contain ...


7

You can just alternate addresses here - sed '/^123@example.com:{SHA512-CRYPT}/s/{[^:]*/REPLACE/' Here the s///ubstitution command is a function of the /regxp/ address and so the s///ubstitution is not even attempted unless the line first matches its parent address. Or just w/ a single s///: sed 's/^\(123@example.com:\){SHA512-CRYPT}[^:]*/\1REPLACE/' ...


6

sed 's/\([^/]*\)\.phtml$/mydirectory\/\1.php/' <filename> Will do that, if that’s what you need. (optionally with the -i flag to do an in-place replace.) To break it up, first we have s/<regexp>/<replacement>/ Which will replace what <regexp> matches with <replacement>. Next let’s look at the regexp we had: ...


6

I like dd for this, myself. <<\IN LC_ALL=C 2>/dev/null \ dd conv=lcase hi Jigar GANDHI jiga IN ...gets... hi jigar ghandi jiga The LC_ALL=C is to protect any multibytes in input - though any multibyte capitals will not be converted. The same is true for (GNU) tr - both apps are prone to input mangling in any non-C locale. iconv can be ...


6

Since the output of the mv probably will have spaces you need to put double quotes around the result in order not to try and execute commands in the for loop like: mv abc%20def abc def where mv has too many arguments. These are the ones giving you the usage: message. What you should do is: for x in *_MG*.CR2 do mv -- "$x" "$(printf '%s\n' "$x" | sed ...


6

You can chain sed expressions together with ";" %sed -i 's/Some/any/g;s/item/stuff/g' file1 %cat file1 anything 123 stuff1 anything 456 stuff2 anything 768 stuff3 anything 353 stuff4


5

The escaped ( has special meaning in sed: it used for back-references. To match a literal (, simply use it without the backslash: /VALUES ([0-9]/d! If you're attempting to match \(, then escape the \ instead: \\( Escaping the (space) makes no difference.


5

Use rename and replace the %20 with a space in all type of files: $ rename -n 's/%20/ /g' * File%20with%20in00.yA2 renamed as File with in00.yA2 File%20with%20in01.h9H renamed as File with in01.h9H File%20with%20in02.CNR renamed as File with in02.CNR File%20with%20in03.PuP renamed as File with in03.PuP File%20with%20in04.js8 renamed as File with in04.js8 ...


5

Multiple expression using multiple -e options: sed -i.bk -e 's/Some/any/g' -e 's/item/stuff/g' file or you can use just one: sed -i.bk -e 's/Some/any/g;s/item/stuff/g' file You should give an extension for backup file, since when some implementation of sed, like OSX sed does not work with empty extension (You must use sed -i '' to override the original ...


5

Depending on the version of sed on your system you may be able to do sed -i 's/Some/any/; s/item/stuff/' file You don't need the g after the final slash in the s command here, since you're only doing one replacement per line. Alternatively: sed -i -e 's/Some/any/' -e 's/item/stuff/' file The -i option tells sed to edit files in place; if there are ...


5

If I have understood your intention correctly then this does it: sed -e 's/./&\t/g' -e $'s/\t$//' file The second replacement deletes the tab at the end of the line.


5

Here's a solution with ed: ed -s filename <<< $'g/PATTERN/?{?,/}/p\nq\n' that is: g/PATTERN/ # mark each line matching PATTERN ?{?,/}/p # for each marked line, print all lines from the previous { up to the next } q # quit editor This assumes there's only one line with PATTERN between each pair of { } otherwise you ...


5

You can do this with sed — it's Turing-complete. But it isn't the best tool for the job. Sed doesn't have a convenient way of remembering that it's already made a replacement. What you can relatively easily do with sed is to blank all the lines starting /swapfile, and add a new one at the end: sed -i '$! s/^\/swapfile[\t ]//; ...


5

I know this question already has an answer, but I was curious to see if I could come up with a sed-only answer (without using grep as in the accepted answer). As it turns out, it seems possible : sed -ni '/^# deb.*multiverse/{;h;s/^# //;};p;${;g;/^#/!q1;}' /etc/apt/sources.list && apt-get update (Of course, everything must be on the same line, ...


4

If your input doesn't contain <, > nor + characters, you could do: sed ' s/[[:alnum:]]\{1,\}/<&>/g;:1 s/\(<\([^>]*\)>.*\)<\2>/\1+\2+/;t1 s/[<>]//g' If it may, you can always escape them: sed ' s/:/::/g;s/</:{/g;s/>/:}/g s/[[:alnum:]]\{1,\}/<&>/g;:1 ...


4

You can also use Perl 5: perl -pe '$_=lc' temp The option -p tells perl to run the specified expression once for each line of input, printing the result, i.e. the final value of $_. -e indicates that the program will be the next argument, as opposed to a file containing the script. lc converts to lowercase. Without an argument, it will operate on $_. And ...


4

You need to capture the matched pattern and then use it in the replacement with a modifier: sed 's/\([A-Z]\)/\L\1/g' temp The \(...\) "captures" the enclosing matched text, the first capture goes to \1, the next to \2, etc. The numbering is according to opening brackets in case of nested captures. The \L converts the captured pattern to lower case, ...


4

Using vim, it's super simple: $ vim filename gg^guGZZ Opens the file, goes to the first line, first column. Lowers the case of all the characters until the bottom of the file. Saves and exits. It should handle just about anything you throw at it; it'll ignore numbers, it'll handle non ASCII.


4

In your linked question there is already good awk answer, just modify it a little bit by using printf instead of print to insert the content without newline: awk '/First/ { printf $0; getline < "File1.txt" }1' infile.txt Result: Some Text here FirstThis is text to be inserted into the File. Second Some Text here You may want to add space or other ...


4

So it would be a little tricky to make this work portably in sed - you should be looking to cut and/or paste with some regex precursor generating their script in that context - and this is because sed will always insert a \newline before the output of a read. Still, w/ GNU sed: sed '/First/{x;s/.*/cat file/e;H;x;s/\n//}' <<\IN First Second Third IN ...


4

You could use perl (get the file content and substitute pattern with pattern+file content): perl -pe '$text=`cat insert.txt`; chomp($text); s/PAT/$&$text/' file.txt add -i to edit in place; g to append after each PAT (pattern) occurrence, e.g.: perl -i -pe '$text=`cat insert.txt`; chomp($text); s/PAT/$&$text/g' file.txt Another way, using ed: ...


4

You're mixing character classes (a list of characters inside square brackets) with the smb.conf share names which are surrounded by square bracket literals. Also, the echo command is not well-formed: in the case where sed exits with a non-zero status, the shell will attempt to invoke the command Failed. A few suggestions: Remove the character class (outer ...


4

Here are two commands. If you want a command that trims up to the last .*{$ line in a sequence (as @don_crissti does with ed) you can do: sed 'H;/{$/h;/^}/x;/{\n.*PATTERN/!d' ...which works by appending every line to Hold space following a \newline character, overwriting hold space for every line that matches {$, and swapping ing hold and pattern spaces ...


4

Since you are automating this with Puppet, it's better to let Puppet handle the fstab for you. Use the mount resource type. Something like: mount{'swapfile': name => 'none', fstype => 'swap', ensure => mounted, atboot => true, device => '/swapfile', options => 'sw', } should work. If this absolutely has ...


4

Using a bash regex: f=foo.bar.baz.qux if [[ $f =~ (.+)\.[^.]+\.([^.]+)$ ]];then new="${BASH_REMATCH[1]}.${BASH_REMATCH[2]}" if [[ -f "$new" ]]; then echo "moving $f would overwrite $new" else echo mv "$f" "$new" fi fi mv foo.bar.baz.qux foo.bar.qux This has the advantage of doing nothing if there are less than 2 dots in ...


4

If you need to validate a filename match - and so only rename those files which actually have the two extensions you're attempting to modify - you can just do: case ${i##*/} in (*.*.*) mv -- "$i" "${i%.*.*}.${i##*.}" ;;esac Understand that if you do not validate the match and you're setting "$i" with a glob like: for i in * mv "$i" ... ...then using ...


4

Easy rename with mmv command: $ mmv -n '*.*.*' '#1.#3' or with rename command: $ rename -n 's:(.*)\..*(\..*):$1$2:' *


3

By default, sed use Basic Regular Expression (BRE). In BRE, \( and \) are used for defining subexpression: A subexpression can be defined within a BRE by enclosing it between the character pairs "(" and ")". Such a subexpression shall match whatever it would have matched without the "(" and ")", except that anchoring within subexpressions is ...


3

Try this: sed '/CLKA/,/TEST1A/ { s/PortId = \"A/& B/; }' file This sed command appends the B character to the end of pattern PortId = "A which is between two words CLKA and TEST1A. Also you can use start(^) and end($) of line notifies to match/replace only PortId = "A" inside the CLKA { ... } module. ^ CLKA {$ matches the line which only contains ...



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