How to add sizes of all files with the same name in UNIX Shell scripts

Zero Darbelll

I'm new here and I want to ask how to add sizes of files with the same name? My file names are actually different from each other but I cut them so I could get files that are grouped together.

here are my original files with sizes (sample)

sample.txt has this data inside:

  1. 12345 a_1.txt
  2. 12234 b_1.txt
  3. 32123 c_1.txt
  4. 11122 a_2.txt

Now I cut the filenames inside sample.txt to remove those characters starting from '_' (underscore). They become like this:

  1. 12345 a
  2. 12234 b
  3. 32123 c
  4. 11122 a

Now I want to add sizes of all files with same filename as seen above. Output should be like this:

  1. 23467 a
  2. 12234 b
  3. 32123 c

Please help. Thanks a lot guys. I'm stuck here for hours now

Kamil Maciorowski

Assuming there are no line numbers in sample.txt:

cut -f 1 -d _ sample.txt | awk '{a[$2] += $1} END{for (i in a) print a[i], i}'

You may want to add | sort -k 2 at the end.


EDIT1 - explanation as requested:

The cut command cuts every line with _ delimiter and saves the first part only. You have already done it with your original file.

Then the awk command finds two fields in every line. We call them size and name, but awk refers to them as $1 and $2 internally. For every line it increments one element of an array a (the name a is arbitrarily chosen and has nothing to do with filename in the sample). The name $2 tells which element to increment -- it is an index; the size $1 is the incrementation value. awk is smart enough to initialize a element with 0 as it is mentioned for the first time. The particular element is incremented every time its index (name) appears as a second field of the input line. At the end (after the last line of the input) awk goes through every known index of a and prints the value (which is now cumulative size) and an index (name).

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Renaming files with same file name pattern in Unix server using shell script

How to add .xml extension to all files in a folder in Unix/Linux

How to add .xml extension to all files in a folder in Unix/Linux

Unix Shell Scripts Functions

How to calculate the total sizes for all files uploaded

How to rename files in all directories to the same name on terminal

Using SublimeText3, how to open all files with the same name

How to efficiently rename all files/directories with same name?

How to merge text files with the same name from all subdirectories?

How to add parent folder name to all the files in a sub folder in Python

How to add Shell Scripts as a part of Java Project

How to add progress bar to shell scripts

How to rename all specific filetypes of files within subfolders to name with same file extension but same file name

How to copy files on unix shell using wildcards

How to remove all subdirectories? (unix shell scripting)

[Unix][Shell Script] Copy all files from one folder to the other

how to add execution permission (+x) to all scripts

Sending all files into folder with the same name

Unzip all files into directories with the same name

Rename all files with same base name

renaming all files with similar structure to same name

Search all files with same name in a directory python

Rename all files with the same extension and any name

flutter: how to suit all the screen sizes with the same program?

How to fetch sizes of all SFTP files in a directory through Paramiko

How do i check for all the files sizes in a directory while in a loop?

How do I find the sum of sizes of all files in a directory?

How to add a line in multiple files in unix

How to add Horizontal Scrollbar in Winform application to compatibility in all screen sizes