copy, then delete lines in file with sed using a pattern

I need to copy lines to a new file from files with sed using a pattern in char postions 1-3.

Then after the copy, I need to delete those same lines from the input files.

For example, string "ABC" in pos 1-3

Please, specify a sample of your input data and expected output.

Could you give an example?

sample input file 1:

ABC9875193458739457 LKSDJSG 983475394578 LSKJFG
QZS0987983758934584 OIOISDJ 023984234244 IOEUYR
QZS0987591485794788 OIOISDJ 023984234244 IOEUYR

sample input file 2:

QZS0988347653564788 OIOISDJ 023984234244 IOEUYR
ABC9898751345345457 LKSDJSG 983475394578 LSKJFG
QZS0987236523652346 OIOISDJ 023984234244 IOEUYR
 

sample output file:

ABC9875193458739457 LKSDJSG 983475394578 LSKJFG
ABC9898751345345457 LKSDJSG 983475394578 LSKJFG

^and these lines removed from input 1&2

To copy lines starting with "ABC" into a new file:

egrep "^ABC" input_file > output_file

To delete lines starting with "ABC" into another file:

egrep -v "^ABC" input_file > output_file
1 Like
sed -i '/^ABC/W output
/^ABC/d'  file1 file2
 
1 Like

I only want ABC from char pos 1-3 because it could be elsewhere in the file and elsewhere it should be ignored.