I have some text files, and I'd like to be able to move an arbitrary line in any of the files up or down one line (lines at the beginning or end of the file would stay where they are). I have some working code but it seems kludgy and I'm not convinced I have all the edge cases covered, so I'm wondering if there's some tool or paradigm that does this better (e.g. easier to understand the code (for other readers or me in 6 months), easier to debug, and easier to maintain; "more efficient" isn't very important).
move_up() {
# fetch line with head -<line number> | tail -1
# insert that one line higher
# delete the old line
sed -i -e "$((line_number-1))i$(head -$line_number $file | tail -1)" -e "${line_number}d" "$file"
}
move_down() {
file_length=$(wc -l < "$file")
if [[ "$line_number" -ge $((file_length - 1)) ]]; then
# sed can't insert past the end of the file, so append the line
# then delete the old line
echo $(head -$line_number "$file" | tail -1) >> "$file"
sed -i "${line_number}d" "$file"
else
# get the line, and insert it after the next line, and delete the original
sed -i -e "$((line_number+2))i$(head -$line_number $file | tail -1)" -e "${line_number}d" "$file"
fi
}
I can do error checking of inputs inside or outside these functions, but bonus points if bad input (like non-integers, non-existent files, or line numbers greater than the length of the file) are handled sanely.
I want it to run in a Bash script on modern Debian/Ubuntu systems. I don't always have root access but can expect "standard" tools to be installed (think a shared web server), and may be able to request installation of other tools if I can justify the request (though fewer external dependencies is always better).
Example:
$ cat b
1
2
3
4
$ file=b line_number=3 move_up
$ cat b
1
3
2
4
$ file=b line_number=3 move_down
$ cat b
1
3
4
2
$