What's the easiest way for me to script moving the last line of a text file to the Nth line? I have a whole directory full of files that need the exact same treatment.

Update: My solution in replies!

#AskFedi #AskFediverse #LazyWeb

@josh Assuming N is the same for all files, maybe a two-step combo of sed and tail? Eg for N=6

sed -i "6i $(tail -n 1 $file)" && sed -i '$d' $file

Untested, probably totally wrong if you are on a Mac, will most certainly fail if the last line contains a $ etc

I'm also sure some professional sysadmin will cry over this "obviously bad idea"

@josh Based on the timestamps on your own response, it took me at least 6 minutes to type that in on my phone. On-screen keyboard suck for code!

@tkissing My solution was very similar, but using vim!

I knew enough about the content (already mostly normalized via many regex and various find/replace steps) that I knew for sure every file was the same.

Worked really well for what I needed, and a super useful thing to know how to do!