Shell finishing (33) = = = = = = = = = Change text content

Source: Internet
Author: User

Title: There are many sub-directories and files in the/tmp directory of the Linux system. Write a program (Shell/perl/python or any other language) to find the file of the wildcard log.201710* and replace any lowercase ABC letters in the file with uppercase ABC, and the resulting file is saved in the log.new.201710* file in the source directory.


Create conditions for yourself according to the topic

For example my environment: there are two directories under my/tmp/, one 000, one 111.

650) this.width=650; "Src=" https://s2.51cto.com/wyfs02/M02/A6/7F/wKioL1nQlrrxwVZ_AAAy85R0u7w906.png-wh_500x0-wm_ 3-wmp_4-s_4264954184.png "title=" capture. PNG "alt=" Wkiol1nqlrrxwvz_aaay85r0u7w906.png-wh_50 "/>

Look at the files I created, my files except for 000 in the log.201710111 and 111 in the og.201710111 have the content of the other is empty.

[[email protected] 000]# ls

log.201710111 log.201710222 log.201710333 log.201712333 log.201715333

[Email protected] 000]#

[email protected] 111]# ls

log.201710111 log.201710222 log.201715111

[Email protected] 111]#

For example:

[email protected] 111]# cat log.201710111

Aaaaaaaaa

bbbbbbbbbb

Cccccccccc

Dddddddddd

Eeeeeeee

[Email protected] 111]#

[email protected] 000]# cat log.201710111

A

B

C

Aaaaaaaaaaaaa

bnbbbbbbbbbbbb

Dsfsagsagr

Fsdf

[Email protected] 000]#


650) this.width=650; "Src=" https://s2.51cto.com/wyfs02/M01/07/CE/wKiom1nQmISwTvPTAACSBXUPUJM368.png-wh_500x0-wm_ 3-wmp_4-s_2167538975.png "title=" capture. PNG "alt=" Wkiom1nqmiswtvptaacsbxupujm368.png-wh_50 "/>


The shell code is as follows:

#!/bin/bash


A= ' find/tmp/-type f-name ' log.201710* '

echo "$a" | While Read line

Do

C= ' echo $line |awk-f ' [/]+ ' {print $} '

E= ' echo $line |awk-f ' [/.] + ' {print $} '

B= ' Cat $line | grep ' [ABC] '

if [!-Z "$b"];then

#echo $line |sed ' s/abc/abc/g ' >>/tmp/$c/log.new. $e

Cat $line | TR ' abc ' abc ' >/tmp/$c/log.new. $e

Fi

Done

Although there are only a few lines of code, there are some details. For example if that $b must be enclosed in double quotation marks, because if not added, it will

650) this.width=650; "Src=" https://s4.51cto.com/wyfs02/M01/A6/7F/wKioL1nQnJKhkGMmAACNI66qACs861.png-wh_500x0-wm_ 3-wmp_4-s_1175794498.png "title=" capture. PNG "alt=" Wkiol1nqnjkhkgmmaacni66qacs861.png-wh_50 "/>

will be error, because this variable has a carriage return, so you must add double quotes, if you do not want to make a mistake, all the variables as far as possible double quotation marks

Details of the 2:sed and TR usage is not the same, there is no use to find an append, a use of the overlay.

Detail 3: The second line, a must be enclosed in double quotes, the variable is not in a row

Detail 4: Line sixth, grep ' abc ' represents a continuous ABC string, while grep ' [ABC] ' represents any of the parentheses.

For example:

[email protected] 000]# cat log.201710111

A

B

C

Abc

Abckjawdfalfd

A JSKD BNSDJKADC

Aaaaaaaaaaaaa

bnbbbbbbbbbbbb

Dsfsagsagr

Fsdf

[Email protected] 000]#

[Email protected] 000]# grep ' abc ' log.201710111

Abc

Abckjawdfalfd

[Email protected] 000]#

[[email protected] 000]# grep ' [ABC] ' log.201710111

A

B

C

Abc

Abckjawdfalfd

A JSKD BNSDJKADC

Aaaaaaaaaaaaa

bnbbbbbbbbbbbb

Dsfsagsagr

[Email protected] 000]#

The results of the implementation are as follows:


650) this.width=650; "Src=" https://s3.51cto.com/wyfs02/M02/07/CE/wKiom1nQmUvzafIzAACl1jorX2M340.png-wh_500x0-wm_ 3-wmp_4-s_1189063789.png "title=" capture. PNG "alt=" Wkiom1nqmuvzafizaacl1jorx2m340.png-wh_50 "/>

[email protected] 111]# cat log.new.201710111

Aaaaaaaaa

bbbbbbbbbb

Cccccccccc

Dddddddddd

Eeeeeeee

[Email protected] 111]#

[email protected] 000]# cat log.new.201710111

A

B

C

Aaaaaaaaaaaaa

bnbbbbbbbbbbbb

Dsfsagsagr

Fsdf

[Email protected] 000]#

If we change the subject,

Title: There are many sub-directories and files in the/tmp directory of the Linux system. Write a program (Shell/perl/python or any other language) to find the file with the wildcard log.201710* and replace any "ABC" string in the file with an uppercase "ABC", and the resulting file is saved in the log.new.201710* file in the source directory.

The shell script is as follows:

#!/bin/bash

For i in ' Find/tmp-type f-name "log.201710*" '

Do

str= ' grep ' abc ' $i '

if [!-Z "$str"];then

Sed ' s/abc/abc/g ' $i > ' dirname $i '/log.new. ' Echo $i |awk-f '. ' ' {Print $NF} '

Fi

Done

~

Linux dirname command:

Usage: dirname filename

Function: Removes the file name (non-directory part) from the given file name containing the absolute path, leaving the directory part.

For example:

[Email protected]/]# DIRNAME/ETC/PASSWD

/etc

[Email protected]/]#


This article is from the "It Life" blog, so be sure to keep this source http://dingxue.blog.51cto.com/12290895/1970097

Shell finishing (33) = = = = = = = = = Change text content

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.