You are not logged in.

#2476 2014-10-21 12:31:57

parchd
Member
Registered: 2014-03-08
Posts: 78

Re: Post your handy self made command line utilities

I haven't tried it yet, I'm supposed to be working too and the pdfs I had problems with are already converted.

Offline

#2477 2014-10-21 12:35:06

skanky
Member
From: WAIS
Registered: 2009-10-23
Posts: 1,734

Re: Post your handy self made command line utilities

parchd wrote:

I haven't tried it yet, I'm supposed to be working too and the pdfs I had problems with are already converted.

Okay, my thinking is that you'll still need the intermediate step as sed will try each command on every line - though address patterns may help. I think there's a command to skip to the next line, but can't remember it. So I can possibly think of 2 or 3 ways to get round it, but as I say I don't have time. I'll have a crack later, if I get a chance - it's been a while since I needed to do any sed, and I need a refresher.


"...one cannot be angry when one looks at a penguin."  - John Ruskin
"If I had a pound for every time someone told me that I could monetise something..." - Ed Reardon

Offline

#2478 2014-10-21 13:32:09

fsckd
Forum Moderator
Registered: 2009-06-15
Posts: 3,077

Re: Post your handy self made command line utilities

skanky wrote:

EDIT: sorry, I think I made a big mistake and rather than confuse people, it's better to delete what I wrote.

Now I have no idea what you guys are talking about. sad


aur S & M :: forum rules :: Community Ethos
Resources for Women, POC, LGBT*, and allies

Offline

#2479 2014-10-21 13:57:12

skanky
Member
From: WAIS
Registered: 2009-10-23
Posts: 1,734

Re: Post your handy self made command line utilities

fsckd wrote:
skanky wrote:

EDIT: sorry, I think I made a big mistake and rather than confuse people, it's better to delete what I wrote.

Now I have no idea what you guys are talking about. sad

Sorry, I did try to go back and undo that but had lost it - I was too busy really to try an answer, but quickly fired one off, then thought better of it, then changed my mind and the whole thing was a mess.

I thought the sed line could be shortened to:

sed -i 's/0 0 0 sc/1 1 1sc/g; s/1 1 1 sc/0 0 0 sc/g' /tmp/pdfinvtmp

But see my above post for why I think that won't work.


"...one cannot be angry when one looks at a penguin."  - John Ruskin
"If I had a pound for every time someone told me that I could monetise something..." - Ed Reardon

Offline

#2480 2014-10-21 16:57:25

skanky
Member
From: WAIS
Registered: 2009-10-23
Posts: 1,734

Re: Post your handy self made command line utilities

@parchd - right, I've had a chance to look at this properly. Sorry about the earlier debacle.

To summarise:

* You can amalgamate all the sed commands into one set of '...' (script).
* You can embed un-escaped new lines into the script, or use ;'s
* You are able to remove the intermediate step (see below)
* In GNU sed you can edit the tmp file in place

Here's the sed line taking all into account, and tested on a *very* simple test file:

sed 's/0 0 0 sc/1 1 1 sc/g;t; s/1 1 1 sc/0 0 0 sc/g' /tmp/pdfinvtmp

Note the "t" in there. That says that if the current line has been substituted, branch to the label, and if there is no label, to the end of the script.
Thus if we make a substitution on the current line we don't then substitute it back on the next command. If both settings are on the same line, it will fail to do the second substitution. I don't have pdftk installed so haven't checked. If that is the case, you could use one command to do both at the same time.

Again, sorry for the noise.


"...one cannot be angry when one looks at a penguin."  - John Ruskin
"If I had a pound for every time someone told me that I could monetise something..." - Ed Reardon

Offline

#2481 2014-10-30 13:00:49

andya
Member
Registered: 2014-10-20
Posts: 3

Re: Post your handy self made command line utilities

I wrote this bash / zsh script for mass file numbering:

#!/bin/sh
#########
##
##  Script for: Mass numbering of files
##
##  It SHOULD prevent files from being destroyed. Try at your own risk.
##  Use -t flag to just echo.
##  Use -b d to print dir name
##
#########

usage () {
          printf "     Usage:\n"
          printf "              \e[1;36mnumb -b photo *.*\e[0m\n"
          printf "                         ... will number all files in current dir Pics as photo_001, photo_002, preserving extensions\n"
          printf "              \e[1;36mnumb -c 12 -p 2 *.*\e[0m\n"    
          printf "                         ... will number all files in current dir Pics as Pics_12, Pics_13, preserving extensions\n"
	  }

PAD=3
COUNTER=1
RANGE=1

BASE=

while getopts "c::p::b::r::tsm" OPTIONS; do
    case "${OPTIONS}" in
        c)
            COUNTER=${OPTARG}
            ;;
        p)
            PAD=${OPTARG}
            ;;
        b)
	    if [ ${OPTARG} = d ]; then
		BASE=${PWD##*/}
	    else
	        BASE=${OPTARG}
            fi
	    ;;        
        r)  
            RANGE=${OPTARG}
            ;;
        t)  
            TRY=y
            ;;
	s)
	    SWITCH=y
	    ;;
	m)
	    MANTAIN=y
	    ;;
    esac
