Zenityで選択したプロジェクトの行番号を取得する方法

Zenityで選択したプロジェクトの行番号を取得する方法

誰かが選択した項目の行番号を計算する方法を教えてもらえますか?別のファイルの同じ行を処理するサブルーチンを参照するために、特定の行番号が必要です。

#! /bin/bash

item=$(zenity --list "Apples" "Peaches" "Pumpkin" "Pie" \
--column="Select your choice" --text="Text above column(s)" --title="My menu")

linenumber=x # Formula to calculate the line number of the selected item here

echo "You selected: $item which is in line number: $linenumber" 

希望の出力は次のとおりです。

You selected Peaches which is in line number: 2

修正する:

以下は、読み取った項目の例です。私は行の例を説明するために上記のスクリプトで果物を使用しました。以下は、特定のプロジェクトの例です。ご覧のとおり、実際のテキストの一部は繰り返されますが、別の行にあります。欲しい商品をユーザーが選んだときジェニティクリックした行を表示するオプションがあります。実行するたびにアイテムのリストが異なります。

cairo-dock
Desktop
XdndCollectionWindowImp
unity-launcher
unity-panel
unity-panel
unity-dash
Hud
Your turn - Play esskwa003 in HneO9CtF • lichess.org - Google Chrome
ljames@ubunzeus
ljames@ubuntuserver
ljames@hera5
site
site
ljames@ubunzeus
launcher - Add Unity Entry for Locally Installed Program - Ask Ubuntu - Google Chrome
ljames@ubunzeus
eclipse desktop launcher categories - Google Search - Google Chrome
launcher - Add Unity Entry for Locally Installed Program - Ask Ubuntu - Google Chrome
eclipse
MightyText - Google Chrome
launcher - Add Unity Entry for Locally Installed Program - Ask Ubuntu - Google Chrome
ljames@ubunzeus
Inbox - L. D. James - Mozilla Thunderbird
ljames@hera5
ljames@hera5
ljames@ubunzeus
ljames@hera5
How to get the line number of a Zenity selected Item - Unix & Linux Stack Exchange - Google Chrome
workspace - MyPyDev - ShellTools/SEWork/SEWork/hkrecord.sh - Eclipse - /home/users/l/j/ljames/workspace 
email - Mozilla Thunderbird
command line - Is it possible to control the recording if Audacity is running in the background? - Ask Ubuntu - Google Chrome
Bookmark Manager - Google Chrome
Formatting Sandbox - Meta Stack Exchange - Google Chrome
Apollo III Support - Backing up the Office Computer - Mozilla Thunderbird

これは、上記のデータを呼び出すために使用する正確なブロックです。

#!/bin/bash

INPUT=$HOME/infile.txt
# IFS=$'\n'
item=$(while read l
do
    echo "$l"
done <$INPUT|zenity --list --text "sample text " --column "Choose") 
echo "You selected: [$item] which is in line number: [$linenumber"]

ベストアンサー1

これはyadとzenityで私に効果的で、列IDはGUIに表示されませんでした。

zenity --list 1 "Apples" 2 "Peaches" 3 "Pumpkin" 4 "Pie" --column="id" \
--column="Select your choice" --hide-column=1 --print-column=1

awk入力がファイルの場合、同じ効果を得るために、例えばファイルを前処理し、
awk '{print NR};1' infile結果をzenity
文書:

Zenityは、選択した行のテキストの最初の列にある項目を標準出力に返します。

あなたの$item願い行番号のみ保存(最初の列の項目)、行の内容ではありません。
行の内容を取得するには、ファイルを再処理し、行番号に基づいて行を抽出する必要があります。だから

linenumber=$(awk '{print NR};1' infile | zenity --list --column="No" \
--column="Select your choice" --text="Text above column(s)" \
--title="My menu" --hide-column=1)

それから

linecontent=$(sed ${linenumber}'!d;q' infile)

これで、選択した行の番号とその内容をlinenumberそれぞれおよびに保存しましたlinecontent

おすすめ記事