I have .bashrc file and in the file this line appears:
PS1='${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u#\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]\$ '
And I'm trying to replace it with this:
PS1='\[\e[32m\]\A\[\e[m\] \[\e[31m\]\u\[\e[m\]#\[\e[36m\]\h\[\e[m\]\[\e[32m\]:\[\e[m\]\[\e[32m\]\w\[\e[m\]\\$ '
I tried to do this with:
sed "s#PS1='\$\{debian_chroot:\+\(\$debian_chroot\)\}\\\[\\033\[01;32m\\\]\\u#\\h\\\[\\033\[00m\\\]:\\\[\\033\[01;34m\\\]\\w\\\[\\033\[00m\\\]\\\$ '#PS1='\\\[\\e[32m\\\]\\A\\\[\\e\[m\\\] \\\[\\e\[31m\\\]\\u\\\[\\e\[m\\\]#\\\[\\e\[36m\\\]\\h\\\[\\e\[m\\\]\\\[\\e\[32m\\\]:\\\[\\e\[m\\\]\\\[\\e\[32m\\\]\\w\\\[\\e\[m\\\]\\\\\$ '#g" .bashrc
But I got error saying:
sed: -e expression #1, char 267: Invalid content \{\}
I'd use sed or any other bash/dash scripting way so I can make me a customization script for systems that I regularly use.
Thank you for help.
What I would do instead of weird ANSI codes :
PURPLE=$(tput setaf 5)
RED=$(tput setaf 1)
WHITE=$(tput setaf 7)
GREEN=$(tput setaf 2)
YELLOW=$(tput setaf 3)
CYAN=$(tput setaf 4)
LIGHT_CYAN=$(tput setaf 6)
STOP=$(tput sgr0)
PS1="\[$PURPLE\]\u\[$WHITE\]#\[$GREEN\]\h\[$WHITE\]:\[$GREEN\]\w\[$WHITE\] $ \[$STOP\]"
Finally to make a full reply to all the aspect of the question, :
sed -i '/^PS1=/d' ~/.bashrc # remove PS1 line in bashrc
# now feeding bashrc with goodies :
cat<<'EOF'>>~/.bashrc
PURPLE=$(tput setaf 5)
RED=$(tput setaf 1)
WHITE=$(tput setaf 7)
GREEN=$(tput setaf 2)
YELLOW=$(tput setaf 3)
CYAN=$(tput setaf 4)
LIGHT_CYAN=$(tput setaf 6)
STOP=$(tput sgr0)
PS1="\[$PURPLE\]\u\[$WHITE\]#\[$GREEN\]\h\[$WHITE\]:\[$GREEN\]\w\[$WHITE\] $ \[$STOP\]"
EOF
This might work for you (GNU sed):
sed 's|PS1='\''${debian_chroot:+($debian_chroot)}\\\[\\033\[01;32m\\\]\\u#\\h\\\[\\033\[00m\\\]:\\\[\\033\[01;34m\\\]\\w\\\[\\033\[00m\\\]\\$ '\''|PS1='^''\\[\\e[32m\\]\\A\[\\e[m\\] \\[\\e[31m\\]\\u\\[\\e[m\\]#\\[\\e[36m\\]\\h\\[\\e[m\\]\\[\\e[32m\\]:\\[\\e[m\\]\\[\\e[32m\\]\\w\\[\\e[m\\]\\\\$ '\''|' file
Replace ' by '\'' and \ by \\ in both the pattern and replacement and [] by \[\] in the pattern only.
Related
I am trying to find aws arn from logs and change its color
regex I tried
echo 'arn:aws:lambda:us-east-2:421142534505:function:list-users"' | sed -e "s/arn:\S*[^\s\"]/$(tput bold setaf 1)&$(tput setaf 9)/gi"
result I got
I want arn:aws:lambda:us-east-2:421142534505:function:list-users to be colored (any whitespace or " should not be colored)
Using sed
$ sed "s/arn:aws[^\" ]*/$(tput setaf 1)&$(tput sgr 0)/g" input_file
I have a text file that has lines of different lengths. I need to make these uniform so that the PLSQL Developers text import function reads them correctly. Lines that are 89 characters long need to be padded with 4 spaces on the end. For some reason the -i argument to sed isn't accepted either.
The file can be found here
I have tried a number of different regex commands found from various sources through Google but none of them are working, either because the 'function cannot be parsed' or it doesn't add the spaces needed.
The code that I wrote that worked using Notepad++ was
Find: (^.{89})($)
Replace: \1 \2
I've tried a number of unix sed commands such as
sed -e "s/(^.{89})($)/\1 \2/" file.txt
sed -e "s/(^.{89})($)/\1\s\s\s\s\2/" file.txt
sed -e "s/(^.{89})($)/\1\ \ \ \ \2/" file.txt
sed -e "s/\(^.\{89\}\)\($\)/\1\ \ \ \ \2" file.txt
sed -e 's/\(^.\{89\}\)\($\)/\1[[:space:]]\2/g' file.txt
sed -e 's/\(^.\{89\}\)\($\)/\1[[:space:]]\{4\}\2/g' file.txt
sed -e 's/(^.{89})($)/\1[[:space:]]{4}\2/g' file.txt
The main issue here is that you are using BRE POSIX syntax and unescaped ( / ) are treated as literal parentheses and unescaped {/} are treated as literal braces.
This should work:
sed 's/^.\{89\}$/& /' file.txt > newfile.txt
Here:
^ - matches the start of a line
.\{89\} - matches any 89 chars
$ - asserts the end of line position.
The & in the replacement refers to the whole match.
If you need to use -i option, see sed edit file in place.
A software tools kludge for HP-UX, based on its manuals:
sed 's/.*/ /' file | paste -d ' ' file - | cut -c 1-93
How it works:
Use sed to create a dummy stream of the same number of lines as file, but with four spaces on each line.
Use paste to append the dummy stream to what's in file. (Because of the -d ' ' it adds five spaces, but it doesn't much matter.)
Use cut to chop off anything over 93 bytes.
If HP-UX sed is even less like GNU sed than I've supposed, it could be replaced with some equivalent like:
yes ' ' | head -n $(wc -l < file) | paste -d ' ' file - | cut -c 1-93
Try this:
awk '{ <br>
diff = 89 - length($0); <br>
for(i=1;i<=diff; i++) <br>
{ <br>
$0 = $0 "a" <br>
} <br>
print $0 <br>
} <br>
' FileName.txt
I tried:
sed "s/#/\%/g"
but the batch file stripped out the %, and sed gave an error
sed "s/#/\x37/g"
didn't work either, it just put the text x37 in there
Note I need this to work in a batch file, not the command line.
%% does the trick:
echo "foo ## bar ##" | sed 's/#/%%/g'
This question already has answers here:
How to give a pattern for new line in grep?
(6 answers)
Closed 7 years ago.
How to find more than one newline in code before } with regular expression and add warning to it? This is what I tried add bash script in xcode:
TAGS2="\}/\n"
echo "searching ${SRCROOT} for ${TAGS2}"
find "${SRCROOT}" \( -name "*.h" -or -name "*.m" \) -print0 | xargs -0
egrep --with-filename --line-number --only-matching "($TAGS2).*\$" |
perl -p -e "s/($TAGS2)/ warning\$1/"
If you mean that you would like to find places in your file where there is a blank line before a line whose first non-blank character is a }, the following awk program might help.
awk '/\S/ { blanks = NR - prev - 1; prev = NR }
blanks && /^\s*}/ { printf "%d: WARNING: Useless blank line\n", NR - 1 }'
That will work with Gnu awk; with more Posix-compatible awks you'll need to use /[^ \t]/ and /^[ \t]*}/ (or something similar) to instead of the given patterns.
egrep is a wonderful tool but it does line-by-line matching. It cannot recognize multiline patterns, nor can you change the line delimiter.
egrep does not work with multiple lines.
You can use sed (or awk as mentioned in previous answer) instead of "egrep". No need to use perl. See the script below.
TAGS2="}"
echo "searching ${SRCROOT} for ${TAGS2}"
find "${SRCROOT}" \( -name "*.h" -or -name "*.m" \) \
-print -exec sed "/$TAGS2/{ # search for tag
=; # print line number
N; # join with next line
s/$TAGS2/warning/; # substitute tags found with warning
}" {} \;
How can you write a single sed command that will remove lines that contain any of several regular expressions?
For example I want sed to remove "/./. ::", ":: ", "::foo", and "^^bar" from a document.
As of now, when I run
sed -ir '/ "//.//. ::|:: _|::foo_|^^bar" /d' text.file
the response is "unknown command '/'".
This is the case with or without the inner "s around the regex:
sed -ir '/ //.//. ::|:: _|::foo_|^^bar /d' text.file
Also if I remove the escape(/) before the '/' such as:
sed -ir '/ "/./. ::|:: _|::foo_|^^bar" /d' text.file
the return becomes "unknown command '.'"
You should be using the escape symbol: \
And there's no need to quote the pattern match or add underscores, simply try:
sed -i -r '/\/.\/. ::|:: |::foo|\^\^bar/d' file.txt
Also, you may want to consider escaping the . symbols. The dot would otherwise match any character.
HTH
sed '{
s/::foo_//
s/\^\^bar//
s!/./. ::!!
s/:: _//
}' input_file
To totally delete lines containing patterns:
sed '{
/::foo_/d
/\^\^bar/d
\!/./. ::!d
/:: _/d
}' input_file