Except Script to read data from file


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Except Script to read data from file
# 1  
Old 09-22-2015
Linux Except Script to read data from file

Guys,

I ma beginner to shell scripting.I am looking for a script, "read data from file & pass it on bash script output."
Looking forward for help on this.
# 2  
Old 09-22-2015
Hi Geekris and welcome to the forums.

Any attempts from your side yet?
Where are you stuck?

Example:
We are stuck to help you since:
  1. We dont know what data
  2. from what file
  3. to which script

Have a good day.

EDIT:
But to get you started, here are some samples

Code:
VAR="this is a variable"
echo "$VAR"

echo "This you dont see" >/dev/zero
echo "This is in a file" > a_file.txt
echo "This is the next line of a file" >> a_file.txt

while read line
do
    echo "Reading : $line"
done<a_file.txt

Have fun playing.
# 3  
Old 09-23-2015
Code:
FILENAME="file.txt"
#while loop 
while read line; do
        echo "each line $line"
done < $FILENAME


##For Loop 
for line in (cat $FILENAME);do
        echo "each line $line"
done

Source from : Different ways to read file from file in shell script
# 4  
Old 09-23-2015
This looks like homework ( class work...) .. Is it so?
# 5  
Old 09-23-2015
Quote:
Originally Posted by kidSandy
Code:
FILENAME="file.txt"
#while loop 
while read line; do
        echo "each line $line"
done < $FILENAME


##For Loop 
for line in (cat $FILENAME);do
        echo "each line $line"
done

Source from : Different ways to read file from file in shell script
Note that even if you correct the syntax in the 2nd example above to use:
Code:
for line in $(cat $FILENAME);do

instead of:
Code:
for line in (cat $FILENAME);do

the output from those two ways to read lines produce VERY different results (unless the file named file.txt contains no spaces or tabs between "words" on a line and contains no blank lines).

Compare the output the above while and for loops produce if that file contains the text:
Code:
Is this one line or seven lines?
   

			
Were the blank lines above preserved?

# 6  
Old 09-23-2015
Yep, you are correct, i missed $ in the example Smilie .

here is the corrections for 2nd example
Code:
IFS=$'\n'  ##blank lines preserved?
for line in $(cat $FILENAME);do
        echo "each line $line"
done

Nice Catch Don, thank you so much
# 7  
Old 09-23-2015
The loop probably doesn't do what you think it does anyway.

'for' does not loop on lines, it loops on words. If any of your lines have spaces in them, it will not be lines. To read lines:

Code:
while read line
do
        echo "line was $line"
done < inputfile

 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

In PErl script: need to read the data one file and generate multiple files based on the data

We have the data looks like below in a log file. I want to generat files based on the string between two hash(#) symbol like below Source: #ext1#test1.tale2 drop #ext1#test11.tale21 drop #ext1#test123.tale21 drop #ext2#test1.tale21 drop #ext2#test12.tale21 drop #ext3#test11.tale21 drop... (5 Replies)
Discussion started by: Sanjeev G
5 Replies

2. Shell Programming and Scripting

How to get the shell script to read the .txt file as an input/data?

i have written my shell script in notepad however i am struggling to pass the data file to be read to the script the data file is of .txt format. My target is to run the shell script from the terminal and pass 3 arguments e.g. polg@DESKTOP-BVPDC5C:~/CS1420/coursework$ bash valsplit.sh input.txt... (11 Replies)
Discussion started by: Gurdza32
11 Replies

3. UNIX for Dummies Questions & Answers

Shell script to read lines in a text file and filter user data Shell Programming and Scripting

sxsaaas (3 Replies)
Discussion started by: VikrantD
3 Replies

4. Shell Programming and Scripting

How to read file and load data into a script as variable

I need to read a text file that contain columns of data, i need to read 1st column as a function to call, and others are the data i need to get into a ksh script. I am quite new to ksh scripting, i am not very sure how to read each row line by line and the data in each columns of that line, set... (3 Replies)
Discussion started by: gavin_L
3 Replies

5. Shell Programming and Scripting

Script to read file and extract data by matching pattern

Hello, I have a file ( say file1) which has lines like below. xxxx:xxxx,yyyy,1234,efgh zzzz:zzzz,kkkk,pppp,1234,xxxx,uuuu,oooo dddd:dddd here the word before ":" ( ie: xxxx) is the file name and the string after : are also file names, but each file name separated by "," In case of... (20 Replies)
Discussion started by: pradeepmacha
20 Replies

6. Shell Programming and Scripting

Read data from .csv file through shell script & modify

I need to read data from a file called "test.csv" through shell script where the file contains values like name,price,descriptor etc. There are rows where descriptor (& in some rows name) are written as string & other characters like "car_+" OR "bike*" etc where it should contains strings like... (3 Replies)
Discussion started by: raj100
3 Replies

7. Shell Programming and Scripting

How to read the data from the text file in shell script?

I am having one text file and i need to read that data from my shell script. I will expain you the scenario: Script look like: For name type 1: For age type 2: For Salary type3: echo "Enter the input:" read the data if input is 1 then go to the Text file and print the... (2 Replies)
Discussion started by: dineshmurs
2 Replies

8. Shell Programming and Scripting

Shell script to read lines in a text file and filter user data

hi all, I have this file with some user data. example: $cat myfile.txt FName|LName|Gender|Company|Branch|Bday|Salary|Age aaaa|bbbb|male|cccc|dddd|19900814|15000|20| eeee|asdg|male|gggg|ksgu|19911216||| aara|bdbm|male|kkkk|acke|19931018||23| asad|kfjg|male|kkkc|gkgg|19921213|14000|24|... (4 Replies)
Discussion started by: srimal
4 Replies

9. Shell Programming and Scripting

shell script to read data from text file and to load it into a table in TOAD

Hi....can you guys help me out in this script?? Below is a portion text file and it contains these: GEF001 000093625 MKL002510 000001 000000 000000 000000 000000 000000 000001 GEF001 000093625 MKL003604 000001 000000 000000 000000 000000 000000 000001 GEF001 000093625 MKL005675 000001... (1 Reply)
Discussion started by: pallavishetty
1 Replies

10. Shell Programming and Scripting

Help required with a Csh script to read data from a file

Dears, This is what i want.. I need to read a comma separated text file whose name is config.txt. whose content is like ; bscnara,btserrr bscsana,btssanacity ..... i need to read the first string and second string and use it to execute a another shell script. This is the logic. ... (1 Reply)
Discussion started by: fizzme
1 Replies
Login or Register to Ask a Question