Sponsored Content
Top Forums Shell Programming and Scripting Read a file and save every word in a variable to use Post 303033852 by wisecracker on Friday 12th of April 2019 01:55:40 PM
Old 04-12-2019
Hi azaiie...

This sounds easy, but is much more difficult than you think using a fully POSIX compliant shell.
This is fully POSIX compliant and gets 39 individual words into individual variables just as you wanted.
Arrays per-se do not exist in POSIX but this creates a pseudo-array.

With a random file of words these variables have to be searched and the value used as the new variable with a relevant integer assigned to it.
Lots of conditional tasks as well as variable searching.

The rest should be easy.

Code:
#!/bin/sh

# #!/usr/local/bin/dash
# Tested using dash as the shell.

# Generate a file containing required words.
echo 'one two
buckle my shoe
three four
knock at the door
five six
pick up sticks
seven eight
lay them straight
nine zero
a big fat hero
multiply add
and maybe to subtract
divide too
just to satisfy you' > /tmp/txt

# Convert basic text file to single word lines.
tr ' ' '\n' < /tmp/txt > /tmp/text

# *******************************************
# **** The main pseudo-array generator. *****
INDEX=1
while read -r ARRAY
do
    eval MY_ARRAY${INDEX}='${ARRAY}'
    INDEX=$(( INDEX+1 ))
done < /tmp/text
# You now have a number of variables containing
# 1 word in each. The above is POSIX compliant.
# *******************************************

# Check the environment to see if the variables are there.
set

And the results, viewing the environment variables.
Code:
Last login: Fri Apr 12 18:47:43 on ttys000
AMIGA:amiga~> cd Desktop/Code/Shell
AMIGA:amiga~/Desktop/Code/Shell> ./words1.sh
ARRAY=
Apple_PubSub_Socket_Render=/private/tmp/com.apple.launchd.OnpUkj5yS0/Render
BASH=/bin/sh
BASH_ARGC=()
BASH_ARGV=()
BASH_LINENO=([0]="0")
BASH_SOURCE=([0]="./words1.sh")
BASH_VERSINFO=([0]="3" [1]="2" [2]="57" [3]="1" [4]="release" [5]="x86_64-apple-darwin18")
BASH_VERSION='3.2.57(1)-release'
DIRSTACK=()
EUID=502
GROUPS=()
HOME=/Users/amiga
HOSTNAME=Barrys-MBP
HOSTTYPE=x86_64
IFS='     
'
INDEX=40
LANG=en_GB.UTF-8
LOGNAME=amiga
MACHTYPE=x86_64-apple-darwin18
MY_ARRAY1=one
MY_ARRAY10=the
MY_ARRAY11=door
MY_ARRAY12=five
MY_ARRAY13=six
MY_ARRAY14=pick
MY_ARRAY15=up
MY_ARRAY16=sticks
MY_ARRAY17=seven
MY_ARRAY18=eight
MY_ARRAY19=lay
MY_ARRAY2=two
MY_ARRAY20=them
MY_ARRAY21=straight
MY_ARRAY22=nine
MY_ARRAY23=zero
MY_ARRAY24=a
MY_ARRAY25=big
MY_ARRAY26=fat
MY_ARRAY27=hero
MY_ARRAY28=multiply
MY_ARRAY29=add
MY_ARRAY3=buckle
MY_ARRAY30=and
MY_ARRAY31=maybe
MY_ARRAY32=to
MY_ARRAY33=subtract
MY_ARRAY34=divide
MY_ARRAY35=too
MY_ARRAY36=just
MY_ARRAY37=to
MY_ARRAY38=satisfy
MY_ARRAY39=you
MY_ARRAY4=my
MY_ARRAY5=shoe
MY_ARRAY6=three
MY_ARRAY7=four
MY_ARRAY8=knock
MY_ARRAY9=at
OPTERR=1
OPTIND=1
OSTYPE=darwin18
PATH=/Library/Frameworks/Python.framework/Versions/3.5/bin:/Users/amiga/Programs:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin
PIPESTATUS=([0]="1")
POSIXLY_CORRECT=y
PPID=1363
PS4='+ '
PWD=/Users/amiga/Desktop/Code/Shell
SHELL=/bin/bash
SHELLOPTS=braceexpand:hashall:interactive-comments:posix
SHLVL=2
SSH_AUTH_SOCK=/private/tmp/com.apple.launchd.a4cnRI4Q71/Listeners
TERM=xterm-256color
TERM_PROGRAM=Apple_Terminal
TERM_PROGRAM_VERSION=421.1
TERM_SESSION_ID=C5501462-11A6-4B33-AC5B-14C026610778
TMPDIR=/var/folders/9c/bb_c667j5nj5cbbkw__1l8n40000gp/T/
UID=502
USER=amiga
XPC_FLAGS=0x0
XPC_SERVICE_NAME=0
_=ARRAY
AMIGA:amiga~/Desktop/Code/Shell> _

