filtering input from read command

Thread Tools Search this Thread
Top Forums Shell Programming and Scripting filtering input from read command
# 1  
Old 07-13-2011
filtering input from read command

I need help understanding a script I'm modifying which someone else has written. Basically I’m looping through a buffer that holds records fetched from a database query. I need a way to separate the primary key values from other attributes in the result. Heres the code:
[query results] > $BUFF
   while read row
       rowID= $(row#*|)   #??
       rowCol=$(row%*|)        #??
} < $BUFF

Last edited by d3mon_spawn; 07-13-2011 at 05:24 PM..
# 2  
Old 07-13-2011
I think the last > is probably supposed to be a <.

this table of string operations explains what those mean among many more, assuming you're using a bourne or ksh shell.

Assuming the number of columns is always the same, there's probably a better way to do this too.
# 3  
Old 07-13-2011
I agree
# 4  
Old 07-14-2011
Is the number of columns always the same, though? Hard to offer suggestions when I don't know what the input and output is supposed to be.
# 5  
Old 07-14-2011
Sorry for not specifying. I'm using bourne and yes the collumns are always the same (only two). Output from the query would look something like this:
1 server001
2 server002
3 server003

---------- Post updated at 12:47 PM ---------- Previous update was at 12:45 PM ----------

I seem to have found a solution that has been working but may not be the best immplementation:
rowID=$(echo $row | awk '{print $1}')
rowCol=$(echo $row | awk '{print $2}')

# 6  
Old 07-14-2011
Easy peasy. read does this all by itself.
while read rowID rowCol
        echo "rowID=$rowID rowCol=$rowCol"
done < filename

If the separator was | instead of whitespace you'd just do
while IFS="|" read rowID rowCol
        echo "rowID=$rowID rowCol=$rowCol"
done < filename

This builtin is hundreds of times faster than calling awk for individual lines.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Shell read command is not waiting for user input

Hi, i am working on one automation , for that i have writing one shell program that take user input in "while read line" block. but read command is taking value that is readed by While block. while read line; do command 1; command 2 echo -n "Do you want to continute > " read rsp... (2 Replies)
Discussion started by: ranvijaidba
2 Replies

2. UNIX for Dummies Questions & Answers

Filtering output from given input

Hi All, I have a input file as below. Input file may contain more hostlists. sample Input file $ cat hostlist.lst cs18-db1-1-sjl cs22-db1-1-was na88-db1-1-chi na21-db1-2-was I want the output like below format. Pls help. Thanks ! Output format: ... (4 Replies)
Discussion started by: kamauv234
4 Replies

3. Shell Programming and Scripting

Error to Read Input from command line

Team , I am trying to write a case condition for database backups.But I am unable to make the script to read input from command line . while true ;do read -p "Do You Wish To Take Database Backup ?? " yn case $yn in *) echo " YES take backup ";; *) echo " NO BACKUP " ;; ... (9 Replies)
Discussion started by: rocking77
9 Replies

4. UNIX for Dummies Questions & Answers

Input for read command

Hello, I want to use the read command in order to input a keyword from the keyboard. I then want to use this keyword in a grep command. How to I input a phrase as a keyword? For example if I use read keyword "today is" and then use grep, I get "No such file or directory" error. (6 Replies)
Discussion started by: FelipeAd
6 Replies

5. Shell Programming and Scripting

Script to read command line input and change it to some form

Hi, I want to write a small code in which script changes command line input to some form. Example a1 a2 a3 a4 ..... output should be "a1|a2|a3|....." Number of inputs in command line can be any variable (2 Replies)
Discussion started by: Raza Ali
2 Replies

6. Shell Programming and Scripting

Read input file with in awk script not through command line

Hi All, Do we know how to read input file within awk script and send output toanother log file. All this needs to be in awk script, not in command line. I am running this awk through crontab. Cat my.awk #!/bin/awk -f function test(var){ some code} { } END { print"test code" } (5 Replies)
Discussion started by: random_thoughts
5 Replies

7. Shell Programming and Scripting

Read input while executing the command

Hi everyone, i have made a very simple script where it reads the user input and converts the number from celcius to faranheit but instead of running the command and prompting the user for input I want to be able to simply enter a number at the end of the command to run the script. ex. instead of... (1 Reply)
Discussion started by: subway69
1 Replies

8. Shell Programming and Scripting

Solaris- Read command from user input

I need to write a bourne shell script (solaris 10) that accepts input from the user. The input will be a command- any command like ls/ pwd/ mv etc. After the input is read, the shell must execute the command supplied by the user. I know we use read to play with user inputs. Just not sure how to... (2 Replies)
Discussion started by: PDManc
2 Replies

9. Shell Programming and Scripting

How can I send the input of a read line command through a shell script

Hi All, I wish to automate the unix command 'su' through a shell script. I would like to pass the content of a file as password to 'su' command. My script is as below, #! /bin/sh su userA while read line do rpm -ivh $line done < pwd.txt where pwd.txt contains the password of... (6 Replies)
Discussion started by: little_wonder
6 Replies

10. Shell Programming and Scripting

read command (input) inside the while loop

Hi, 'read' command is not working inside the while loop, How can I solve this? Rgds, Sharif. (2 Replies)
Discussion started by: sharif
2 Replies
Login or Register to Ask a Question