Remove colour code special characters from bash file - bash

I have a bash script that runs and outputs to a text file however the colour codes it uses are also included what i'd like to know is how to remove them from the file, ie
^[[38;1;32mHello^[[39m
^[[38;1;31mUser^[[39m
so I just want to be left with Hello and User, so something like sed -r "special characters" from file A save to file B

sed 's/\^\[\[[^m]*m//g'
remove (all) part of line starting with ^[[ until first m

Some like this:
awk '{sub(/\^\[\[38;1;[0-9][0-9]m/,x);sub(/\^\[\[39m/,x)}1'
Hello
User

Related

how to extract all prefix words from an ispell .mwl file in bash

I have an ispell huge .mwl file and I want to remove all the ispell suffixes to generate a simple text-only words dictionnary
using unix ispell, bash or perl commands.
Is there ispell command options to do that?
(in unix, the .mwl.gz files are located in the /usr/share/ispell/ directory)
a short extract non exhaustive of the file:
a/MRSY
A'asia
a'body
a'thing
aaa
AAAS
Aaberg/M
Aachen/M
Aaedon/M
AAeE
AAeE's
aaerially
aaerialness
Aaerope/M
AAgr/M
aah/DGS
aal/MS
Aalborg
Aalesund
aalii/MS
Aaliyah/M
Aalst/M
Aalto
aam
Aandahl/M
Aani/M
Aaqbiye/M
Aar/MN
Aara/M
Aarau
aardvark/MS
aardwolf/M
aardwolves
Aaren/M
Aargau
aargh
Aarhus
Aarika/M
aarogramme
I'm not sure what you mean by suffix but I'll assume it's the part following the / or ' in your sample text. You can do this with a simple pipeline from Bash.
cat something.mwl | perl -pe 's{[/\x27].*$}{}; ' > stripped_something.txt
The -p switch means to run perl in a pipeline. Whatever you pipe in will be put into $_ one line at a time, worked on, and then printed out. Notice I put \x27 for the apostrophe in the regex. Escaping it in the command line is a big pain. If there are any other characters that start a suffix you can put them in the character class.
You can do any other work on the line before printing it out this way too.
See the perlrun documentation for more about the -p switch.

Substitution of substring doesn't work in bash (tried sed, ${a/b/c/})

Before to write, of course I read many other similar cases. Example I used #!/bin/bash instead of #!/bin/sh
I have a very simple script that reads lines from a template file and wants to replace some keywords with real data. Example the string <NAME> will be replaced with a real name. In the example I want to replace it with the word Giuseppe. I tried 2 solutions but they don't work.
#!/bin/bash
#read the template and change variable information
while read LINE
do
sed 'LINE/<NAME>/Giuseppe' #error: sed: -e expression #1, char 2: extra characters after command
${LINE/<NAME>/Giuseppe} #error: WORD(*) command not found
done < template_mail.txt
(*) WORD is the first word found in the line
I am sorry if the question is too basic, but I cannot see the error and the error message is not helping.
EDIT1:
The input file should not be changed, i want to use it for every mail. Every time i read it, i will change with a different name according to the receiver.
EDIT2:
Thanks your answers i am closer to the solution. My example was a simplified case, but i want to change also other data. I want to do multiple substitutions to the same string, but BASH allows me only to make one substitution. In all programming languages i used, i was able to substitute from a string, but BASH makes this very difficult for me. The following lines don't work:
CUSTOM_MAIL=$(sed 's/<NAME>/Giuseppe/' template_mail.txt) # from file it's ok
CUSTOM_MAIL=$(sed 's/<VALUE>/30/' CUSTOM_MAIL) # from variable doesn't work
I want to modify CUSTOM_MAIL a few times in order to include a few real informations.
CUSTOM_MAIL=$(sed 's/<VALUE1>/value1/' template_mail.txt)
${CUSTOM_MAIL/'<VALUE2>'/'value2'}
${CUSTOM_MAIL/'<VALUE3>'/'value3'}
${CUSTOM_MAIL/'<VALUE4>'/'value4'}
What's the way?
No need to do the loop manually. sed command itself runs the expression on each line of provided file:
sed 's/<NAME>/Giuseppe/' template_mail.txt > output_file.txt
You might need g modifier if there are more appearances of the <NAME> string on one line: s/<NAME>/Giuseppe/g

sed Command adding space at start of line

I am trying to use the sed program to replace two lines of text in a config file using bash variables, the line replacement works however there is a extra space at the start of the line.
My commands are as follows:
replacement="computer_id = $server_ref"
(where $server_ref is a user entered variable)
and then:
sed "/computer_id/c \ ${replacement}" -i slapos.cfg
The other line being replaced uses just the same commands just changes a different variable in the config file.
The ouput of this change looks like
computer_id = something
when it should be
computer_id = something
which results in crashing the program using the config as it is not excepting that space.
Might be better to do it like this:
sed -i "s/^\\(computer_id = \\).*/\\1${server_ref}/" slapos.cfg
The program which reads the configuration file isn't very robust if extra whitespace crashed it.

cat a file inside a shell function

I am wondering how I can implement something like the following:
test(){
cat>file<<'EOF'
abc
EOF
}
Many thanks.
Qiang:
Remove the spaces in front of EOF (so it's on a line by itself and not indented).
From bash(1):
If the redirection operator is <<-, then all leading tab
characters are stripped from input lines and the line
containing delimiter. This allows here-documents within
shell scripts to be indented in a natural fashion.
It says tab, and in my testing, tab works, but spaces do not:
#!/bin/bash
cat>file <<-END
hello
world
hello
END
echo done
(All those indents are tabs; the funny thing about the four-leading-spaces markup for code means only four spaces show up in the rendered text, too.)
Your code should work just fine, is there anything specific you are looking for?
#!/bin/sh
input() {
cat > file <<EOF
input
line
another line
EOF
}
input
EDIT: Changed function input to input()

bash templating

i have a template, with a var LINK
and a data file, links.txt, with one url per line
how in bash i can substitute LINK with the content of links.txt?
if i do
#!/bin/bash
LINKS=$(cat links.txt)
sed "s/LINKS/$LINK/g" template.xml
two problem:
$LINKS has the content of links.txt without newline
sed: 1: "s/LINKS/http://test ...": bad flag in substitute command: '/'
sed is not escaping the // in the links.txt file
thanks
Use some better language instead. I'd write a solution for bash + awk... but that's simply too much effort to go into. (See http://www.gnu.org/manual/gawk/gawk.html#Getline_002fVariable_002fFile if you really want to do that)
Just use any language where you don't have to mix control and content text. For example in python:
#!/usr/bin/env python
links = open('links.txt').read()
template = open('template.xml').read()
print template.replace('LINKS', links)
Watch out if you're trying to force sed solution with some other separator - you'll get into the same problems unless you find something disallowed in urls (but are you verifying that?) If you don't, you already have another problem - links can contain < and > and break your xml.
You can do this using ed:
ed template.xml <<EOF
/LINKS/d
.r links.txt
w output.txt
EOF
The first command will go to the line
containing LINKS and delete it.
The second line will insert the
contents of links.txt on the current
line.
The third command will write the file
to output.txt (if you omit output.txt
the edits will be saved to
template.xml).
Try running sed twice. On the first run, replace / with \/. The second run will be the same as what you currently have.
The character following the 's' in the sed command ends up the separator, so you'll want to use a character that is not present in the value of $LINK. For example, you could try a comma:
sed "s,LINKS,${LINK}\n,g" template.xml
Note that I also added a \n to add an additional newline.
Another option is to escape the forward slashes in $LINK, possibly using sed. If you don't have guarantees about the characters in $LINK, this may be safer.

Resources