Enter an input and reference another line in ksh script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Enter an input and reference another line in ksh script
# 1  
Old 11-11-2011
Enter an input and reference another line in ksh script

Hi

I have a file like so:

Code:
Frank Peter Tony Robert Mike 1 2 3 4 5 5 4 2 3 1 4 3 1 5 2
My out should look like this:

Peter
Tony
Mike

and so on....

I have the first part done to ask the user to enter the number
Code:
echo -n 'Enter number:' ; read num
if [[ $num = "" ]];then
echo " Missing field "
exit 1
else

Thanks in advance
# 2  
Old 11-12-2011
Can you explain you question? It is not clear to me what you want
# 3  
Old 11-12-2011
My file is messed up in the previous thread sorry. The file looks like

Code:
Peter   Tony    Frank   Mike     Robert
1          2         3           4           5
5          4         1           2           3
4          3         2           5           1

So if the user inputs a 2 the following names show up

Tony
Frank
Mike

and so on for the other numbers...
# 4  
Old 11-12-2011
I still dont get it, how is 2 related to your names?
# 5  
Old 11-12-2011
Well I'm sure there is an awk statement that can parse this file and if it finds 2 (example) it looks at the first row and reports Tony...then moves over see2 another 2 and looks at the top of the row and finds Frank....and so on....I'm not an advanced awk person or perl, I don't know all the switches that can do this...Maybe perl can also do it..
# 6  
Old 11-12-2011
Well this will get you close

The order of the output isn't the same...

Code:
/home/brad/unix.com/requests/2 >cat x
#! /bin/bash

awk -v t=$1 ' 
	$0 ~ /[A-Za-z]/ { split($0, names); next;}
	{ 
		split($0, numbers);
		for(i in numbers)
			if ( $(i) == t)
				print names[i];
} ' input.txt

Output: -

Code:
/home/brad/unix.com/requests/2 >./x 2
Tony
Mike
Frank
/home/brad/unix.com/requests/2 >./x 5
Robert
Peter
Mike

Input file: -

Quote:
/home/brad/unix.com/requests/2 >cat input.txt
Peter Tony Frank Mike Robert
1 2 3 4 5
5 4 1 2 3
4 3 2 5 1
# 7  
Old 11-14-2011
Hi

Thanks steadyonabix I appreciate your help however I noticed that if my numbers are 6 digit long this does not work. I can't seem to figure it out...Example 335191or 336455 You can this as an example

Code:
Tony                    Frank                   Peter                   Steve                   John              Tom
330459                330384                335191               337060               336455         335177   
336286                                            330068               331129               333023         334812
331439                                            336140               335191               334912
                                                                                                              330119
                                                                                                              332027
                                                                                                              336341

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Automatically enter input in command line

Hi, This is a script which to create an opvn user, I want which answer automatically to a certain part so, I try this, it works without the red part but I must type manually.. : #!/bin/bash ## Environnement ## LC_ALL=C ## Paths ## rsa_dir="etc/openvpn/easy-rsa"... (10 Replies)
Discussion started by: Arnaudh78
10 Replies

2. Shell Programming and Scripting

Input password to bash script, save, and enter when needed

I am looking for a way to start a script and have it prompt for a password that will be used later on in the script to SSH to another host and to SFTP. I don't want the password to be hard coded. Below is my script with the actual IP's and usernames removed. #!/usr/bin/expect -f... (2 Replies)
Discussion started by: jbrass
2 Replies

3. Shell Programming and Scripting

Loop logic, enter into respective IF as per enter input file name

have three big data file, however I just need to see the mentioned below one line form the all the file which has SERVER_CONNECTION Value File 1 export SERVER_CONNECTION=//dvlna002:10001/SmartServer File2 export SERVER_CONNECTION=///SmartServer File3 export... (1 Reply)
Discussion started by: Nsharma3006
1 Replies

4. Shell Programming and Scripting

function terminating if i give input as space or no input and enter

HI i have written a script to ask input from the user. this script should promote the user for y/n input. if user enters anyother input then y/n the script promotes him again. this below code is working fine for all the cases. except for space and enter " if i give space and enter it is... (2 Replies)
Discussion started by: BHASKARREDDY006
2 Replies

5. Shell Programming and Scripting

KSH - type ENTER to continue

Hello, Is ther any way to run a loop which promt an echo"waiting for user to hit ENTER" sleeps for an iterval and once user hit the ENTER key the loop exit and the script continue to the next stage? I need this ASAP !!! Thanks!! (9 Replies)
Discussion started by: LiorAmitai
9 Replies

6. Shell Programming and Scripting

using read to enter the input at runtime

Hi I am stucked in the below script .I want to input with yes/no from the user and then execute the code inside if but it is not working .I just need the logic as where I am wrong so that i can use the same in my work . then echo "Hi All" fi ]. Please suugest . (4 Replies)
Discussion started by: mani_isha
4 Replies

7. Shell Programming and Scripting

how to input answer (enter) if output contains a string?

how to wrote a script that reads an input from the reader (dir name) and then answer yes to all questions in the script unless the answer to any of the questions contains a certain string? example: $] script.sh dir_name $] question_1: (answer should be y right after the question is echoed,... (3 Replies)
Discussion started by: faizlo
3 Replies

8. Shell Programming and Scripting

Unzip the input file using shell script (ksh)

Hi, I need help in unziping input file through shell script. I had written script, which checks for input file extention. If Extension is "zip" or "gz", then I want to do unzip/uncompress that file. Caould you please let me know that, How to unzip a file through shell script (ksh). Thanks... (16 Replies)
Discussion started by: Poonamol
16 Replies

9. Shell Programming and Scripting

ksh: cmd output to input of another script

I want to write a script in KSH that takes the output of one command and redisplays it. Something like: while true do read inpt date +"%I:%M:%S %p <-> $inpt" done and then some how get the output of the ping command to redirect to the input of this script. does that make sense? (2 Replies)
Discussion started by: IMTheNachoMan
2 Replies

10. Shell Programming and Scripting

Shell Script Menus - Rejecting invalid input (KSH)

Greetings all, I'm currently writing a shell script menu which is dynamically populated from an array. Have a question to ask about the filtering of invalid input. I'm using KSH. A brief description of my algorithm is as follows: 1) Read in input from user and store in a variable. (a valid... (2 Replies)
Discussion started by: rockysfr
2 Replies
Login or Register to Ask a Question