Bash – How to aggregate data from many files into one file

bashtext processing

I have this directory structure for about 100 users.

users
- user1
  - info.txt
- user2
  - info.txt
- user3
  - info.txt
...

Inside the info.txt files, the contents look like this.

5 some_other_info

Basically, it's just one line, containing a number, a space, then some text (may have spaces within).

I'd like to create a file result.txt that looks like this.

user1 5
user2 6
user3 7
...

Where user1, user2, user3, … matches the directory name and the numbers match what's in their respective info.txt files.

You can assume that the user directories have no spaces in their names.

What's a good way to do this?

Best Answer

awk '{split(FILENAME,u,"/"); print u[2], $1}' users/*/info.txt
Related Question