Read line from file, manipulate, and then append to another file Code Answer

Hello Developer, Hope you guys are doing great. Today at Tutorial Guruji Official website, we are sharing the answer of Read line from file, manipulate, and then append to another file without wasting too much if your time.

The question is published on by Tutorial Guruji team.

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).

Answer

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
We are here to answer your question about Read line from file, manipulate, and then append to another file - If you find the proper solution, please don't forgot to share this with your team members.

Related Posts

Tutorial Guruji