Sponsored Content
Top Forums Shell Programming and Scripting Number lines of file and assign variable to each number Post 302390043 by TonyFullerMalv on Tuesday 26th of January 2010 06:04:05 PM
Old 01-26-2010
Reading every line in to variables isn't necessary, how about:
Code:
$ cat numbered_file.sh
echo -n "input a number: "
read RESP

VARIABLE=`grep "^${RESP} " numbered_file`

echo "VARIABLE = ${VARIABLE}."
$

Note ^ = beginning of line and space after ${RESP} so when you enter 3 you do not also get lines 30, 31, 32, ..., 300, etc.
Run of script using your example 3 line input file:
Code:
$ ./numbered_file.sh
input a number: 3
VARIABLE = 3 more stuff.
$./numbered_file.sh
input a number: 1
VARIABLE = 1 some data.
$ ./numbered_file.sh
input a number: 2
VARIABLE = 2 something else.
$

Is that what you are after?

---------- Post updated at 23:04 ---------- Previous update was at 22:36 ----------

Otherwise you could do this:

Code:
$ cat ./numbered_file_array_select.sh

# Load array:
ARRAYPTR=0
while read LINE; do
  ARRAY_ENTRY=`echo ${LINE} | awk '{ print $1 }'`
  ARRAY_LINE=`echo ${LINE} | sed -e 's/^'${ARRAY_ENTRY}' //'`
  ARRAYPTR=`expr ${ARRAYPTR} + 1`
  ARRAYSTORE[${ARRAY_ENTRY}]=${ARRAY_LINE}
done < numbered_file

# Search Array:
echo -n "Input a number up to ${ARRAYPTR}: "
read RESP
VARIABLE=`echo ${ARRAYSTORE[${RESP}]}`

echo "VARIABLE = ${VARIABLE}."
$

Run through:
Code:
$ ./numbered_file_array_select.sh
Input a number up to 3: 2
VARIABLE = something else.
$ ./numbered_file_array_select.sh
Input a number up to 3: 1
VARIABLE = some data.
$ ./numbered_file_array_select.sh
Input a number up to 3: 3
VARIABLE = more stuff.

 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Join in a single line variable number of lines

Hi all, I have a file with little blocks beginning with a number 761XXXXXX, and 0, 1, 2 or 3 lines below of it beginning with STUS as follow: 761625820 STUS ACTIVE 16778294 STUS NOT ACTIVE 761157389 STUS ACTIVE 16778294 761554921 STUS ACTIVE 16778294 STUS NOT ACTIVE STUS ACTIVE OP... (4 Replies)
Discussion started by: cgkmal
4 Replies

2. Shell Programming and Scripting

count the number of lines that start with the number

I have a file with contents similar to this. abcd 1234 4567 7666 jdjdjd 89289 9382 92 jksdj 9823 298 I want to write a shell script which count the number of lines that start with the number (disregard the lines starting with alphabets) (1 Reply)
Discussion started by: grajp002
1 Replies

3. Shell Programming and Scripting

Get letter from number and assign to a variable

Hi to all in forum, I'm trying to convert the letter number between 1 (A) and 26 (Z), that part is working, my issue is how to assign the printf output to a variable:LetterNumber=10 printf "\x$(printf %x $((${LetterNumber}+64)))" $ J #The problem, how to assign printf output (J in this... (8 Replies)
Discussion started by: Ophiuchus
8 Replies

4. Shell Programming and Scripting

Search and replaces lines with a variable number

Helo, I have a kml file with 39.000 lines. Need search text and add a sequential number before text. Original file <Placemark> <name>POI</name> <styleUrl>#waypoint</styleUrl> <Point> <coordinates>2.104510,41.341900</coordinates> </Point> ... (3 Replies)
Discussion started by: saba01
3 Replies

5. Shell Programming and Scripting

How to assign a shell variable to a NUMBER parameter in pl/sql?

I have the below script running for generating file from PL/SQL stored procedure. I need to declare a shell variable and then pass this to sqlplus command to pass the same as a INPUT parameter for the stored procedure. Please help to do this. #!/bin/sh minlimit=0 maxlimit=10 size=100 while... (0 Replies)
Discussion started by: vel4ever
0 Replies

6. Shell Programming and Scripting

Insert a variable to a text file after fixed number of lines

Hi, I am new to unix. I need to insert a variable which contains some lines of text into a text file after fixed number of lines.. Please help me on this.. Thanks in Advance, Amrutha (3 Replies)
Discussion started by: amr89
3 Replies

7. Shell Programming and Scripting

Assign a variable to number of lines in a file

Hello Gurus, Here is my requirement. I need to find the number of lines in a file and need to assign it to a variable. This is what I did and not wroking. #!/bin/ksh set -xv Src_Path=/mac/dev/Generic/SrcFiles Src_Count=wc -l ${Src_Path}/FILE_JUNE.txt Count_file = $Src_Count | awk -F... (2 Replies)
Discussion started by: thummi9090
2 Replies

8. Shell Programming and Scripting

Grep lines for number greater than given number

Hello, I am newbie to bash scripting. Could someone help me with the following. I have log file with output as shown below **************************LOG************************* 11/20/2013 9:11:23.64 Pinging xx.xx.xx.xx with 32 bytes of data: 11/20/2013 9:11:23.64 Reply from xx.xx.xx.xx:... (4 Replies)
Discussion started by: meena_2013
4 Replies

9. Shell Programming and Scripting

Assign large number of blanks to a variable

I want to assign large number of blanks to a variable in Korn shell. If it is a small number it is fine like if I want to assign 3 blanks I would code var=" " But if it is a big number say 100 blanks, what is a better way? Ultimately I will use it in printf statement printf... (3 Replies)
Discussion started by: Soham
3 Replies

10. Shell Programming and Scripting

Assign number of records to a variable

How does one assign a variable, x to equal the number of records in a different file. I have a simple command such as below: awk -F "\t" '(NR>5) { if(($x == "0/0")) { print $0} }' a.txt > a1.txt but I want x to equal the number of records in a different file, b.txt (10 Replies)
Discussion started by: Geneanalyst
10 Replies
BLD.CONF(5)							File Formats Manual						       BLD.CONF(5)

NAME
bld.conf - Configuration file for BLD DESCRIPTION
bld.conf consists of lines used to set various configuration options for bld. The lines are processed in order of appearance. Options set via bld.conf are overriden by bld command line options. If two lines assign different values to the same variables, the last is used. Blank lines and lines beginning with a `#' are ignored. bld.conf lines have the following format: variable = value variable can be on of the following: o daemon: (0 or 1) fork to background or not. o address: IP address to bind to. o port: port to listen to. o min_interval: minimum time interval before blacklisting. o max_submissions: maximum submissions in time interval. o ip_list_size: IP list size. o blacklist_size: blacklist size. o blacklist_expiration: blacklist expiration (seconds). o log_level: log level. o user: user to run as. o group: group to run as. o client_timeout: client idle timeout before closing connection (seconds). o iplist_dump: file to dump IP list to. o blacklist_dump: file to dump blacklist to. o pid_filename: PID filename. o acl_filename: ACL filename. o whitelist_filename: whitelist filename. o notifies_to: comma separated hosts (`hostname' or `hostname:port') to notify when a new address is inserted into the blacklist NOTES
You need to completely restart bld after any modification of this file. FILES
/etc/bld/bld.conf SEE ALSO
bld(8) bld_acl.conf(5) bld_whitelist.conf(5) AUTHOR
Olivier Beyssac <obld@r14.freenix.org> October 2004 BLD.CONF(5)
All times are GMT -4. The time now is 08:27 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy