sed containing $ as literal and variable


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers sed containing $ as literal and variable
# 1  
Old 02-13-2012
sed containing $ as literal and variable

Hi, i have a data file

cat f1.txt
$V01$S
this is 1
$v02$D
this is 2
$v03$F
this is 3

I have a variable $PARM which contains value say 'V01'
I have to print lines from f1.txt that start with '$'<${PARM}>'$'

How do i get matching lines from f1.txt that start with '$' then with ${PARM} variable value and then followed by '$'
I also have to get the next line
Code:
cat f1.txt | sed -n -e '/$${PARM}$/{N;p;}'

doesn't work

Thanks,
-srinivas yelamanchili
# 2  
Old 02-13-2012
Code:
user~$ PARM='V01'
user~$ grep -A1 '^\$'$PARM'\$' f1.txt
$V01$S
this is 1

If you still want a sed, here's one:
Code:
sed -n '/\$'$PARM'\$/{N;p;}' f1.txt


Last edited by balajesuri; 02-13-2012 at 07:32 AM..
This User Gave Thanks to balajesuri For This Post:
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Fgrep literal string from a file

have a file1 aaa-bbb-ccc-abcd aaa-bbb-ccc-bacd aaa-bbb-ccc-aaad aaa-bbb-ccc-ahave another file2 aaa-bbb-ccc-a fileusing the fgrep command, trying to have only the literal string returned. fgrep -f file2 file1 is returning aaa-bbb-ccc-abcd aaa-bbb-ccc-aaad aaa-bbb-ccc-aOnly looking for... (1 Reply)
Discussion started by: jimmyf
1 Replies

2. Shell Programming and Scripting

Reading a long literal continued next line

I am trying to identify all messages or prompts from a number of COBOL programs and they can usually be identified by a pair of double quotes on one line. However, sometimes the literal will not be finished on the first line but after a dash in column 7 of the next line, the literal will be... (6 Replies)
Discussion started by: wbport
6 Replies

3. Shell Programming and Scripting

Fgrep/grep -f and literal strings

I have a file like this: cat file name = server jobname = 1010 snapshot_name = funky_Win2k12_20140213210409 I'm trying to use grep to isolate that first line (name = server), but grep -f "name = " file as well as fgrep "name = " file returns all 3 lines. How do I return... (1 Reply)
Discussion started by: ampsys
1 Replies

4. Shell Programming and Scripting

Cannot get literal ampersand to display in SQL output

Gurus, Thanks so much for your help, in advance. I'm using ksh and outputting a literal string value to an output file, however, Unix isn't playing by SQL's rules. The ampersand character which I'm trying to disply as a knowledge base link is screwing up the output. Typically, the "&&" is... (1 Reply)
Discussion started by: WhoDatWhoDer
1 Replies

5. Red Hat

How to pass value of pwd as variable in SED to replace variable in a script file

Hi all, Hereby wish to have your advise for below: Main concept is I intend to get current directory of my script file. This script file will be copied to /etc/init.d. A string in this copy will be replaced with current directory value. Below is original script file: ... (6 Replies)
Discussion started by: cielle
6 Replies

6. UNIX for Dummies Questions & Answers

Comparing a String variable with a string literal in a Debian shell script

Hi All, I am trying to to compare a string variable with a string literal inside a loop but keep getting the ./testifstructure.sh: line 6: #!/bin/sh BOOK_LIST="BOOK1 BOOK2" for BOOK in ${BOOK_LIST} do if then echo '1' else echo '2' fi done Please use next... (1 Reply)
Discussion started by: daveu7
1 Replies

7. Shell Programming and Scripting

how do i print literal value?

hi guys!!! i wanna print literal value itself. for example, mycommand=$( ls -ltr ) "echo $mycommand" is not working... it runs the "ls" command. also "echo $"mycommand" " is not working either. it print out "mycommand" literally... how can i print out " ls -ltr " using variable "mycommand"? (4 Replies)
Discussion started by: curtis911
4 Replies

8. Shell Programming and Scripting

Expand an environment variable in sed, when the variable contains a slash

I'm trying to make a sed substitution where the substitution pattern is an environment variable to be expanded, but the variable contains a "slash". sed -e 's/<HOME_DIRECTORY>/'$HOME'/'This gives me the following error: sed: -e expression #1, char 21: unknown option to `s'Obviously this is... (2 Replies)
Discussion started by: Ilja
2 Replies

9. Shell Programming and Scripting

Insert a line including Variable & Carriage Return / sed command as Variable

I want to instert Category:XXXXX into the 2. line something like this should work, but I have somewhere the wrong sytanx. something with the linebreak goes wrong: sed "2i\\${n}Category:$cat\n" Sample: Titel Blahh Blahh abllk sdhsd sjdhf Blahh Blah Blahh Blahh Should look like... (2 Replies)
Discussion started by: lowmaster
2 Replies

10. Shell Programming and Scripting

Sed variable substitution when variable constructed of a directory path

Hello, i have another sed question.. I'm trying to do variable substition with sed and i'm running into a problem. my var1 is a string constructed like this: filename1 filerev1 filepath1 my var2 is another string constructed like this: filename2 filerev2 filepath2 when i do... (2 Replies)
Discussion started by: alrinno
2 Replies
Login or Register to Ask a Question