Descubrimientos de confinamiento

El estar encerrados por el bien de nosotros y la sociedad se convirtió en algo obligatorio, esto causo que mi tiempo libre se multiplicara. Al principio use este tiempo para descansar, ver películas…

Smartphone

独家优惠奖金 100% 高达 1 BTC + 180 免费旋转




Piping a file list for another shell command

Being a student again takes a longer while than I expected to get used to, which gives me a long hiatus in my posting, but also puts me in a different and stimulating environment.

This post was inspired by a question from my colleague, who want to first find some file entries and then merge them into a file, so the initial attempt was:

After getting the list of file names , it might be tempting to pipe this result to cat and then to a text file like:

But this is only giving you the list of file names in merged.txt because it is actually ./c.txt\n./b.txt\n./a.txt (\n is the new-line character) being piped to cat. Though cat does try to find a file matching this name, there is obviously none in this directory, and cat has no other choice but printing the string it receives as it is.

I am bad at terminal commands, so the first thing I came up with was to use awk to send the file names one by one to cat with system(), which makes a system call in awk scripts.

This worked, but the use of awk seemed like an overkill.

I later noticed xargs, which takes standard input, splits it by blanks or new lines and sends the resulting list to a command. This looked like exactly the thing I wanted.

Indeed, find . -regex “./[a-z].txt" | xargs cat >> "merged.txt" does exactly the same thing in a more elegant way. Additionally, xargs is of course more versatile than that.

Add a comment

Related posts:

NFT Giveaway results

After 5 days of Dragon Kart event, we have recorded more than 1000 participants with many articles and reviews about the project on social media platforms. This is an important and meaningful event…

The Vital Role of Life Sciences in Advancing Healthcare and Improving Lives

EOS Drug Discovery is your trusted partner in the journey towards developing life-saving drugs. Our team of experienced researchers and state-of-the-art facilities are dedicated to delivering…

What is Google Bard?

A big language model chatbot called Google Bard was introduced by Google AI in 2023. LaMDA, a language model built on research, powers it. It can compose many types of creative material, translate…