Home > OS >  Shell script encountered a problem
Shell script encountered a problem

Time:09-23

The great god, who can tell me the shell script, replaces the specified file within a certain line column content of command is what? How do you use the awk? Thank you very much!

CodePudding user response:

The sed command
Sed -i 's/the original content/new content/' file name
-i is to modify the original file (without the -i will only replace printed to the screen, not modify the original file),
You can also add how many rows in front of the s, or use//pattern matching, such as: sed -i '5 s/yes/no/test. TXT will test. TXT in row 5 yes replace no
Sed -i '/aaa/s/yes/no/' test. TXT will test. TXT contained in aaa replaced line in the yes no

Detailed usage can baidu next sed

CodePudding user response:

reference 1st floor zhouchao6 response:

sed commandsSed -i 's/the original content/new content/' file name
-i is to modify the original file (without the -i will only replace printed to the screen, not modify the original file),
You can also add how many rows in front of the s, or use//pattern matching, such as: sed -i '5 s/yes/no/test. TXT will test. TXT in row 5 yes replace no
Sed -i '/aaa/s/yes/no/' test. TXT will test. TXT contained in aaa replaced line in the yes no

Detailed usage can baidu next sed

Thank you for your reply! I met the problem is, because I want to be a loop, so that the value of the file within the ranks of a fixed location it is change, and the same is that position, so I don't know how to write, to replace the position of the specified value, not pure is a fixed value!

CodePudding user response:

refer to the original poster Drimflier response:
great god, who can tell me the shell script, replaces the specified file within a certain line column content of command is what? How do you use the awk? Thank you very much!

Is the replacement string in the file is changing, the same is it's location, which command can be achieved regardless of the specific contents of the replacement, give the position of a specific category, can replace!

CodePudding user response:

Sed -i '2 c123456 replace the second line of 123456
If you want to replace the named column, you can imagine to replace multiple lines, then one by one to replace
String is random, you can first define and then as a variable way outside the incoming sed command

CodePudding user response:

reference 5 floor wwys reply:
sed -i '2 c123456 replace the second line of 123456
If you want to replace the named column, you can imagine to replace multiple lines, then one by one to replace
String is random, you can first define and then as a variable way outside the incoming sed command

Uh huh! Thank you for your reply! I've ready in external variables, but don't know to replace the ranks of specified values command what to write?

CodePudding user response:

reference Drimflier reply: 3/f
Quote: refer to the original poster Drimflier response:
great god, who can tell me the shell script, replaces the specified file within a certain line column content of command is what? How do you use the awk? Thank you very much!

Is the replacement string in the file is changing, the same is it's location, which command can be achieved regardless of the specific contents of the replacement, give the position of a specific category, can replace!



With a script, out put value, in the replacement
Such as to replace the test. The TXT file line 5, the content of the column 7 for BBB
A=` awk 'NR==5 {print $7}' test. TXT `
Sed -i "5 s/$A/BBB/" test. TXT

But this is not applicable to have more than one column in the line 5 are A

CodePudding user response:

Thank you thank you! I try the

CodePudding user response:

If you want to replace the rows, the columns, there may be repeated, can use the following method

A=` awk 'NR=={5 sub ($7, "BBB"); Print $0} 'test. TXT `
Sed -i "5 A \ \ $A" test. TXT
Sed -i '5 d' test. TXT

Similar sed sub is replaced, s, is the $7 (7) replaced with BBB, then replaced line 5 printed
Sed "5 A \ * * * * *" is the bottom line in line 5, behind \ * * * * * is to add the content of the double quotation marks, and \ \ two backslashes are in order to identify the $A variable
5 d is to delete the original line 5

Probably after replacement line 5 assigned to A
Then under the line 5 new line of A
Then delete the line 5, the addition of A, became A new line 5

CodePudding user response:

refer to 7th floor zhouchao6 response:
Quote: reference Drimflier reply: 3/f

Quote: refer to the original poster Drimflier response:
great god, who can tell me the shell script, replaces the specified file within a certain line column content of command is what? How do you use the awk? Thank you very much!

Is the replacement string in the file is changing, the same is it's location, which command can be achieved regardless of the specific contents of the replacement, give the position of a specific category, can replace!



With a script, out put value, in the replacement
Such as to replace the test. The TXT file line 5, the content of the column 7 for BBB
A=` awk 'NR==5 {print $7}' test. TXT `
Sed -i "5 s/$A/BBB/" test. TXT

But this is not applicable to have more than one column in the line 5 are A


Thank you thank you! A while back I try

CodePudding user response:

references 9 f zhouchao6 response:
if you want to replace the line, the column, there may be repeated, can use the following method

A=` awk 'NR=={5 sub ($7, "BBB"); Print $0} 'test. TXT `
Sed -i "5 A \ \ $A" test. TXT
Sed -i '5 d' test. TXT

Similar sed sub is replaced, s, is the $7 (7) replaced with BBB, then replaced line 5 printed
Sed "5 A \ * * * * *" is the bottom line in line 5, behind \ * * * * * is to add the content of the double quotation marks, and \ \ two backslashes are in order to identify the $A variable
5 d is to delete the original line 5

Probably after replacement line 5 assigned to A
Then under the line 5 new line of A
Then delete the line 5, add A, became A new line 5

Uh huh! Very thank you for your enthusiastic help! A while back I try it on! Thank you very much!

CodePudding user response:

references 9 f zhouchao6 response:
if you want to replace the line, the column, there may be repeated, can use the following method

A=` awk 'NR=={5 sub ($7, "BBB"); Print $0} 'test. TXT `
Sed -i "5 A \ \ $A" test. TXT
Sed -i '5 d' test. TXT

Similar sed sub is replaced, s, is the $7 (7) replaced with BBB, then replaced line 5 printed
Sed "5 A \ * * * * *" is the bottom line in line 5, behind \ * * * * * is to add the content of the double quotation marks, and \ \ two backslashes are in order to identify the $A variable
5 d is to delete the original line 5

Probably after replacement line 5 assigned to A
Then under the line 5 new line of A
Then delete the line 5, add A, became A new line 5

Very happy, your method to solve my problem! Thank you thank you!
  • Related