sed - searching for string and storing in variable


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting sed - searching for string and storing in variable
# 1  
Old 04-12-2008
sed - searching for string and storing in variable

Hi

I'm trying to find a way to search a text file for a specific string.

I have a file which contains i.p. addresses and port numbers in the following format:

'ip="www.xxx.yyy.zzz"'
'port="xx""'

I want to print only the parts between the double quotes for use in seperate variables, if this makes sense. I do not want to store the i.p=" " or port=" " parts of the string.

Also the i.p. and corresponding port number are on the same line, if this helps.

This is what the actual line looks like:

<LS id="group1" ip="www.xxx.yyy.zzz" port="aa" acceptorthreads="1" security="off" defaultvs="http-test-http" servername="test.test.com" >

Thanks in advance.
# 2  
Old 04-12-2008
Try something like:

Code:
awk -F"\"" '/<LS id=/{print $4;print $6}' file

Regards
# 3  
Old 04-12-2008
I forgot to mention that the info is not always stored under the same field numbers, so I need to search for the specific string rather than looking for $4,$6 or any other specific field.
# 4  
Old 04-12-2008
Try this sed command (not fully tested) :
Code:
sed -n 's/.*ip="\([^"]*\)".*/\1/p' inputfile

Jean-Pierre.
# 5  
Old 04-12-2008
Many thanks Jean-Pierre, that did the trick.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Searching for variable string

Hi Guys, So im trying to search for the most recent occurance of a string in the most recently updated .log file in a certain directory. The string i am searching for is a value, now if this value is greater than 800 i would like an email sent out with some text (blah blah blah). This is what... (7 Replies)
Discussion started by: hello_world
7 Replies

2. UNIX for Advanced & Expert Users

Passing variable as input & storing output in other variable

I have a below syntax its working fine... var12=$(ps -ef | grep apache | awk '{print $2,$4}') Im getting expected output as below: printf "%b\n" "${VAR12}" dell 123 dell 456 dell 457 Now I wrote a while loop.. the output of VAR12 should be passed as input parameters to while loop and results... (5 Replies)
Discussion started by: sam@sam
5 Replies

3. Shell Programming and Scripting

Storing a variable using sed

I would like to use sed to store a variable. The code is : echo $HEADERREC CUTVAR=$(echo "$HEADERREC"|sed 's/SDV/STR') echo $CUTVAR Output I am getting now: 0012PVGRSCDVSDV 005 00000000000000000000 2014 0.00 sed: Function s/SDV/STR cannot be parsed. Desired... (5 Replies)
Discussion started by: MIA651
5 Replies

4. Shell Programming and Scripting

Cutting a string and storing it in a variable

Hello I have a script: path=test1 echo "${path%?}" till this the program is successful in deleting hte last character i.e. "1" and displays an output --> test. After this how can i save this output to another variable. (2 Replies)
Discussion started by: Kishore920
2 Replies

5. Shell Programming and Scripting

How to replace a string with a variable in a file using sed?

I have a file having some text like: PATH_ABC=/user/myLocation I have to replace "/user/myLocation" with a session variable say, $REPLACE_PATH, where $REPLACE_PATH=/user/myReplaceLocation The following sed command is not working. It is writing PATH_ABC=$REPLACE_PATH in the file ... (2 Replies)
Discussion started by: SKhan
2 Replies

6. Shell Programming and Scripting

perl : searching for month and storing the date and time in an array

I am writing the code in perl. I have an array in perl and each variable in the array contains the data in the below format Now I need to check the below variable w.r.t system month I need to store the date and time(Tue Aug 7 03:54:12 2012) from the below data into file if contains only 'Aug'... (5 Replies)
Discussion started by: giridhar276
5 Replies

7. Shell Programming and Scripting

replace (sed?) a string in file with multiple lines (string) from variable

Can someone tell me how I can do this? e.g: a=$(echo -e wert trewt ertert ertert ertert erttert erterte rterter tertertert ert) How do i replace the STRING with $a? I try this: sed -i 's/STRING/'"$a"'/g' filename.ext but this don' t work (2 Replies)
Discussion started by: jforce
2 Replies

8. Shell Programming and Scripting

Using sed to replace a string in file with a string in a variable that contains spaces

Hi, i call my shell like: my_shell "my project name" my script: #!/bin/bash -vx projectname=$1 sed s/'PROJECT_NAME ='/'PROJECT_NAME = '$projectname/ <test_config_doxy >temp cp temp test_config_doxy the following error occurres: sed s/'PROJECT_NAME ... (2 Replies)
Discussion started by: vivelafete
2 Replies

9. Shell Programming and Scripting

searching and storing unknown number of lines based on the string with a condition

Dear friends, Please help me to resolve the problem below, I have a file with following content: date of file creation : 12 feb 2007 ==================== = name : suresh = city :mumbai #this is a blank line = date : 1st Nov 2005 ==================== few lines of some text this... (7 Replies)
Discussion started by: swamymns
7 Replies

10. UNIX for Dummies Questions & Answers

Searching for a string variable

Hi ... I have a string variable STR = "This is a test message" I have a file abc.txt that I am searching for the occurence of the string STR ... I am using the command in a script cat abc.txt | grep $STR It identifies each space as a seperator and prints word by word. How to... (2 Replies)
Discussion started by: mattrix
2 Replies
Login or Register to Ask a Question