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
CTL(5)                                              CTL file format of ASIM/LIP6/CAO-VLSI lab.                                              CTL(5)

NAME
ctl - Control Temporal Logic file format. ORIGIN
This software belongs to the ALLIANCE CAD SYSTEM developed by the ASIM team at LIP6 laboratory of Universite Pierre et Marie CURIE, in Paris, France. Web : http://asim.lip6.fr/recherche/alliance/ E-mail : alliance-users@asim.lip6.fr DESCRIPTION
This document describes the CTL file format used by moka(1) for model checking of finite states machine description. This CTL file format subset is defined to enable classical CTL formulae description. A CTL file is made of two parts: a declaration part and a formulae statement part. The declaration part described types, constants, macros and all variables used in CTL formulae. It also describes assumption conditions and initial conditions that have to be applied by moka(1) during the model checking. The formulae statement part described all the CTL formulae that have to be verified. All boolean and relational VHDL operators are supported (see vbe(5)) and also the 8 CTL operators AF, AG, AX, AU, EF, EG, EX and EU. The CTL file format support also the imply boolean operator '->' and the equivalence operator '<=>'. EXAMPLE
-- user type definition TYPE A_ETAT_TYPE IS (A_E0, A_E1); TYPE B_ETAT_TYPE IS (B_E0, B_E1); -- variables definition VARIABLE A_NS, A_CS : A_ETAT_TYPE; VARIABLE B_NS, B_CS : B_ETAT_TYPE; VARIABLE ck : BIT; VARIABLE data_in : BIT; VARIABLE data_out : BIT; VARIABLE reset : BIT; VARIABLE ack : BIT; VARIABLE req : BIT; -- example of a macros definition DEFINE def1 : BOOLEAN := ack='1'; -- the assigned value can be a constant DEFINE c1 : BIT := '1'; -- the assumption condition ASSUME ass1 := (reset='0'); -- the initial reset condition -- be careful, the assumption condition is not applied -- to the initial conditions. RESET_COND init1 := (reset='1'); -- It is also possible to describe the first state -- with the INITIAL keywork, as follows: -- -- INITIAL init1 := ((A_CS=A_E0) AND (B_CS=B_E0)); -- -- formulae description statement part begin prop1 : EX( ack='1' ); prop2 : AG( req -> AF( ack ) ); prop4 : AU( req='1', ack='1'); end; SEE ALSO
moka(1) BUG REPORT
This tool is under development at the ASIM department of the LIP6 laboratory. We need your feedback to improve documentation and tools. ASIM
/LIP6 August 5, 2002 CTL(5)
All times are GMT -4. The time now is 11:29 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy