In-place edits with sed on OS X

You can use the -i flag correctly by providing it with a suffix to add to the backed-up file. Extending your example: sed -i.bu ‘s/oldword/newword/’ file1.txt Will give you two files: one with the name file1.txt that contains the substitution, and one with the name file1.txt.bu that has the original content. Mildly dangerous If you … Read more

sed fails with “unknown option to `s'” error [closed]

The problem is with slashes: your variable contains them and the final command will be something like sed “s/string/path/to/something/g”, containing way too many slashes. Since sed can take any char as delimiter (without having to declare the new delimiter), you can try using another one that doesn’t appear in your replacement string: replacement=”/my/path” sed –expression … Read more

RE error: illegal byte sequence on Mac OS X

A sample command that exhibits the symptom: sed ‘s/./@/’ <<<$’\xfc’ fails, because byte 0xfc is not a valid UTF-8 char. Note that, by contrast, GNU sed (Linux, but also installable on macOS) simply passes the invalid byte through, without reporting an error. Using the formerly accepted answer is an option if you don’t mind losing … Read more

Command to get nth line of STDOUT

Using sed, just for variety: ls -l | sed -n 2p Using this alternative, which looks more efficient since it stops reading the input when the required line is printed, may generate a SIGPIPE in the feeding process, which may in turn generate an unwanted error message: ls -l | sed -n -e ‘2{p;q}’ I’ve … Read more

Change multiple files

I’m surprised nobody has mentioned the -exec argument to find, which is intended for this type of use-case, although it will start a process for each matching file name: find . -type f -name ‘xa*’ -exec sed -i ‘s/asd/dsg/g’ {} \; Alternatively, one could use xargs, which will invoke fewer processes: find . -type f … Read more

Retrieve last 100 lines logs

You can use tail command as follows: tail -100 <log file> > newLogfile Now last 100 lines will be present in newLogfile EDIT: More recent versions of tail as mentioned by twalberg use command: tail -n 100 <log file> > newLogfile

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)