Search and assign using sed


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Search and assign using sed
# 1  
Old 09-23-2010
Search and assign using sed

Hi,
I would like to know how to search for a pattern in a file and assign it to a variable using sed.

For example, my hope.txt
address1=123 street,abc city,xyz
address2=456 street,abc city,xyz

In my script I would like to search for patter "address1" and assign the complete line into a variable "A1". How do I do that?


Thanks,
# 2  
Old 09-23-2010
Use the right tool, which in your case is awk, not sed.
# 3  
Old 09-23-2010
Quote:
Originally Posted by jpradley
Use the right tool, which in your case is awk, not sed.
Thanks, How do I do that using awk?
# 4  
Old 09-23-2010
Quote:
Originally Posted by pattu
Hi,
I would like to know how to search for a pattern in a file and assign it to a variable using sed.
Why sed only, is this a homework ? .. anyway
Code:
var=$(sed '/address1/s/.*=\(.*\)/\1/p;d' file)



---------- Post updated at 07:43 PM ---------- Previous update was at 07:34 PM ----------

hmm... I didn't seen that you are open for other solution.
Shell only:
Code:
pattern=address1
var=""
while IFS=\= read pat adr
do 
    case $pat in 
        $pattern ) var=$adr;break;;
    esac
done< file
echo $var

# 5  
Old 09-23-2010
or, something like:
Code:
pat=address1
addr=$(sed -n "s/^$pat=//p" infile)

# 6  
Old 09-23-2010
Quote:
Originally Posted by danmero
Why sed only, is this a homework ? .. anyway
Code:
var=$(sed '/address1/s/.*=\(.*\)/\1/p;d' file)



---------- Post updated at 07:43 PM ---------- Previous update was at 07:34 PM ----------

hmm... I didn't seen that you are open for other solution.
Shell only:
Code:
pattern=address1
var=""
while IFS=\= read pat adr
do 
    case $pat in 
        $pattern ) var=$adr;break;;
    esac
done< file
echo $var

Thanks a lot man.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

sed search

Hello, I know how to use sed for simple search but i need to search something like below in a file. From the below string, i need to find and replace only ./ with a $ using sed. ./abcd/str.xyz Thanks in advance KK (8 Replies)
Discussion started by: Pavan Kumar19
8 Replies

2. Shell Programming and Scripting

Regex in sed to find specific pattern and assign to variable

(5 Replies)
Discussion started by: radioactive9
5 Replies

3. Shell Programming and Scripting

Trim sed output & assign to variable

Hi, I have the following command that parses an xml file to read a node <port>'s value. Hoever the output comes with spaces. My requirement is to trim the spaces around the value and assign to a variable. sed -n 's|<port>\(.*\)</port>|\1|p' ../cfg.xml How do I go about it? (6 Replies)
Discussion started by: sai2013
6 Replies

4. Shell Programming and Scripting

Running a program multiple times to search pattern and assign structure

Hi all, I have a big file (n.txt) with following pattern: ATOM 1 N SER A 1 122.392 152.261 138.190 1.00 0.00 N ATOM 2 CA SER A 1 122.726 151.241 139.183 1.00 0.00 C TER ENDMDL ATOM 1 N SER A 1 114.207 142.287 135.439 1.00 0.00 ... (3 Replies)
Discussion started by: bioinfo
3 Replies

5. Shell Programming and Scripting

sed help - search/copy from one file and search/paste to another

I am a newbie and would like some help with the following - Trying to search fileA for a string similar to - AS11000022010 30.4 31.7 43.7 53.8 60.5 71.1 75.2 74.7 66.9 56.6 42.7 32.5 53.3 I then want to replace that string with a string from fileB - ... (5 Replies)
Discussion started by: ncwxpanther
5 Replies

6. Shell Programming and Scripting

How to assign the Pattern Search string as Input Variable

guys, I need to know how to assing pattern matched string as an input command variable. Here it goes' My script is something like this. ./routing.sh <Server> <enable|disable> ## This Script takes an input <Server> variable from this line of the script ## echo $1 | egrep... (1 Reply)
Discussion started by: raghunsi
1 Replies

7. UNIX for Dummies Questions & Answers

SED: Can't Repeat Search Character in SED Output

I'm not sure if the problem I'm seeing is an artifact of sed or simply a beginner's mistake. Here's the problem: I want to add a zero-width space following each underscore between XML tags. For example, if I had the following xml: <MY_BIG_TAG>This_is_a_test</MY_BIG_TAG> It should look like... (8 Replies)
Discussion started by: rhetoric101
8 Replies

8. Shell Programming and Scripting

How to assign value to a variable with row(using -n) returned by sed

Hi Friends, REQUIREMENT: Want to delete files from the current directory match with the same in the file test.txt set -x i=1 echo "i=$i" COUNT=`sed -n '$=' test.txt` echo "Count=$COUNT" while do "## Here is error##" FILETOREMOVE=`sed -n \'$i,1p\' test.txt` echo $FILETOREMOVE... (5 Replies)
Discussion started by: sourabhsharma
5 Replies

9. Shell Programming and Scripting

assign subst|grep|sed command result to a variable

Hi, I'm quite new to scripting and I want to modify following line of an existing script: MYVAR=`subst |grep 'L:\\\:' | sed -e 's/.*\\\//'`; What I have to do is to use the content of a variable instead of the constant expression 'L:\\\:' as the grep string to be matched. Assuming I already... (5 Replies)
Discussion started by: snowbiker99
5 Replies

10. UNIX for Dummies Questions & Answers

search file for word, then assign to variable

Hi all, Trying to search a file for one word only, then assign that word to a variable. Not sure if this is a grep or awk (or either) function. Should be a simple operation. Example: This file contains the string "COMPLETE". I would like to pull that word out and assign it to a... (2 Replies)
Discussion started by: dejit
2 Replies
Login or Register to Ask a Question