I have a text file in which I have something like this-
10.2.57.44 56538154 3028
120.149.20.197 28909678 3166
10.90.158.161 869126135 6025
In that text file, I have around 400,000 rows exactly as above. I have opened the same text file in Notepad++. I needed a way to remove everything from that text file leaving only IP Address (first column in the above text file is IP Address). I think, I can do that using Regular Expression. And notepad++ also has the option of using Regular Expression. But not sure what regular expression I need to use. Can anyone help me out here?
So output should be something like this-
10.2.57.44
120.149.20.197
10.90.158.161
Find:
(\d+\.\d+\.\d+\.\d+).*
And replace with:
\1
The parentheses will capture that part of the regex to a variable, which is named \\1
(since it is the first capture block in the regex). The rest of the line is not captured but simply thrown out by the replace operation.
Just replace \\s.*
with nothing! A regex needs only to match what it needs to match, and what you want to match here is a space followed by anything (thus leaving the IP address alone, since it starts the line and there are no spaces in it)
Alternatively, if you have them, this is even better done with a classical Unix command:
sed -i 's/\s.*//' thefile
Example:
$ cat <<EOF | sed 's/\s.*//'
> 10.2.57.44 56538154 3028
> 120.149.20.197 28909678 3166
> 10.90.158.161 869126135 6025
> EOF
10.2.57.44
120.149.20.197
10.90.158.161
$
If you have gawk or something:
gawk "{print $1}" filename
In linux(bash) usage of awk
will look like following:
suku@ubuntu-vm:~$ cat stack
10.2.57.44 56538154 3028
120.149.20.197 28909678 3166
10.90.158.161 869126135 6025
suku@ubuntu-vm:~$ cat stack | awk '{ print $1 }'
10.2.57.44
120.149.20.197
10.90.158.161
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.