Получение функциональности pushd, подобной tcsh dextract, в bash

Для прямого обращения к 4-му столбцу используйте следующийawkподход:

awk '{ sub(/^POS.*GINFO=/, "", $4) }1' file > new_file
  • $4-указывает на 4-е поле
  • sub(/^POS.*GINFO=/, "", $4)-заменяет подстроку, заданную шаблоном /^POS.*GINFO=, в 4-м поле
1
04.04.2020, 21:42
1 ответ

После некоторой работы и вдохновения я решил свою проблему:

# dextract-like pushd -- push $1'st directory to top of stack and shift rest down
function pushd {
    local t
    if [[ $1 =~ ^[-+]?[0-9]+$ ]]; then
        pos=$1
        [[ ${pos} -lt 0 ]] && ((pos=${#DIRSTACK[@]}+${pos}))
        if [[ ${pos} -gt 0 && ${pos} -lt ${#DIRSTACK[@]} ]]; then
            t="${DIRSTACK[$pos]}"
            # This is crufty. You can't put ~ in DIRSTACK, except the
            # shell will put it into position zero when it refers to your
            # own directory (not anyone else's!). So replace any occurrences.
            DIRSTACK=("" "${DIRSTACK[0]/#~/${HOME}}" "${DIRSTACK[@]:1:${pos}-1}" "${DIRSTACK[@]:${pos}+1}")
            cd "$t"
            builtin dirs -v
        else
            echo "$0: pushd: $1: directory stack index out of range" 1>&2
        fi
    else
        builtin pushd "$@" >/dev/null || return
        cd.
        builtin dirs -v
    fi
}
1
28.04.2021, 23:18

Теги

Похожие вопросы