3

Can someone tell me how to calculate the line number of a selected item. I would like to have the specific line number to refer to a subroutine that will process the same line of a different file.

#! /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" 

The desired output is:

You selected Peaches which is in line number: 2

Update:

This is an example of the items that are read. I used the fruit in the script above to illustrate a line example. This is an example of the specific items. As you can see some of the actual text is duplicated but on a different line. When the user select an item, I'm hoping that Zenity has an option to show which line was clicked. Every time it's run it will have a different list of items.

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

This is the exact block that I have for calling the above data:

#!/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"]
L. D. James
  • 1,584
  • 2
  • 11
  • 19
  • Let us [continue this discussion in chat](http://chat.stackexchange.com/rooms/49804/discussion-between-l-d-james-and-don-crissti). – L. D. James Dec 08 '16 at 21:06

1 Answers1

3

This worked for me with yad and zenity, and column id is not visible in GUI:

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

Now, to achieve the same when input is a file you could pre-process the file with awk e.g.
awk '{print NR};1' infile and pass the result to zenity.
Since, per the documentation:

Zenity returns the entries in the first column of text of selected rows to standard output.

your $item will only store the line no.(which is the entry in the 1st column), not the line content.
To get the line content you have to process the file again and extract that line based on the line number. So

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)

then

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

So now you have both the number of the selected line and its content saved into linenumber and respectively linecontent.

don_crissti
  • 79,330
  • 30
  • 216
  • 245
George Vasiliou
  • 7,803
  • 3
  • 18
  • 42