Inserting shell script input data automatically from a text file


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Inserting shell script input data automatically from a text file
# 1  
Old 01-23-2016
Linux Inserting shell script input data automatically from a text file

Dear experts,

I am new to linux programming. I have a shell script which i should run it on all my samples.
I only define input and out put for this script. The inputs are 3 numbers(coordination numbers) which are available in a series of text file.
Since i have a lots of samples, it takes a lot of time to manually insert the numbers to the script.
Is it possible to write a script which automatically insert the numbers from the text files to the script?

An example of my text fileSmiliein this example only 49 75 48 are used as inputs, so i need to extract them automatically from the text file and insert them as inputs to the script)

Code:
Cluster Index	Voxels	MAX	MAX X (vox)	MAX Y (vox)	MAX Z (vox)	COG X (vox)	COG Y (vox)	COG Z (vox)
15	17	5.07	49	75	48	48.1	73.5	47.4
14	16	5.52	42	80	50	41.3	79.7	49.7

I will be grateful for any help you can provide.

Best,
Mohamad

Last edited by Scrutinizer; 01-23-2016 at 04:18 PM.. Reason: CODE tags
# 2  
Old 01-23-2016
Try:
Code:
TAB=$(printf "\t")
while IFS=$TAB read ci voxels max max_x max_y max_z cog_x cog_y cog_z
do
  echo "$max_x, $max_y, $max_z"
done < infile


If you only need those the three values you could simplify like this :
Code:
TAB=$(printf "\t")
while IFS=$TAB read x x x max_x max_y max_z x
do
  echo "$max_x, $max_y, $max_z"
done < infile

where "x" contains dummy values..


--
In bash ksh93 and zsh you can use:
Code:
while IFS=$'\t' read ...

in other shells you can use a hard-TAB (entered through CTRL-V TAB)
This User Gave Thanks to Scrutinizer For This Post:
# 3  
Old 01-23-2016
Thanks for your prompt answer,i really really appreciate it.

Sorry, if I ask, but I don't know how to read the text file and also i don't know how to give the outputs to my script.
First i should copy your script to a text file, next i should make it executable and finally i should set the path,right?

i only need the first row parameters, does this scripts only echo values of max_x max_y and max_z in the first row?

my script is:

Code:
fslmaths InputImage -mul 0 -add 1 -roi max_x 1 max_y 1 max_z 1 0 1 outputName

(Inputimage and max_x, max_y and max_z are the inputs)

Since i am new to Linux programming, i will be grateful for your help with this situation

Best,
Mohamad

Last edited by Scrutinizer; 01-23-2016 at 09:04 PM.. Reason: code tags
# 4  
Old 01-23-2016
To read only the first row you can leave out the while loop, you could try something like:

Code:
TAB=$(printf "\t")
IFS=$TAB read x x x max_x max_y max_z x < input_file
fslmaths InputImage -mul 0 -add 1 -roi "$max_x" 1 "$max_y" 1 "$max_z" 1 0 1 outputName

This User Gave Thanks to Scrutinizer For This Post:
# 5  
Old 01-24-2016
I guess you should read and throw away the header line:
Code:
TAB=$(printf "\t")
{ read x; IFS=$TAB read x x x max_x max_y max_z x; } < input_file
fslmaths InputImage -mul 0 -add 1 -roi "$max_x" 1 "$max_y" 1 "$max_z" 1 0 1 outputName

These 2 Users Gave Thanks to RudiC For This Post:
# 6  
Old 01-24-2016
I appreciate the time you have put into detailing these requirements. Thank you.

Its working.

Best
Mohamad
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Need list of input and output parameter of task in a text file, using shell script

//file begin ===== //some code task abcd_; input x; input y,z; //some comment output w; //some comment reg p; integer q; begin //some code end endtask : abcd_ //some code //file end ===== expected output from above... (1 Reply)
Discussion started by: rishifrnds
1 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

Need help on inserting data from text file to excel using shell script

Hi, Please help me on this. I want to insert data from text file to excel using shell script nawk -v r=4 -v c=4 -v val=$a -F, 'BEGIN{OFS=","}; NR != r; NR == r {$c = val; print}' "file.csv" I used above one to insert $a value in 4th row, 4th column in an excel file.csv and it... (3 Replies)
Discussion started by: suman.frnz
3 Replies

5. Shell Programming and Scripting

The scope of the shell/perl script is to read the input text file. Validate the expiry date of each

The scope of the shell/perl script is to read the input text file. Validate the expiry date of each certificate and send the mail to the user. The user takes action to add the new certificate to the storage file and user owns the responsibility to update the input text file with the new certificate... (5 Replies)
Discussion started by: casmo
5 Replies

6. Shell Programming and Scripting

shell script to take input from a text file and perform check on each servers and copy files

HI all, I want to script where all the server names will be in a text file like server1 server2 server3 . and the script should take servernames from a text file and perform copy of files if the files are not present on those servers.after which it should take next servername till the end of... (0 Replies)
Discussion started by: joseph.dmello
0 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

how to use data in unix text file as input to an sql query from shell

Hi, I have data in my text file something like this. adams robert ahmed gibbs I want to use this data line by line as input to an sql query which i run by connecting to an oracle database from shell. If you have code for similar scenario , please ehlp. I want the output of the sql query... (7 Replies)
Discussion started by: rdhanek
7 Replies

9. Shell Programming and Scripting

Using Shell Script to get data from text file

Hello, I have got the script below that reads a text file and then extract data from the third column and then send the result to another shell scrpt call tsim - the script works fine. The problem I am having is that I want the user to enter the 'Test Day' which the first column in the text file,... (1 Reply)
Discussion started by: jermaine4ever
1 Replies

10. Shell Programming and Scripting

How to input username on text file into finger command on shell script

I'm trying to clean up my server and I have the list of some "special" users stored on the text file like this Now I want to write a shell script to finger all of them so I can have some kind of ideas who they are but here comes the problem....I completely forgot how to do it with shell... (3 Replies)
Discussion started by: Micz
3 Replies
Login or Register to Ask a Question