how to get the string stored in a variable in a line???


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting how to get the string stored in a variable in a line???
# 1  
Old 04-04-2008
CPU & Memory how to get the string stored in a variable in a line???

Hi all,

I want to search for a data type in a line.For this in a loop i am checking for $DATA_TYPE in a line using grep.But grep is not able to find when i give this.
Can any one tell me how to check string in $DATA_TYPE variable in line usign grep (or) any other way to do the above task.

declare -a TYPES_LIST
declare -a EXIST_FLAG=0
TYPES_LIST[0]="char"
TYPES_LIST[1]="int"
TYPES_LIST[2]="float"
TYPES_LIST[3]="double"


for DATA_TYPE in "${TYPES_LIST[@]}"
do

EXIST_FLAG=`echo $CHECK_LINE|grep -c ':$[ *]"$DATA_TYPE"'

if test $EXIST_FLAG -gt 0 ; then
echo $CHECK_LINE
break
fi
done

Here CHECK_LINE="580:$ char *tablename;"

Thanks in Advance
JS
# 2  
Old 04-05-2008
To parse the line you can do something like this:

Code:
for DATA_TYPE in "${TYPES_LIST[@]}"
do
  echo "$CHECK_LINE"|grep $DATA_TYPE >/dev/null 2>&1
  if [ $? -eq 0 ]; then
    echo "Find in "$CHECK_LINE" datatype = "$DATA_TYPE
    break
  fi
done

Regards
# 3  
Old 04-05-2008
The problem with your script is the use of a literal dollar sign in the regular expression; this is a special character, which means "end of line" to grep. You can escape it by prefixing it with a backslash, or (like you alrady did with the asterisk) by putting it in a character class. Also you seem to be missing a closing single quote.

The motions to run grep in backticks and then examine the output are redundant, you can just run [ef]grep in the if directly.

grep cannot (typically) grep for multiple things, but you can look at egrep or fgrep if you want to simplify your script.

Code:
if echo "$CHECK_LINE" | egrep ':[$][ ][*](char|int|float|double)'; then
  break
fi

grep (without -c or -q) prints the matching line as a matter of course, so you don't need to do that separately.

Even the break (and thus the whole if, and the whole for loop) is redundant if you use egrep or fgrep to look for all the possibilities in one go.

Last edited by era; 04-05-2008 at 12:05 PM.. Reason: CODE tags don't like space between [$] and [*], duh
# 4  
Old 04-05-2008
Franklin52: et tu Brute. Please don't perpetrate this "if test $?" idiom when if already checks the value of $? by design.
# 5  
Old 04-05-2008

Replace your entire script with:
Code:
case $CHECK_LINE in
  *char*|*int*|*float*|*double*) printf "%s\n" "$CHECK_LINE ;;
esac

If you need to know which type was found:
Code:
case $CHECK_LINE in
  *char*)   type=char ;;
  *int*)    type=int;;
  *float*)  type=float;;
  *double*) type=double;;
  *) type= ;;
esac
if [ -n "$type" ]
then
     printf  "%s\n" "$CHECK_LINE
fi

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Grep a sub-string from a string stored in a variable.

For example: I am grepping "Hello" from a file and there are 10 matches. So all ten lines with match will get stored into a variable($match). Now I want to ignore those lines which have "Hi" present in that. Currently I tried this: match = grep "Hello" file | grep -v "Hi" file But that's not... (2 Replies)
Discussion started by: pavan
2 Replies

2. Shell Programming and Scripting

awk to lookup stored variable in file and print matching line

The bash bash below extracts the oldest folder from a directory and stores it in filename That result will match a line in bold in input. In the matching line there is an_xxx digit in italics that (once the leading zero is removed) will match a line in link. That is the lint to print in output.... (2 Replies)
Discussion started by: cmccabe
2 Replies

3. Shell Programming and Scripting

Skip to next line if the variable is string

Hi I have the follwoing requirement I have a file as follows: # cat priy yyy.poweroff_cmd = /sbin/poweroff hhh.powersave-nap = 1 When this file is provided as input, I first used "awk" command and saved variables present after "=" replace=$line replace1=`echo $line | awk -F "="... (3 Replies)
Discussion started by: Priya Amaresh
3 Replies

4. Shell Programming and Scripting

awk - Multi-line data to be stored in variable

Greetings Experts, As part of automating the sql generation, I have the source table name, target table name, join condition stored in a file join_conditions.txt which is a delimited file (I can edit the file if for any reason). The reason I needed to store is I have built SELECT list without... (5 Replies)
Discussion started by: chill3chee
5 Replies

5. Shell Programming and Scripting

Deleting double quoted string from a line when line number is variable

I need to remove double quoted strings from specific lines in a file. The specific line numbers are a variable. For example, line 5 of the file contains A B C "string" I want to remove "string". The following sed command works: sed '5 s/\"*\"//' $file If there are multiple... (2 Replies)
Discussion started by: rennatsb
2 Replies

6. Shell Programming and Scripting

PERL : pattern matching a string stored in a variable

I have two variables, my $filename = "abc_yyyy_mm_dd.txt"; my $filename1 = " abc_2011_11_07.txt"; I need to perform some operations after checking if $filename has $filename1 in it i have used the below code, if($filename =~ /^$filename1/) { ---- -- } (2 Replies)
Discussion started by: irudayaraj
2 Replies

7. Shell Programming and Scripting

Variable not found error for a variable which is returned from stored procedure

can anyone please help me with this: i have written a shell script and a stored procedure which has one OUT parameter. now i want to use that out parameter as an input to the unix script but i am getting an error as variable not found. below are the unix scripts and stored procedure... ... (4 Replies)
Discussion started by: swap21783
4 Replies

8. Shell Programming and Scripting

Read Line and sent as variable for each string

Hi , I want to read below output, lets called it output1.txt and each string for every line will be declare as a variables. This is the input file 196 server_a server_unix_2 FW 196 server_b server_win_1 CD 196 server_c server_win_2 CD 196 server_bd ... (2 Replies)
Discussion started by: sQew
2 Replies

9. Shell Programming and Scripting

Delete first character from a string stored in a variable

Hallo! Example. #!/bin/bash BACKUP_DIR=/home/userx/backups/evolution echo $BACKUP_DIR # delete the first character from the string BACKUP_DIR=$(echo $BACKUP_DIR | cut -c 2-) echo $BACKUP_DIR It works. It does want I want, delete the first character from string in the... (11 Replies)
Discussion started by: linuxinho
11 Replies

10. Shell Programming and Scripting

How to define a variable with variable definition is stored in a variable?

Hi all, I have a variable say var1 (output from somewhere, which I can't change)which store something like this: echo $var1 name=fred age=25 address="123 abc" password=pass1234 how can I make the variable $name, $age, $address and $password contain the info? I mean do this in a... (1 Reply)
Discussion started by: freddy1228
1 Replies
Login or Register to Ask a Question