0

I'm wondering if anybody could suggest a way to duplicate the contents of a file, in such a way that the duplicates have been altered?

For example, file pre duplication:

Nempar|EIJ87098.1  
Ecanceri|ORD93056.1

File post duplication:

Nempar|EIJ87098.1  
Ecanceri1|ORD93056.1  
Nempardup|EIJ87098.1  
Ecanceridup|ORD93056.1

I don't need the alteration to be in any particular location or any particular character. Just that it marks the duplicates.

Currently, I'm simply using:

cat file.txt file.txt > file.dup.txt

Is there any way I can just add on top of this, or is cat too simple?

  • Could you describe what isn't working as you'd want with the cat command that you are using? Is it that you'd want to "mark the duplicates"? What does that mean? – Kusalananda Aug 2 at 14:54
  • @Kusalananda Sorry, that may have been unclear. Nothing is "wrong" with the cat command, it works. I'm wondering if there's a way that when I duplicate the contents of a file, I can add a character to make it different from the original. For example: let's say in the original file I had a line that said 'Space', the duplicate of it could be 'Space1'. – Jahcub Trew Aug 2 at 15:12
  • Did you mean: “@Kusalananda I may have been unclear. While the cat command does what I expected. I am wondering if there is a way that when I duplicate the contents of the file, I can add some characters to make it different from the original. For example: let us say in the original file I had a line that said 'Space', the duplicate of it should be 'Space1'”? – ctrl-alt-delor Aug 2 at 15:53
0

Try this,

(cat file.txt && awk -F '|' '{print $1"dup|"$2}' file.txt) > file.dup.txt


cat file.dup.txt 

Nempar|EIJ87098.1
Ecanceri|ORD93056.1
Nempardup|EIJ87098.1
Ecanceridup|ORD93056.1

or using sed:

(cat file.txt && sed 's/|/dup|/g' file.txt) > file.dup.txt
  • Perfect, worked a charm and seemed to be the simplest! – Jahcub Trew Aug 5 at 13:59
0

You could use ed to replace (globally) some given PATTERN in file with REPLACEMENT and then append (W) the result to the original contents of file

Given

$ cat file
Nempar|EIJ87098.1
Ecanceri|ORD93056.1

then

$ ed -s file << 'EOF'
,s/|/dup|/
Wq
EOF

result

$ cat file
Nempar|EIJ87098.1
Ecanceri|ORD93056.1
Nempardup|EIJ87098.1
Ecanceridup|ORD93056.1
0

Using process substitution and sed

Imagine you would modify the input file and append dup to the first column:

$ sed 's/|/dup|/' file.txt
Nempardup|EIJ87098.1
Ecanceridup|ORD93056.1

This substitutes the first | with dup| in each line.

You could cat this modified file with a process substitution of the form cat <(command).

$ cat <(sed 's/|/dup|/' file.txt)
Nempardup|EIJ87098.1
Ecanceridup|ORD93056.1

Obviously this produces the same output as the first sed command.

And with the original input file cated, it produces

$ cat file.txt <(sed 's/|/dup|/' file.txt)
Nempar|EIJ87098.1
Ecanceri|ORD93056.1
Nempardup|EIJ87098.1
Ecanceridup|ORD93056.1

You can redirect the output as usual to another file:

$ cat file.txt <(sed 's/|/dup|/' file.txt) > file.dup.txt

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.