Help needed in processing multiple variables in a single sed command.


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Help needed in processing multiple variables in a single sed command.
# 1  
Old 12-15-2005
Question Help needed in processing multiple variables in a single sed command.

Is it possible to process multiple variables in a single sed command?
I have the following ksh with three variables and I want to search for all variables which start with "var" inside input.txt.
I tired "$var$" but it just prints out everyting in input.txt and does not work.

$ more test.ksh
#!/bin/ksh

var1=AAA
var2=BBB
var3=CCC

sed -n "/$var$/p" input.txt

$ more input.txt
DDD
CCC
EEE
AAA
BBB
$


My desired output is the below:

CCC
AAA
BBB

Any help will be appreciated.
# 2  
Old 12-15-2005
Just use egrep....

Code:
#!/bin/ksh

var1=AAA
var2=BBB
var3=CCC

egrep "^(${var1}|${var2}|${var3})$" input.txt

Cheers
ZB
# 3  
Old 12-15-2005
Thanks zazzybob.
However, for cases when there are like over 100 variables the code will be too long and will look ugly so I want way of some how using a wildcard character to process multiple variables. Is this possible?
# 4  
Old 12-15-2005
The output order may be slightly different but you could do something like this...

Code:
#!/bin/ksh

var[0]=AAA
var[1]=BBB
var[2]=CCC

for i in ${var[@]}; do
  sed -n "/$i$/p" input.txt
done

exit 0

Cheers
ZB
# 5  
Old 12-15-2005
use -e option of sed...

sed -n -e "/$var1$/p" -e "/$var2$/p" -e "/$var3$/p" input.txt

Smilie
# 6  
Old 12-16-2005
Quote:
Originally Posted by zazzybob
The output order may be slightly different but you could do something like this...

Code:
#!/bin/ksh

var[0]=AAA
var[1]=BBB
var[2]=CCC

for i in ${var[@]}; do
  sed -n "/$i$/p" input.txt
done

exit 0

Cheers
ZB
in this case file needs to be scanned for each and every search pattern

---->
better way would be to accumalate search patterns together and use egrep in a single shot
Code:
>cat input.txt
DDD
CCC
EEE
AAA
BBB

Code:
>cat search_pattern.txt
var1=AAA
var2=BBB
var3=CCC

Code:
>cat try.ksh
# !/usr/bin/ksh

pattern=""
for val in `sed -e 's/.*=//' search_pattern.txt`
do
   pattern=$pattern$val"|"
done

egrep -e $pattern input.txt
exit 0

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Processing Multiple Arguments in Command Line Options

Hi All, I am new to scripting. Could you please assist me . Here is my requirement. I have written a script that has 2 option flags defined. -l) calls some function with the arguments passed in front of -l -r) calls second function with the arguments passed in front of -r *) calls the... (7 Replies)
Discussion started by: Jay Deshpande
7 Replies

2. Shell Programming and Scripting

sed command to grep multiple pattern present in single line and delete that line

here is what i want to achieve.. i have a file with below contents cat fileName blah blah blah . .DROP this REJECT that . --sport 7800 -j REJECT --reject-with icmp-port-unreachable --dport 7800 -j REJECT --reject-with icmp-port-unreachable . . . more blah blah blah --dport 3306... (14 Replies)
Discussion started by: vivek d r
14 Replies

3. UNIX Desktop Questions & Answers

How to squeeze multiple pipe character '|' into single '|' using sed?

Hi, I am trying to convert multiple Unix pipe symbol or bar into single |. I have tried with the following sed statements, but, no success :(. I need it using sed only echo "sed 's/\|\+/\|/g' sed 's/*/\|/' sed 's/\|*/|/' sed -r 's/\|+/\|/' However, the below awk code is working fine.... (4 Replies)
Discussion started by: royalibrahim
4 Replies

4. Shell Programming and Scripting

SED - Multiple String - Single Line

Would appear to me to be a farily simple question but having search all the threads I can't find the answer .. I just want sed to output the single line in a file that contains two string anywhere on the line.. e.g. currently using this command sed -n -e'/str1/p' -e '/str2/p' < file and... (3 Replies)
Discussion started by: flopster
3 Replies

5. UNIX for Dummies Questions & Answers

Grep multiple strings in multiple files using single command

Hi, I will use below command for grep single string ("osuser" is search string) ex: find . -type f | xarg grep -il osuser but i have one more string "v$session" here i want to grep in which file these two strings are present. any help is appreciated, Thanks in advance. Gagan (2 Replies)
Discussion started by: gagan4599
2 Replies

6. Shell Programming and Scripting

Filtering Multiple variables from a single column

Hi, I am currently filtering a file, "BUILD_TIMES", that has multiple column of information in it. An example of the data is as follows; Fri Nov 5 15:31:33 2010 00:28:17 R7_BCGNOFJ_70.68 Fri Nov 5 20:57:41 2010 00:07:21 R7_ADJCEL_80.6 Wed Nov 10 17:33:21 2010 00:01:13 R7_BCTTEST3_80.1X... (7 Replies)
Discussion started by: crunchie
7 Replies

7. UNIX for Dummies Questions & Answers

single output of awk script processing multiple files

Helllo UNIX Forum :) Since I am posting on this board, yes, I am new to UNIX! I read a copy of "UNIX made easy" from 1990, which felt like a making a "computer-science time jump" backwards ;) So, basically I have some sort of understanding what the basic concept is. Problem Description:... (6 Replies)
Discussion started by: Kasimir
6 Replies

8. Shell Programming and Scripting

Putting multiple sed commands on a single line

Hi, I want to make sed write a part of fileA (first 7 lines) to file1 and the rest of fileA to file2 in a single call and single line in sed. If I do the following: sed '1,7w file1; 8,$w file2' fileA I get only one file named file1 plus all the characters following file1. If I try to use curly... (1 Reply)
Discussion started by: varelg
1 Replies

9. Shell Programming and Scripting

flexible sed command needed to handle multiple input types

Hello, I need a smart sed command that can take any of the following two as an input and give below mentioned output. As you can see, I am trying to convert some C code INPUT: struct abc_sample1 { char myString; UINT16 myValue1; ... (2 Replies)
Discussion started by: SiftinDotCom
2 Replies

10. Shell Programming and Scripting

Reading Multiple Variables From a Single Line in Shell

I'm a Linux newb, I've been running a Debian Linux server for about a year now, and I've written some simple scripts to automate various things, but I still don't know much, and I forget what I learn as fast as I figure it out... Anyway, that really isn't important, I just want you to know that... (14 Replies)
Discussion started by: Drek
14 Replies
Login or Register to Ask a Question