done
shift $((OPTIND-1))

if [ "$#" -lt 2 ]; then
   usage
   exit 1
fi

IFS=$'\n\t'
	
for OLDNAME in "$@"; do

    if [ -n "$SWITCH" ]; then
        NEWBASE=$(printf "%0${PAD}d_${BASE}" ${COUNTER})
    elif [ -z "$SWITCH" ]; then
	NEWBASE=$(printf "${BASE}_%0${PAD}d" ${COUNTER})
    fi

    if [ "$BASE" = "" ]; then
	NEWBASE=$(printf "%0${PAD}d" ${COUNTER})
    fi
	
    if [ -n "$MANTAIN" ]; then
        NEWNAME=${NEWBASE}_${OLDNAME}
    else
	NEWNAME=${NEWBASE}.${OLDNAME#*.}
    fi
	
    if [ -n "$TRY" ]; then
        echo "mv --backup=nil ${OLDNAME} ${NEWNAME}"
    else
        mv --backup=nil ${OLDNAME} ${NEWNAME}
    fi
    
    let COUNTER=COUNTER+${RANGE}
done

######
## TODO: 
##       !!!!  let it be recursive:
#              MAXDIRDEPTH=1
#              PAD=3
#           for PATHSUBDIR in $(find -maxdepth ${MAXDIRDEPTH} -type d \( ! -name '.*' \)); do
#               i=1;
#              for PATHFILE in $(find "${PATHSUBDIR}" -maxdepth 1 -type f -name '*.*' | sort); do
#              NEWBASE=$(printf "$(basename ${PATHFILE%/*})_%0${PAD}d" $i);
#                echo "mv --backup=nil ${PATHFILE} ${PATHFILE%/*}/${NEWBASE}.${PATHFILE#$PATHSUBDIR/*.}";
#              let i=i+1;
#              done;
#            done
##
##
##        !!!!  exit 1 when arguments are missing, etc
##        !!!!  update usage
######

Edit: removed bashisms (thanks @jasonwryan)
Edit 2: added options

Last edited by andya (2014-11-01 13:00:37)

Offline

#2482 2014-10-30 16:19:20

jasonwryan
Forum & Wiki Admin
From: .nz
Registered: 2009-05-09
Posts: 12,872
Website

Re: Post your handy self made command line utilities

You have a POSIX shebang, but are using bashisms (`echo -e`, `[[` and `$(..)`)...


Arch + dwm   •   Mercurial repos

Registered Linux User #482438

Offline

#2483 2014-10-30 16:41:31

steve___
Member
Registered: 2008-02-24
Posts: 435

Re: Post your handy self made command line utilities

Hey Jason, is "$(..)" not POSIX compliant?

Offline

#2484 2014-10-30 16:43:07

jasonwryan
Forum & Wiki Admin
From: .nz
Registered: 2009-05-09
Posts: 12,872
Website

Re: Post your handy self made command line utilities

steve___ wrote:

Hey Jason, is "$(..)" not POSIX compliant?

It is. My bad for posting this early... tongue


Arch + dwm   •   Mercurial repos

Registered Linux User #482438

Offline

#2485 2014-11-05 02:16:48

Xaero252
Member
Registered: 2011-11-28
Posts: 78

Re: Post your handy self made command line utilities

Here's a quick watchdog script I made to emulate the functionality of the "Borderless Gaming" app on Windows. Just add it to your session and create a ~/.fullscreen.lst file with the titles of games you wish to be borderless fullscreen'd; one per line:

#!/bin/bash
#"Borderless Fullscreen" automatic toggling script
#By Xaero252 @ OCN (overclock.net)
#Distributed with the "don't be a douche" license
#Just don't try and claim you thought this up or whatever

#First, let's start a container loop with a sleep command to keep it from eating CPU.
while true; do 
	sleep 1 #We'll use 1 second, we don't want users to wait forever.
	while read title; do
		if ! xprop -name "$title" 2>/dev/null | grep -q _NET_WM_STATE_FULLSCREEN; then
			wmctrl -r "$title" -b toggle,fullscreen
		fi
	done < ~/.fullscreen.lst #This way it's per-user.
done

The game must be run in Windowed mode for this to function as intended, but it fills the gap for a lot of games that don't have this functionality on Linux by default. (Stepmania, Minecraft, etc.)

Offline

#2486 Today 21:06:36

rwd
Member
Registered: 2009-02-08
Posts: 637

Re: Post your handy self made command line utilities

I tend to have a bunch of 'screen' sessions running on a headless system. When I want to resume the 3rd session this alias let's me do that with minimal typing:

scr 2

Without arguments it just lists active sessions.

function scr(){
    if [[ $# == 0  ]]
    then
        screen -list
    else
        var_screen_num="${1}"
        arr_screen_id=($(screen -list | grep -Pzo  '(\d+\.[a-z0-9-]+\.\S+)'))
        screen -r "${arr_screen_id[$var_screen_num]}"
    fi
}

Last edited by rwd (Today 21:07:56)

Online

Board footer

Powered by FluxBB