search a string in a line and save it in a variable


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting search a string in a line and save it in a variable
# 1  
Old 09-18-2009
search a string in a line and save it in a variable

Hi
I want to read a file line by line and search for a particular string in each line(say for example string containing @ )and save that string into a variable.
Can someone suggest me the way to implement it.I am using K- shell
Thanks
Ishita
# 2  
Old 09-18-2009
var=$(awk '/pattern/')
# 3  
Old 09-18-2009
Ishita,grep -i "error" /var/adm/messages > error.log would search the string "error" in /var/adm/message and would redirect the output to error.log.Remember that it would display the whole line in which the string "error" occurs. `cat error.log` read VALUEI think above code will take care of the variable part. HG
# 4  
Old 09-18-2009
Code:
 var=`grep -ho "[a-z]*<String>[a-z]*" Input_file`;echo $var

The above command gives the desired output.
# 5  
Old 09-18-2009
Hi
Sorry guys.I didnt explained my problem clearly.Basically I need to read line by line from an input file and search for two different string in each line.When string 1 is found i need to print it and when string 2 is found i need to save it into a variable.
i tried with below:-
exec 4<input.txt
while read line <&4
do
case $line in
create* ) echo "$line --- Create"
;;
esac
done
exec 4<&-
but it is working only for searching 1 string in the line.Is it possible to modify the above to seach 2 different string in a single line.The input line is something like " Create abc@xyz.com

I am struggling with it from last two days.but didnt got any feasible solution. Smilie
Can someone suggest me some way to implement it.

Last edited by Ishita; 09-18-2009 at 08:14 AM..
# 6  
Old 09-18-2009
Why don't you use grep(1) instead of case ? Use two grep's inside the while loop. One to find the first pattern and second to search for the second pattern(which you can save to a variable).

Also, please surround your code inside code tags for better readability.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Save line from text in variable

Hi, I wrote a csh script where I want to save in a loop each time a different line from a text file (att_file) in the $name variable. But it seems not to work. att_file looks like: 123123123 345345345 345345345 set name = `head -n $count $att_file | tail -n 1 | awk '{print $1}'` Do... (3 Replies)
Discussion started by: MLImag
3 Replies

2. UNIX for Beginners Questions & Answers

Need help with how to search a file for a variable string and delete that line

Hi, I have a working script. It does what I am intending it to but a bit confused whether the sed part is supposed to be working or not. Further down is the script with the sed part that should have been working but not and the grep -v part which is the workaround that I am using at the... (10 Replies)
Discussion started by: newbie_01
10 Replies

3. Shell Programming and Scripting

awk variable search and line count between variable-search pattern

Input: |Running the Rsync|Sun Oct 16 22:48:01 BST 2016 |End of the Rsync|Sun Oct 16 22:49:54 BST 2016 |Running the Rsync|Sun Oct 16 22:54:01 BST 2016 |End of the Rsync|Sun Oct 16 22:55:45 BST 2016 |Running the Rsync|Sun Oct 16 23:00:02 BST 2016 |End of the Rsync|Sun Oct 16 23:01:44 BST 2016... (4 Replies)
Discussion started by: busyboy
4 Replies

4. Shell Programming and Scripting

Search string within a file and list common words from the line having the search string

Hi, Need your help for this scripting issue I have. I am not really good at this, so seeking your help. I have a file looking similar to this: Hello, i am human and name=ABCD. How are you? Hello, i am human and name=PQRS. I am good. Hello, i am human and name=ABCD. Good bye. Hello, i... (12 Replies)
Discussion started by: royzlife
12 Replies

5. Shell Programming and Scripting

Search a string in a text file and add another string at the particular position of a line

I am having a text file which is having more than 200 lines. EX: 001010122 12000 BIB 12000 11200 1200003 001010122 2000 AND 12000 11200 1200003 001010122 12000 KVB 12000 11200 1200003 In the above file i want to search for string KVB and add/replace... (1 Reply)
Discussion started by: suryanarayana
1 Replies

6. Shell Programming and Scripting

Search several string and convert into a single line for each search string using awk command AIX?.

I need to search the file using strings "Request Type" , " Request Method" , "Response Type" and by using result set find the xml tags and convert into a single line?. below are the scenarios. Cat test Nov 10, 2012 5:17:53 AM INFO: Request Type Line 1.... (5 Replies)
Discussion started by: laknar
5 Replies

7. Shell Programming and Scripting

find the line starting with a pattern and save a part in variable

Hi i have a file which has mutiple line in it. inside that i have a pattern similar to this /abc/def/hij i want to fine the pattern starting with "/" and get the first word in between the the symbols "/" i.e. "abc" in this case into a variable. thanks in advance (13 Replies)
Discussion started by: kichu
13 Replies

8. Shell Programming and Scripting

save every line in log file with matched string

i have been doing this script to match every line in a current log file (access_log) with strings that i list from a path (consist of 100 of user's name ex: meggae ).. and then make a directory of every string from the text file (/path/meggae/) --->if it matched.. then print every line from the... (3 Replies)
Discussion started by: meggae
3 Replies

9. Shell Programming and Scripting

Search for string in a file and extract another string to a variable

Hi, guys. I have one question: I need to search for a string in a file, and then extract another string from the file and assign it to a variable. For example: the contents of the file (group) is below: ... ftp:x:23: mail:x:34 ... testing:x:2001 sales:x:2002 development:x:2003 ...... (6 Replies)
Discussion started by: daikeyang
6 Replies

10. Shell Programming and Scripting

Perl: Search for string on line then search and replace text

Hi All, I have a file that I need to be able to find a pattern match on a line, search that line for a text pattern, and replace that text. An example of 4 lines in my file is: 1. MatchText_randomNumberOfText moreData ReplaceMe moreData 2. MatchText_randomNumberOfText moreData moreData... (4 Replies)
Discussion started by: Crypto
4 Replies
Login or Register to Ask a Question