0

I want to extract certain values from files in many subdirectories. I have a working code that does this:

for i in */; do grep "free energy" "$i"filename | tail -1 | xargs -I{} echo "$i: {}" >> output.txt; done

the tail -1 command gives me the last output from each file (which I want) and the xargs construction prepends the folder name so I know which line belongs to which system. The output.txt looks like this:

Cr/: free energy TOTEN = -18.87487583 eV Hf/: free energy TOTEN = -18.76845679 eV N/: free energy TOTEN = -15.90748159 eV Nb/: free energy TOTEN = -20.38801759 eV Ti/: free energy TOTEN = -14.92233353 eV TiN/: free energy TOTEN = -78.73147048 eV

Where Cr, Hf, N, TiN and so on are the folder names. My problem is that the values on the right side are not aligned properly, because the folders have different character lengths. Changing the folder names is out of the question so I want the xargs command to always use a certain amount of spaces (like 10) - at least as a minimum - so the vertical alignment is proper. The result should look something like this:

Cr/ : free energy TOTEN = -18.87487583 eV Hf/ : free energy TOTEN = -18.76845679 eV N/ : free energy TOTEN = -15.90748159 eV Nb/ : free energy TOTEN = -20.38801759 eV Ti/ : free energy TOTEN = -14.92233353 eV TiN/: free energy TOTEN = -78.73147048 eV

or this

Cr /: free energy TOTEN = -18.87487583 eV Hf /: free energy TOTEN = -18.76845679 eV N /: free energy TOTEN = -15.90748159 eV Nb /: free energy TOTEN = -20.38801759 eV Ti /: free energy TOTEN = -14.92233353 eV TiN/: free energy TOTEN = -78.73147048 eV

so that I can easily copy all the values at once with Alt + Leftclick.

1

printf is what you want

xargs -I{} printf "%-10s: %s\n" "$i" "{}"

Note that this will use /usr/bin/printf, not the shell's builtin printf.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.