In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
Xiaobian to share with you how Shell script to achieve find and replace, I believe most people do not know how, so share this article for your reference, I hope you have a lot of harvest after reading this article, let us go to understand it together!
3.1 find text
grep: Basic regular expressions (BRE) defined using POSIX.
egrep: Use extended regular expressions (ERE).
Fgrep: Fast grep. Use optimized algorithms to match fixed strings instead of regular expressions.
The 1992 POSIX standard integrates these three revisions into a single grep program.
$ who | grep -F austen
Use the-F option to find fixed strings. In fact, as long as the matching pattern does not contain meta characters of regular expressions,
The grep default behavior pattern is equivalent to using-F.
3.2.6 Substitution in Text Files
In general, the correct program to perform text substitution is the sed-stream editor.
sed 's/:.*// ' /etc/passwd |Delete everything after the first colon
sort -u Sort lists and remove duplicates
Any displayable character can be used as a delimiter.
The code is as follows:
sed 's;/home/tolstoy/;/home/lt/;'
sed 's/\\/\\/g'
With the-e and-f options, you can replace multiple sed's at once without having to string them through a pipe.
The code is as follows:
$ sed -e 's/foo/bar/g' -e 's/chicken/cow/g' file1.xml > file2.xml
or
$ cat fixup.sed
s/foo/bar/g
s/chicken/cow/g
...
$ sed -f fixup.sed file1.xml > file2.xml
3.2.8 Operation of sed
Each file name on the command line is opened and read in turn. If there is no file, standard input is used.
Sed reads each file, one line at a time, and places the read lines in an area of memory (pattern space).
All actions on the edit are applied to the contents of the pattern space, and when all actions are complete, sed will change the pattern
The last content of the space is printed to standard output, and then back to the beginning to read another input line.
3.3 field handling
Use spaces (tabs) or special delimiters (such as colons).
A line starting with the #character indicates a comment, and software must be able to ignore such a line.
The best example of delimiting fields is/etc/passwd: one line represents a user, and each field is separated by a colon.
The file contains seven fields:
tolstoy:x:2076:10:Leo Tolstoy:/home/tolstoy:/bin/bash
1. User Name:2. Encrypted Password:3. User ID:4. User Group ID:5. Name:6. Root Directory:7. Logged in Shell.
3.3.2 Use cut to select fields
The code is as follows:
$ cut -d : -f 1,5 /etc/passwd
root:root
...
tolstoy:Leo Tolstoy
$ cut -d : -f 6 /etc/passwd
/root
...
/home/tolstoy
3.3.3 Using join fields
Combine multiple files with a common key (primary field).
$ cat sales
#Number of employees
joe 100
jane 200
herman 150
chris 300
$ cat quotas
#Salesman quota
joe 50
jane 75
herman 80
chris 95
The code is as follows:
#! /bin/sh
# merge-sales.sh
#Remove comments and sort data files
sed '/^#/d' quotas | sort > quotas.sorted
sed '/^#/d' sales | sort > sales.sorted
#Combine with the first key
join quotas.sorted sales.sorted
#Delete cache files
rm quotas.sorted sales.sorted
3.3.4 Using awk to rearrange fields
awk program basic architecture: pattern { action }
A pattern is usually an ERE enclosed by slashes, and an action is usually an explicit print statement.
Omitting pattern executes an action on each input record, omitting action is equivalent to { print }.
awk automatically divides each record into fields and stores the number of fields in each record in the built-in variable NF.
The default is white-space delimited, and you can also set the FS variable to a different value.$ Add a number to indicate the field value.
awk '{ print $1 }' Print 1st field
awk '{ print $2, $5 }' Print the 2nd and 5th fields
awk '{ print $1, $NF }' Print first and last fields
awk 'NF > 0 { print $0 }' Print nonblank lines
awk 'NF > 0' ditto
awk -F : '{ print $1, $5 }' /etc/passwd Sets the field separator character, the-F option automatically sets the FS variable.
root root
...
tolstoy Leo Tolstoy
Remember to separate print arguments with commas, otherwise awk concatenates all adjacent values.
awk -F: '{ print "User" $1 "is really" $5 }' /etc/passwd
Userrootis reallyroot
...
Usertolstoyis reallyLeo Tolstoy
The above is "Shell script how to achieve find and replace" all the content of this article, thank you for reading! I believe that everyone has a certain understanding, hope to share the content to help everyone, if you still want to learn more knowledge, welcome to pay attention to the industry information channel!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.