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.

관련 정보