0

Can I double every text line of a file using sed in linux bash?

Example file:

aab1
aac2
awq6a
azs4

What i want:

aab1 | aab1
aac2 | aac2
awq6a | awq6a
azs4 | azs4

I know about "read line" but yet, how i am doing it:

  • by only using sed command?
  • by using "read line"?

ps. I want to double each line of the dir output command to use it for manipulation.

2
  • I have used sed, but i know that it can do process multiple lines for single string replace and not multiple lines with multiple string replace in same text. So i dont know. I think only read line can solve that with sed or awk --- put the $line at the end of each text line. so no attempt. But i asked if sed can do it that without invoking read line. so no idea. Commented Sep 6, 2022 at 18:58
  • 2
    Homework task, maybe?
    – Hannu
    Commented Sep 6, 2022 at 19:19

1 Answer 1

3

Using read is very basic

while IFS= read -r line; do printf "%s | %s\n"  "$line" "$line" ; done < filename

sed is not any more complicated

sed -r 's/(.*)/\1 | \1/' filename

In general, it would be more helpful if you included what you've already tried so that we know that you're learning and so we don't duplicate your efforts.

3
  • 1
    +1 for the sed ;)
    – tink
    Commented Sep 6, 2022 at 20:34
  • Thanks, it worked! :) Commented Sep 6, 2022 at 21:01
  • 1
    Even simpler: sed 's/.*/& | &/' filename Commented Sep 7, 2022 at 5:11

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .