Extract IP with sed from command - linux

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

Related

Sed: Insert text before a random text

I am having problems using sed to change this:
script_summary("Short random text");
script_id(#12345);
Into this:
script_tag(name:"text", value:"Short random text");
script_oid("1.3.6.1.4.1.25623.1.0.#12345");
Could you please, point me out in the right direction?
Thank you very much in advance.
Best regards,
What about:
Using 2 iterations. Testing it:
echo 'script_summary("Short random text");' | sed -e 's/script_summary("\(.*\)");/script_tag(name:"text", value:"\1");/'
echo 'script_id(#12345);' | sed -e 's/script_id(\(#[0-9]\+\));/script_oid("1.3.6.1.4.1.25623.1.0.\1");/'
So you may want to use it with
sed -i -e <regex1> <file>
sed -i -e <regex2> <file>
or
cat <file> | sed -e <regex1> | sed -e <regex2>
depends on how big the files is, and what you want to do with it.

How to remove `^I` character in 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.

Pipe grep output into sed to replace an entire line

I'm trying to pipe the output of a grep command into the 'replace me with' value in a sed command. I've tried xargs and just a pipe, but I can't seem to get it working. All of the examples I've found on stack overflow assume that I know the end result of my grep command. Here is an example of what I'm trying to do.
cat /etc/sysconfig/network | grep HOSTNAME | grep -i s/greppedline/"HOSTNAME=something"/
Effectively, I won't know the full contents of the line that I need to replace, just the fact that HOSTNAME will be in it. Is there a away to do this as a one-liner without creating a variable from the grep commmand?
I think you're trying to do like this,
sed '/HOSTNAME/s/.*/"HOSTNAME=something"/' /etc/sysconfig/network
Add the inline edit -i option to save the changes made.
sed -i.bak '/HOSTNAME/s/.*/"HOSTNAME=something"/' /etc/sysconfig/network
sed '/HOSTNAME/ c\
"HOSTNAME=something"/' /etc/sysconfig/network
or
sed 's/.*HOSTNAME.*/"HOSTNAME=something"/' /etc/sysconfig/network

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

Bash - How to remove all white spaces from a given text file?

I want to remove all the white spaces from a given text file.
Is there any shell command available for this ?
Or, how to use sed for this purpose?
I want something like below:
$ cat hello.txt | sed ....
I tried this : cat hello.txt | sed 's/ //g' .
But it removes only spaces, not tabs.
Thanks.
$ man tr
NAME
tr - translate or delete characters
SYNOPSIS
tr [OPTION]... SET1 [SET2]
DESCRIPTION
Translate, squeeze, and/or delete characters from standard
input, writing to standard output.
In order to wipe all whitespace including newlines you can try:
cat file.txt | tr -d " \t\n\r"
You can also use the character classes defined by tr (credits to htompkins comment):
cat file.txt | tr -d "[:space:]"
For example, in order to wipe just horizontal white space:
cat file.txt | tr -d "[:blank:]"
Much simpler to my opinion:
sed -r 's/\s+//g' filename
I think you may use sed to wipe out the space while not losing some infomation like changing to another line.
cat hello.txt | sed '/^$/d;s/[[:blank:]]//g'
To apply into existing file, use following:
sed -i '/^$/d;s/[[:blank:]]//g' hello.txt
Try this:
sed -e 's/[\t ]//g;/^$/d'
(found here)
The first part removes all tabs (\t) and spaces, and the second part removes all empty lines
If you want to remove ALL whitespace, even newlines:
perl -pe 's/\s+//g' file
This answer is similar to other however as some people have been complaining that the output goes to STDOUT i am just going to suggest redirecting it to the original file and overwriting it. I would never normally suggest this but sometimes quick and dirty works.
cat file.txt | tr -d " \t\n\r" > file.txt
Easiest way for me:
echo "Hello my name is Donald" | sed s/\ //g
This is probably the simplest way of doing it:
sed -r 's/\s+//g' filename > output
mv ouput filename
Dude, Just python test.py in your terminal.
f = open('/home/hduser/Desktop/data.csv' , 'r')
x = f.read().split()
f.close()
y = ' '.join(x)
f = open('/home/hduser/Desktop/data.csv','w')
f.write(y)
f.close()
Try this:
tr -d " \t" <filename
See the manpage for tr(1) for more details.
hmm...seems like something on the order of sed -e "s/[ \t\n\r\v]//g" < hello.txt should be in the right ballpark (seems to work under cygwin in any case).

Resources