0

To pool specific content from a batch of files, I do

for ID in {92..128}; do 
    sed '3q;d' directory_$ID/stats
done

Now what if want to put the $ID in front of each line read (preferably shifting the columns in a fixed-width manner) and then append the line to a report.txt file (creating it if it doesn't exist). I did some research on this but there seem to be many potential ways of doing it, none of which I'm familiar with as a new Linux user (perhaps I should just use Python next time).

1 Answer 1

2

To append $ID (with space) at the beginning of each line something like

sed "s/^/$ID /"

should work (notice double, not single quotes). If you want to do this within the given loop and redirect output to report.txt try

for ID in {92..128}; do 
    sed "s/^/$ID /;3q;d" directory_$ID/stats
done > report.txt
2
  • Ha, that looks rather like a Vim command. Now if there were an easy way for me to maintain fixed-width spacing (e.g. put 4 spaces after a 2-digit ID and 3 spaces after a 3-digit one), that would be excellent.
    – bongbang
    Commented Oct 22, 2014 at 17:35
  • How about using tabs instead of spaces: sed "s/^/$ID\t/"? This way columns will be aligned properly.
    – jimmij
    Commented Oct 22, 2014 at 17:45

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .