Home > OS >  Through bashshell under Linux excuse me how to replace a few lines of the file, file substitute anot
Through bashshell under Linux excuse me how to replace a few lines of the file, file substitute anot

Time:11-10

For example:
File. TXT content:
 
Aaa
# # # # # # config - start
BBB
CCC
DDD
# # # # # # config - end
Eee


Hope to replace for the
 
Aaa
# # # # # # config - start
111
222
333
# # # # # # config - end
Eee


Is the file. TXT specified in the content (config - all content between the start and the config - end), and replaced by another part of the content, the other part of the content may also have more lines or special symbols, and then specify the content outside of it, don't move,
Appended to the end of the file after also cannot be deleted,
I think through the sed, but never find a way, hope expert advice,

CodePudding user response:

Sed '/star/{: A; N;/end/! S/\ n */\ n111 \ n2222 \ n3333/; TA} '

Test:
# echo 'aaa
# # # # # # config - start
BBB
CCC
DDD
# # # # # # config - end
Eee '| sed'/star/{: A; N;/end/! S/\ n */\ n111 \ n2222 \ n3333/; TA} '
Aaa
# # # # # # config - start
111
2222
3333
# # # # # # config - end
Eee

CodePudding user response:

There may be other two methods
 # to create two files 
The cat & lt; Aaa
# # # # # # config - start
BBB
CCC
DDD
# # # # # # config - end
Eee
EOF

The cat & lt; 123
456
789
EOF

The first kind, suitable for alternative content is not much, experts of
 sed '
BBB/,/DDD/c \
123 \ n456 \ n789
'file1
# or
Sed '
BBB/,/DDD/c \
987 \
654 \
321
'file1

Description:
'c \'
'TEXT'
Receive two address, the two address to delete the content of the certain scope, and replace the following TEXT
TEXT of each line with \ the ending, but the last line does not need to use \
Can use the escape character
P.S. you can also use a \ TEXT, but need a few more steps

The second method, suitable for harvest a few lines from other documents, a wide range of replacement
 sed - n '1, 3 p file2 | sed - e - e' 3 r/dev/stdin ' '3, 5 d file1 
# or anything
Echo - e "hello \ nworld" | sed -e '3 r/dev/stdin - e' 3, 5 d file1
# or directly from the file
Sed -e '3 r file2 - e' 3, 5 d file1

Description:
Main use r FILENAME command, '/dev/stdin is only can use the GNU sed special FILENAME, said read from standard input, suitable for piping,
R file cached content first, when the sed after processing a rebirth, so the contents of the cache to output
Due to the d command will force to start the next round, so use r first, then use d
P.S. the so-called transmigration (cycle), sed has two buffer is called the pattern and hold,
Sed every time read a line from the input stream, and in the pattern buffer,
If it matches the locator (address, such as the line number, the regular expression/REGEXP/), is using the command processing it,
If you don't specify the address of the default processing each row;
General if not use the -n option, after the last line of the command processing, print the contents of the pattern buffer.
Begin the next round of

CodePudding user response:

Eldest brother, don't think the sed and so enigmatic usage, you this a few lines, is I to digest a enough, I'll study
  • Related