I am getting MAC address as an input without colons, so looking for alternative to
echo cc0deca96acf | sed -e 's/.\{2\}/&:/g;s/.$//'
in lua
Related
I wrote the following command
echo -en 'uno\ndue\n' | sed -E 's/^.*(uno|$)/\1/'
expecting the following output
uno
This is indeed the case with my GNU Sed 4.8.
However, I've verified that BSD Sed outputs
Why is that the case?
I'd say that BSD's sed is POSIX-compatible only. POSIX specifies support only for basic regular expressions, which have many limitations (e.g., no support for | (alternation) at all, no direct support for + and ?) and different escaping requirements.
BSD sed is default one on MacOS so very first thing on a new system is to get GNU-compatible sed: brew install gsed.
I'm trying to replace a line of text with a single quote using /x27 in my router device to re-configure wifi settings.
The command that I use
sed -i 's/option ssid.*/option ssid \x27test\x27/g' /some/file
The output after I execute above command on my computer(ubuntu 18)
option ssid 'test123'
The output after I execute above command on my router
option ssid x27test123x27
Expected output should be
option ssid 'test123'
So my code is not working properly on the router. How do I achieve this using /x27?
Note: I'm executing this script in a shell script and \x27test\x27 is actually \x27$OPTARG\x27
You can find my full code here
You can double quote Your sed command and then use single quotes inside:
sed -i "s/option ssid.*/option ssid 'test'/g" /some/file
In a comment you said your real code is:
sed -i 's/option ssid.*/option ssid '"'"'"'$OPTARG'"'"'"'/g'
That should be written more correctly and concisely as:
sed -i 's/\(option ssid\).*/\1 '"'$OPTARG'"'/g'
Look:
$ echo 'hi option ssid question' | sed 's/\(option ssid\).*/\1 '"'$RANDOM'"'/'
hi option ssid '5203'
Given your feedback to a different answer it sounds like the version of sed you're running requires a backup file name between the -i and the s/.... Otherwise there's a bug somewhere earlier in your shell script or $OPTARG contains forward slashes or backreferences.
I need to SFTP a file to a server. The password has a dollar sign $ and I need to escape it.
I tried with Perl and sed commands I am able to replace but the string following $ is not getting added.
Example:
echo "Np4$g" | perl -pe 's/$/\\\\\$/g'
output
Np4\\$
It supposed to be Np4\\$g, but g is not getting appended.
Code:
/usr/bin/expect <<EOF
set timeout -1
spawn sftp -C -oPort=$port $sftp_username#$host_name
expect "password:"
send "$password\r"
expect "sftp>"
cd $remote_dir
send "mput *.txt\r"
expect "sftp>"
send
Your command
echo "Np4$g" | perl -pe 's/$/\\\\\$/g'
is failing for two reasons
In "Np4$g", the shell is interpolating the variable g into the double-quoted string. It probably isn't defined so it is replaced with nothing, and you are passing just Np4 to perl. You need to use single quotes to prevent the interpolation
In the Perl substitution s/$/\\\\\$/g the $ in the pattern matches the end of the string, not a literal dollar. That means Np4 is changed to Np4\\$. You need to escape the dollar sign in the pattern to get it to match a literal $
This will work correctly
echo 'Np4$g' | perl -pe 's/\$/\\\$/g'
output
Np4\$g
I suggest to not escape and replace
"Np4$g"
by
'Np4$g'
I'm recording terminal sessions using the script command. Unfortunately the typescript output file contains many control-characters - for example from pressing the full screen command (F11) when in the vim editor or try it below.
script -f -t 2>${LOGNAME}-$(/bin/date +%Y%m%d-%H%M%S).time -a ${LOGNAME}-$(/bin/date +%Y%m%d-%H%M%S).session
vi abc.log
#write something and save
#:x to quit vi
ctrl + d to quit script
The script output hostname-datetime.session contais too many vi control-characters.
I found a perl script in commandlinefu, which can remove these control characters from the typescript.
I am actually doing this replacement in C, and the program runs on a chroot envrioment, where the perl is not avaliable.
Question: Is there a a way to translate the following perl command to sed ?
cat typescript | perl -pe 's/\e([^\[\]]|\[.*?[a-zA-Z]|\].*?\a)//g' | col -b > typescript-processed
if you ONLY want printable ascii :
LC_ALL=C tr -cd ' -~\n\t' < typescript > typescript_printable_ascii_only
why this works? all printable ("normal") ascii are between Space and Tilde
And in addition you need Newline and Tab.
So ' -~\n\t' covers all printable "normal" ascii characters. And tr -d 'chars' deletes all chars, -c takes the opposite of the range given (so everything except 'chars').
=> This LC_ALL=C tr -cd ' -~\n\t' deletes everything except the normal ascii characters (including newline and tab) (I force the locale to be 'C' to be sure we are in the right locale when calling "tr")
This works well for me with GNU sed (or gsed on a Mac):
sed -re 's/\x1b[^m]*m//g' typescript | col -b
I created a sample typescript, and since I'm using a relatively advanced shell prompt, it's full of control characters, and the perl script in the OP doesn't actually work, so rather than converting I had to come up with my own.
Looking at the typescript with hexdump -C, it seems that all control sequences start with \x1b (the Escape character, or ^[), and end with the letter "m". So in sed I use a simple replacement from ^[ until m, normally written as \x1b.*?m but since sed doesn't support the ? symbol to make a pattern non-greedy, I used [^m]*m to emulate non-greedy matching.
I looking to a way to recursively replace all imcompatible windows-1252 caracteres to the respective utf-8 ones.
I tried iconv, without success.
I also found the following command:
grep -rl oldstring . |xargs sed -i -e 's/oldstring/newstring/'
But I'll not like to exec this command by hand for every charactere.
Is there a way or software that can do that?