Searching for a member of an arrray Bash 3.2 with a @..


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Searching for a member of an arrray Bash 3.2 with a @..
# 1  
Old 08-26-2013
Searching for a member of an arrray Bash 3.2 with a @..

Creating the array.
Code:
while read line
	do
	    array+=("$line")
	done < "$temporary_file"

This is the basic idea, but not working at the moment.
It just seems to be returning "No such file or directory"
for each array member

Code:
 for (( i = 0 ; i < "${#array[@]}"; i++ )); do
    	if grep "@" "${array[$i]}"; then
   		echo "${array[$i]}"
 		fi
    done


Last edited by Scott; 08-26-2013 at 12:32 AM.. Reason: Code tags not icode tags
# 2  
Old 08-26-2013
# 3  
Old 08-26-2013
Sorry, I am not good with bash functions that do not return void. How would I re-write those so they were inline?
# 4  
Old 08-26-2013
See if this is what you were looking for:
Code:
$ cat test.sh
FA=("one" "two" "@"  "four" "@" "six")

for i in ${!FA[@]}
do
  if [ '@' = "${FA[$i]}" ]; then
    echo "Found @ at index $i"
  fi
done

$ test.sh
Found @ at index 2
Found @ at index 4

# 5  
Old 08-26-2013
Here is inline:

Code:
A=("one" "two" "mail@yourhost.com")
B=("one" "two" "three")

