How can we create a string table in bash shell ?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How can we create a string table in bash shell ?
# 1  
Old 10-26-2010
Question How can we create a string table in bash shell ?

Hello,

How can we write the pseudo code below using Bash shell scripting?
Code:
table = [apple,orange,tomato];
i=0;
do{
       grep table[i] h.txt;
}while(i<3);

or
Code:
table = [apple,orange,tomato];
i=0;
for(i=0;i<3;i++)
      grep table[i] h.txt;

# 2  
Old 10-26-2010
Code:
[ctsgnb@shell ~]$ cat veg
whatever
orange
tomato
whatever
tomato
whatever
[ctsgnb@shell ~]$ var='tomato|orange'
[ctsgnb@shell ~]$ grep -E "$var" veg
orange
tomato
tomato
[ctsgnb@shell ~]$

This User Gave Thanks to ctsgnb For This Post:
# 3  
Old 10-26-2010
Thank you very much.
# 4  
Old 10-26-2010
To do a direct convert of your pseudo code, using bash arrays:

Code:
#!/bin/bash
table=(apple orange tomato)
for((i=0; i < ${#table[@]}; i++))
do
    grep ${table[$i]} h.txt
done

This User Gave Thanks to Chubler_XL For This Post:
# 5  
Old 10-26-2010
you also can have
Code:
#cat toto
orange
tomato
# grep -f toto veg
orange
tomato
tomato

This User Gave Thanks to ctsgnb For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Convert a string to variable in Bash shell

Hi All; I have 2 variable let's say $A and $B. These are actually some remotely executed command outputs. I captured these values in my local variables. Here is my problem. When I try to do some arithmetic operation with these variables, I receive an error message. Neither expr nor typeset -i... (3 Replies)
Discussion started by: Meacham12
3 Replies

2. UNIX for Advanced & Expert Users

string manipulation in bash shell

Hi All, I am using a bash shell and want to the following thing. A process sends the following string to my script BACKUP_FAIL_REASON="Failed - Application Dump CDMACA-0:grep: /opt/nortel/ca/data/1245184/sd00/image1/S110907070708HIS... (4 Replies)
Discussion started by: Pkumar Sachin
4 Replies

3. Shell Programming and Scripting

Bash shell script that inserts a text data file into an HTML table

hi , i need to create a bash shell script that insert a text data file into an html made table, this table output has to mailed.I am new to shell scripting and have a very minimum idea of shell scripting. please help. (9 Replies)
Discussion started by: intern123
9 Replies

4. Shell Programming and Scripting

Create a table using shell scripting

Hi Can we create a rectangular table as i have attached in the query . This is primarily for populating the created table with data gathered . Hope I made myself clear ... Pls suggest Thanks (1 Reply)
Discussion started by: ultimatix
1 Replies

5. Shell Programming and Scripting

Create DB table through shell script

Hi, Can anyone tell me that, How to create table in Oracle database through shell script(ksh). Table contains 3 fields, 1] Emp ID, String, primary key 2] Name, String 3] B Date, date. Thanks in advance. (6 Replies)
Discussion started by: Poonamol
6 Replies

6. Shell Programming and Scripting

Create Bash shell scripts corresponding to windows bat files

Experts, I am newbie in shell scripting. I want to write Bash shell scripts corresponding to windows bat files. I have installed cygwin at c:\cygwin and i am trying to crate the sh file using vi editor. i am not able to understand how to use linux/unix convention for the code. following is my... (1 Reply)
Discussion started by: rajuchacha007
1 Replies

7. UNIX and Linux Applications

create table via stored procedure (passing the table name to it)

hi there, I am trying to create a stored procedure that i can pass the table name to and it will create a table with that name. but for some reason it creates with what i have defined as the variable name . In the case of the example below it creates a table called 'tname' for example ... (6 Replies)
Discussion started by: rethink
6 Replies

8. Shell Programming and Scripting

help with a bash script to create a html table

Hi guys as the title says i need a little help i have partisally written a bash script to create a table in html so if i use ./test 3,3 i get the following output for the third arguement in the script i wish to include content that will be replace the A characters in the... (2 Replies)
Discussion started by: dunryc
2 Replies

9. Shell Programming and Scripting

[ORACLE] CREATE TABLE in bash

Hey, I want to create a table in bash, my db server is oracle. You can find my script below: I am very confused. I tried to run this script many times without any luck. Please help! (6 Replies)
Discussion started by: radek
6 Replies
Login or Register to Ask a Question