Linux system can use the command line to perform various operations compared with other systems, which is a prominent feature. Today Xiaobian will share with you how to use the command in the Linux system to find and replace the contents of the file.
How to use the Find and Replace command on the contents of the file:
Batch Find content contained in the heads of a file, for example:
# grep -. Rn "Find the text you are looking for" ./
2. Find and replace the contents of the file in batches.
# sed -i "s/Find text to find/replaced text/g" `grep -rl "Find text to find" ./`
sed Other usages are as follows:
1. Delete the first space of the line
sed 's/^[ ]*//g' filename
sed 's/^ *//g' filename
sed 's/^[[:space:]]*//g' filename
2. Add a new line after the line and before the line
After: sed 's/pattern/&\ /g' filename
Before the line: sed 's/pattern/\ &/g' filename
& represents pattern
3, use variable substitution (use double quotes)
sed -e "s/$var1/$var2/g" filename
4, insert before the first line Text
sed -i '1 i\\insert string ' filename
5, insert in the last line
sed -i '$ a\\insert string ' filename< Br>
6, insert before the matching line
sed -i '/pattern/i " insert string "' filename
7, insert the matching line after the
sed -i '/pattern/a &qu Ot; insert string "' filename
8, delete the line of text lines and spaces and the line of ## comment
grep -v ^# filename |
Sed /^[[:space:]]*$/d |
Sed /^$/d
find command
find -name 'filename to look for' |
Xargs perl -pi -e 's|
The string being replaced |
Replaced string |
G'
#find replace the current directory containing strings and replace them
find -name '*.txt' |
Xargs perl -pi -e 's|
Wisdom village|
Northern mountains |
g'
#recursive lookup replacement
find . -type f -name '*.html' |
Xargs perl -pi -e 's|
Wisdom village|
Northern mountains |
g'
#Search directory LOG contains display
find ./-name '*log*' -type d
find ./-name '*Log*' -type d more
Of course, you can also use linux vim bulk replacement
to replace only the current line:
Example
:s/XXX/YYY /g
, if you need to replace all:
Example
:%s/XXX/YYY/g
If you need to replace the specified part You can use V to enter the visual mode, then
Example
:s/XXX/YYY/g
or you can specify the number of rows to replace the specified range:
Example
:10,31s/XXX/YYY/g
If you need to display the line number, under vim
Example
: Set nu
Cancel the display line number:
Example
:set nonu
:s/well/good/Replace the current line with the first well Good Previous12Next page Total 2 pages
When using vim on Linux, because the font of the annotation font is too light, it cant be seen clear
After many friends upgraded the system to Ubuntu 12.04 LTS, the icon of the ibus input method in the
A user has installed OpenSC before, but somehow suddenly can not be used, how to deal with this prob
A network protocol is a collection of communication rules between all devices on th
What should I do if I get an error when installing LDAP on Linux under Linux?
How Linux looks at the last reboot time
Steps to set up UFW firewall on Ubuntu
Linux accidentally delete the log file syslog how to do?
Detailed method of configuring CentOS devices using Quagga software
CentOS 6.6 system installation steps
Ubuntu steps to install PlayOnLinux
How to use Ctags tool to quickly find source code in Linux
How Linux can quickly modify the sql file in the specified ID
Linux how to use the whereis command to quickly find the file
Linux uses Florence to set up screen keyboard operation examples
What should I do if the Win7 Installer Task Manager does not respond when I shut down the process?
"Double-sided" network people: remote control giants and mobile newcomers
Win7 Home Edition can not install the desktop theme how to do
Novices don't have to teach you how to easily play Baidu Cloud Manager
Win8 how to uninstall the program 3 kinds of Win8 uninstall software method
How to disable the folder on the desktop of windows7 computer
Windows 7 uses Win7codecs to add subtitles