for((i=0;i<${#A};i++)) {
   if [[ ${A[i]} = *@* ]]
   then
       echo "A contains @"
   fi
}

for((i=0;i<${#B};i++)) {
   if [[ ${B[i]} = *@* ]]
   then
       echo "B contains @"
   fi
}


And here is function version:

Code:
A=("one" "two" "mail@yourhost.com")
B=("one" "two" "three")

containsAT() {
    for((i=1;i<=$#;i++)) {
        [[ "${!i}" = *@* ]] && return 0
    }
    return 1
}

if containsAT "${A[@]}"
then
   echo "A contains @"
fi

if containsAT "${B[@]}"
then
   echo "B contains @"
fi

This User Gave Thanks to Chubler_XL For This Post:
# 6  
Old 08-26-2013
@Spacebar
Not working still. Thanks for you input.

Taking this data

Code:
First Last
123 some road
some unit
citySTATEZIP
(111) 222-1111
(111) 222-1112
Fax:(111) 222-1113
user@domain.com
#:123456

stored in

"$temporary_file"

then

Code:
while read line
	do
	    array+=("$line")
	done < "$temporary_file"

and for debugging I print...

Code:
    echo " array 0 = ${array[0]}"
    echo " array 1 = ${array[1]}"
    echo " array 2 = ${array[2]}"
    echo " array 3 = ${array[3]}"
    echo " array 4 = ${array[4]}"
    echo " array 5 = ${array[5]}"
    echo " array 6 = ${array[6]}"
    echo " array 7 = ${array[7]}"
    echo " array 8 = ${array[8]}"

and the array has populated successfully.

with this output..

Code:
 array 0 = First Last
 array 1 = 123 some road
 array 2 = some unit
 array 3 = citySTATEZIP
 array 4 = (111) 222-1111
 array 5 = (111) 222-1112
 array 6 = Fax:(111) 222-1113
 array 7 = user@domain.com
 array 8 = #:123456


The next problem is this data set (members 5-6) may or may not appear.
So algorithmically speaking and where I am getting stuck...

Next step:

1. search array for email address (using the @ symbol here to keep things simple)



Then I am going to use conditional / control flow statements using the position of the email address to further parse the data.
Not sure if this helps, but wanted to add some context.

Thanks in advance, for any help.
Moderator's Comments:
Mod Comment As requested before, use CODE tags; not ICODE tags for multi-line code, input, and output samples.

Last edited by Don Cragun; 08-26-2013 at 01:42 AM.. Reason: Change ICODE tags to CODE tags where appropriate.
# 7  
Old 08-26-2013
Quote:
Originally Posted by briandanielz
Creating the array.
Code:
while read line
	do
	    array+=("$line")
	done < "$temporary_file"

This is the basic idea, but not working at the moment.
It just seems to be returning "No such file or directory"
for each array member

Code:
 for (( i = 0 ; i < "${#array[@]}"; i++ )); do
    	if grep "@" "${array[$i]}"; then
   		echo "${array[$i]}"
 		fi
    done

The code that you have is looking for lines containing @ in the files named by lines in the file named by the expansion of the shell variable temporary_file. Presumably you're getting error messages because the lines in your file are not names of files to be searched.

If what you want to do is print lines in the file named by $temporary_file that contain an @, that would much more simply be done by:
Code:
grep '@' "$temporary_file"

If you want a way to determine if the expansion of a shell variable ($x in this example) contains an @ using only features that are available in all standards conforming shells, you could try something like:
Code:
if [ "${x#*@}" = "$x" ]
then    echo 'no @ found'
else    echo '@ found'
fi

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. What is on Your Mind?

New member

Hi guys! My name is Leonida and I am new here to this forum. Nice meeting you all. (1 Reply)
Discussion started by: leeoona
1 Replies

2. Shell Programming and Scripting

Bash 3.2 - Array / Regex - IF 3rd member in array ends in 5 digits then do somthing...

Trying to do some control flow parsing based on the index postion of an array member. Here is the pseudo code I am trying to write in (preferably in pure bash) where possible. I am thinking regex with do the trick, but need a little help. pesudo code if == ENDSINFIVEINTS ]]; then do... (4 Replies)
Discussion started by: briandanielz
4 Replies

3. Shell Programming and Scripting

Searching file for pattern, output to file (BASH)

Hello, I'm trying to write a script in Bash to assist in pentesting. Essentially I'm looking to use a script to search for some terms in a log file and then send that key information into another file. The log files consist of HTTP and SSL information that someone creates while browsing and... (2 Replies)
Discussion started by: Sagesparten007
2 Replies

4. Shell Programming and Scripting

searching a file with a specified text without using conventional file searching commands

without using conventional file searching commands like find etc, is it possible to locate a file if i just know that the file that i'm searching for contains a particular text like "Hello world" or something? (5 Replies)
Discussion started by: arindamlive
5 Replies

5. Shell Programming and Scripting

Searching for bash to cshell convertor

Hi ,I have seen this comverters before ,but I was searching today and I can not find them .Is anybody used them before .Any recomendations Thanks (5 Replies)
Discussion started by: lio123
5 Replies

6. UNIX for Dummies Questions & Answers

New Member

HI Bruce from Central PA I have never used Unix but am sick of Microsoft so want to learn it. I use to own a computer store front and training center and stated our with Atari, Commodore and the 1st PC and MS/DOS then Windows BUT sick of Microsoft controlling the computer industry. ... (2 Replies)
Discussion started by: Brucec
2 Replies

7. Shell Programming and Scripting

Performing fast searching operations with a bash script

Hi, Here is a tough requirement , to be served by bash script. I want to perform 3,00,000 * 10,000 searches. i.e. I have 10,000 doc files and 3,00,000 html files in the file-system. I want to check, which of the doc files are referred in any html files. (ex- <a href="abc.doc">abc</a>)... (3 Replies)
Discussion started by: jitendriya.dash
3 Replies

8. Shell Programming and Scripting

Bash script for searching a string

Hi, I have a file that contains thousands of records. Each record starts with "New Record". I need to search this file based on a given value of "timestamp" and write all the records that match this timestamp into a new file. I was able to locate the existence of a given value of timestamp using... (10 Replies)
Discussion started by: shoponek
10 Replies

9. Shell Programming and Scripting

Searching Bash Arrays

Hi, I am writing a bash shell script. I would like to execute a statement only if an array contains a specific value. For example: array=(1 3 5 7) I would like to execute the statement only if the value 3 is present in ${array}. Thanks for any help, Mike (1 Reply)
Discussion started by: msb65
1 Replies

10. UNIX for Dummies Questions & Answers

new member need help

brothers and sisters iam a new member of your respectful forum hope to accept me i really would like to an expert in unix and linux so please tell me how could ? and what i should have ? i want to make my project in these subject but i dont know what to do and which... (12 Replies)
Discussion started by: yemen
12 Replies
Login or Register to Ask a Question