Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to duplicate lines in Linux with keeping the original order?

Tags:

linux

I have a file (with only 1 column) like this:

A
B
Z
D
N

and what I want to do is to duplicate each line so I get this:

A
A
B
B
Z
Z
D
D
N
N

I only could think of using cat for the same file and then sort it:

cat file1 file1 | sort -k1 > file1_duplicate

but then I lose the order of my file which is important for me:

A
A
B
B
D
D
N
N
Z
Z

any suggestion would be helpful.

like image 411
mahmood Avatar asked Nov 17 '25 20:11

mahmood


1 Answers

Try e.g.

sed p file >newfile
like image 80
tripleee Avatar answered Nov 21 '25 10:11

tripleee



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!