How to remove `^I` character in Linux? - linux

This looks a number of space(20170628 ,) but using cat -A it shows ^I (20170628^I,).
I tried sed -i '/s/^I//g' xxx and sed -i '/s/\^I//g' xxx, but neither works.
How to delete this character ? Any help is appreciated.

That's a tab character, so you can use:
sed -i 's/\t//g/' fileToChange
for this.

Related

unix sed search replace Section sign §

I want to replace a Section sign "§" in CSV files.
So i try some sed commands, but it did not work for me.
i tried:
sed -i 's/§/\;/g' file.csv
sed -i 's/\§/\;/g' file.csv
Is it a SED or Shell issue?
Thanks!

sed not obeying \t or actual tab

I have seen this question a lot and I have tried everything I found and still I cannot get this working.
I am trying to add a new line in my virtualhost file for a script that manages aliases and have the new entry tabbed properly.
I have this line that adds the alias after I check it doesn't exist.
My final attempt: (still no tab.. I actually have 2 tabs in this one)
sed -i "/ServerAlias www.$account/a $newAlias" "$VHOST_FILE"
Here's it again with a single tab
sed -i "/ServerAlias www.$account/a $newAlias" "$VHOST_FILE"
I have also tried: (these all prefix with t instead of using a tab)
sed -i "/ServerAlias www.$account/a \t$newAlias" "$VHOST_FILE"
sed -i "/ServerAlias www.$account/a \\t$newAlias" "$VHOST_FILE"
sed -i "/ServerAlias www.$account/a\t$newAlias" "$VHOST_FILE"
sed -i "/ServerAlias www.$account/a\\t$newAlias" "$VHOST_FILE"
What am I missing here?
Thanks for any assistance
You're close. The problem is that \\ becomes a literal \ in double quotes, so \\t and \t become the same thing.
Escape them to get the result you want:
sed -i "/ServerAlias www.$account/a \\\\t$newAlias" "$VHOST_FILE"
In bash, you can use $'\t' to insert tab.
sed "/ServerAlias www.$account/a"$'\t'"$newAlias" "$VHOST_FILE"
There are many ways to accomplish the same thing. The best approach is to use stuff you can easily understand:
line=$'\t'"$newAlias" # a tab followed by contents of newAlias
sed "/ServerAlias www.$account/a $line" "$VHOST_FILE"
and then combine these (next time) once you've got a good grasp on what is going on.

linux shell sed command

I have file sedFile.txt which has string in format CONNECTION='mysql://user:user#10.79.19.2:3308/SSMS/SUBSCRIBE';
I created one script which has following lines:
fin=CONNECTION='mysql://user:user#10.79.19.2:3308/SSMS/SUBSCRIBE';
repla=connection
sed -i "s/\$fin/$repla/g" /home/sedFile.txt
Even though the script is running, it's not doing changes in my file.
I tried following:
sed -i 's/${fin}/${repla}/g' /home/sedFile.txt
sed -i 's/^$fin/$repla/g' /home/sedFile.txt
sed -i "s/$fin/$repla/g" /home/sedFile.txt
sed -i "s/${fin}/${repla}/g" /home/sedFile.txt
If you want the single quotes to be included in the pattern you have to quote or escape them:
fin="CONNECTION='mysql://user:user#10.79.19.2:3308/SSMS/SUBSCRIBE'"
then, use any of the four lines you tried (i.e. not the one with \$fin).
Update: In order to make sed work, you cannot use / to separate the pattern and the substitution, because this character exists in the string already. Use a different separator:
sed -i "s,$fin,$repla,g" /home/sedFile.txt
Might be the same as the other answers, but I doesn't hurt to try
fin="CONNECTION='mysql://user:user#10.79.19.2:3308/SSMS/SUBSCRIBE';"
repla="connection"
sed -i "s|${fin}|${repla}|g" /home/sedFile.txt
fin="CONNECTION='mysql://user:user#10.79.19.2:3308/SSMS/SUBSCRIBE'"
repla=connection
in=$fin out=$repla perl -pi.nk -e 's/\Q$ENV{"in"}/$ENV{"out"}/g' /home/sedFile.txt

How to remove a special character in a string in a file using linux commands

I need to remove the character : from a file. Ex: I have numbers in the following format:
b3:07:4d
I want them to be like:
b3074d
I am using the following command:
grep ':' source.txt | sed -e 's/://' > des.txt
I am new to Linux. The file is quite big & I want to make sure I'm using the write command.
You can do without the grep:
sed -e 's/://g' source.txt > des.txt
The -i option edits the file in place.
sed -i 's/://' source.txt
the first part isn't right as it'll completely omit lines which don't contain :
below is untested but should be right. The g at end of the regex is for global, means it should get them all.
sed -e 's/://g' source.txt > out.txt
updated to better syntax from Jon Lin's answer but you still want the /g I would think

Extract IP with sed from command

I've a string which looks like:
BOOT_IMAGE=/boot/vmlinuz-2.6.32-31-generic HTTP_BOOT=192.168.1.133 root=UUID=b4 ro quiet splash
In example "/proc/cmdline"
I would like to extract HTTP_BOOT with sed. My current sed command looks like that.
HTTP_BOOT=$(sed -r 's/^.*HTTP_BOOT=(.*?).*/\1/' /proc/cmdline)
The var HTTP_BOOT should contain 192.168.1.133.
Would be really happy if someone could correct my sed.
Thanks
Try this :
HTTP_BOOT=$(sed 's/.*HTTP_BOOT=\([^ ]*\) .*/\1/' < /proc/cmdline)
You are almost there. Try this sed:
sed -r 's/^.*HTTP_BOOT=([^\ ]*).*$/\1/'
HTTP_BOOT=$(egrep -o 'HTTP_BOOT=[1-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}' /proc/cmdline | cut -d '=' -f 2)
Also does a nice minor sanity check on the IP

Resources