This User Gave Thanks to wisecracker For This Post:
 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Read file from within AWK and save $1 to a variable

Hi I am very new to NAWK programming so this question is probably going to sound really stupid: I have a NAWK script which contains a DO loop. During each loop it runs a FORTRAN program which in turn generates two output files , each one containing 2 integer variables. I would appreciate it... (8 Replies)
Discussion started by: robbiegregg
8 Replies

2. Shell Programming and Scripting

Read popup message and save it in file

Hi, I am trying to automate one of the application using IE:Auotmation in perl My web application has few text fields and 2 buttons "Save Changes" and "Discard Changes".I have written code to enter values to the text fields fetching from input file and click the button "Save Changes".As soon as... (0 Replies)
Discussion started by: jyo123.jyothi
0 Replies

3. Shell Programming and Scripting

To read data word by word from given file & storing in variables

File having data in following format : file name : file.txt -------------------- 111111;name1 222222;name2 333333;name3 I want to read this file so that I can split these into two paramaters i.e. 111111 & name1 into two different variables(say value1 & value2). i.e val1=11111 &... (2 Replies)
Discussion started by: sjoshi98
2 Replies

4. Shell Programming and Scripting

Read file and for each line replace two variables, add strings and save output in another file

Hi All, I have a file, let's call it "info.tmp" that contains data like this .. ABC123456 PCX333445 BCD789833 I need to read "info.tmp" and for each line add strings in a way that the final output is put /logs/ua/dummy.trigger 'AAA00001.FTP.XXX.BLA03A01.xxxxxx(+1)' where XXX... (5 Replies)
Discussion started by: Andy_ARG
5 Replies

5. Shell Programming and Scripting

cannot get to read from file and save to file

how can i get this script to read users input from file and save the users input if it is not in the file? #!/bin/sh echo -n "filename:" read filename for i in 1 2 3 4 5 do echo -n "Enter filename:" read lineoftext echo "$lineoftext" >> $filename done (1 Reply)
Discussion started by: noob
1 Replies

6. Shell Programming and Scripting

Read a file name from a text file and save it in a variable

i have a text file consists of different file names like: line 1: lib/libIMb.so message broker file line 2: lil/imbdfg.lil message broker file i need to extract libIMb.so and imbdfg.lil files from those lines and save them in a variable. so that i can search for... (9 Replies)
Discussion started by: santosh2626
9 Replies

7. UNIX for Advanced & Expert Users

How to save a data of a file into a variable?

My requirement is to read a column data from a file & save it in a variable for each row & process it. I am using the below code- Leadlines="$TGTFILE/Error.txt">>$log_file while read line do id = ` echo $line | cut -d "," -f1 ` email = ` echo $line | cut -d "," -f2 ` ----------- done My... (2 Replies)
Discussion started by: saga20
2 Replies

8. Shell Programming and Scripting

Read a File line by line and split into array word by word

Hi All, Hope you guys had a wonderful weekend I have a scenario where in which I have to read a file line by line and check for few words before redirecting to a file I have searched the forum but,either those answers dint work (perhaps because of my wrong under standing of how IFS... (6 Replies)
Discussion started by: Kingcobra
6 Replies

9. UNIX for Dummies Questions & Answers

To count total of specific character in a file and save its value to a variable

Hi all, I have a file that contains characters. How do I get total of spesific character from that file and save the count to a variable for doing for calculation. data.txt 1 2 2 2 2 3 3 4 5 6 7 8 5 4 3 4 (5 Replies)
Discussion started by: weslyarfan
5 Replies

10. Shell Programming and Scripting

Find a word and increment the number in the word & save into new files

Hi All, I am looking for a perl/awk/sed command to auto-increment the numbers line in file, P1.tcl: run_build_model sparc_ifu_dec run_drc set_faults -model path_delay -atpg_effectiveness -fault_coverage add_delay_paths P1 set_atpg -abort_limit 1000 run_atpg -ndetects 1000 I would like... (6 Replies)
Discussion started by: jypark22
6 Replies
All times are GMT -4. The time now is 02:15 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy