Search code examples
linuxsedconky

How to Move Column/(conky command) up 1 Line - Preferably with sed


parts of my file look like this:

${goto 97}${exec sed -n '1p' $HOME/.config/conky/itl/raw2}#
${goto 194}${exec sed -n '2p' $HOME/.config/conky/itl/raw2}#
${goto 291}${exec sed -n '3p' $HOME/.config/conky/itl/raw2}#
${goto 388}${exec sed -n '4p' $HOME/.config/conky/itl/raw2}#
${goto 485}${exec sed -n '5p' $HOME/.config/conky/itl/raw2}#
${goto 582}${exec sed -n '6p' $HOME/.config/conky/itl/raw2}#
${goto 679}${exec sed -n '7p' $HOME/.config/conky/itl/raw2}#
${goto 776}${exec sed -n '8p' $HOME/.config/conky/itl/raw2}#
${goto 873}${exec sed -n '9p' $HOME/.config/conky/itl/raw2}#

and i want to move "${goto nn}" up 1 line, discarding the first one (or, optionally, move the first one to the last position)

edit: the numeric values after goto repeat in this way several times throughout the file. i want to change them all in the described way. also, the config file contains other stuff before and after.

since i'm just getting aquainted with sed i would like to solve it with sed but it's not a must as long as it can go into a linux shell script.

edit2: i hope it's not confusing that the config file contains sed commands, too. that has nothing to do with the solution i'm looking for.


Solution

  • This might work for you (GNU sed):

    sed '$!N;s/^\(${goto [0-9]\+}\)\(.*\n\)\(${goto [0-9]\+}\)/\3\2\1/;P;D' file
    
    • $!N for all but the last line append a newline and the following line to the pattern space
    • s/^\(${goto [0-9]\+}\)\(.*\n\)\(${goto [0-9]\+}\)/\3\2\1/ for two consecutive lines beginning with the expression ${goto [0-9]\+} where [0-9]\+ means one or more digits, make a note of these strings and the intervening string and swap them around.
    • P;D print upto the first newline \n and then delete upto and including the newline. If the pattern space is not empty do not automatically read the next line but do begin a new cycle starting with the first command (which then appends a newline...).