How can I read the lines of a file into an array in Perl?

后端 未结 7 2631
一向
一向 2021-02-20 12:22

I have a file named test.txt that is like this:

Test
Foo
Bar

But I want to put each line in a array and pri

7条回答
  •  你的背包
    2021-02-20 13:12

    Here is my single liner:

    perl -e 'chomp(@a = <>); print join(" ", @a)' test.txt
    

    Explanation:

    • read file by lines into @a array
    • chomp(..) - remove EOL symbols for each line
    • concatenate @a using space as separator
    • print result
    • pass file name as parameter

提交回复
热议问题