Variable expansion in sed


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Variable expansion in sed
# 1  
Old 02-16-2011
Variable expansion in sed

The objective of the code below is to create sed script to be later executed. However, it bonks because $ARCHIVENAME expands to a directory specification so the forward slashes cause problems. I can think of a few solutions that would involve redesigning the process, but I'm hoping there might be something I can do inline by way of escaping the slashes that might allow me to keep my basic logic intact.
Code:
cut -f1 -d "|" $ARCHIVENAME/changes.dat | sed "s/^.*/grep & $ARCHIVENAME\/clean_*/g" > $ARCHIVENAME/create_message_names

Thanks in advance,
Al

Last edited by Scott; 02-16-2011 at 02:29 PM.. Reason: Please use code tags
# 2  
Old 02-16-2011
Code:
cut -f1 -d "|" $ARCHIVENAME/changes.dat | sed "s#^.*#grep & $ARCHIVENAME/clean_*#g" > $ARCHIVENAME/create_message_names

# 3  
Old 02-16-2011
You can use nearly any non-special character as the delimiter, not just forward slash. Try an expression like s#a#b#g instead of s/a/b/g
If you can pick a character you're guaranteed will never show up in your paths, all the better.
[edit] Jinx!
# 4  
Old 02-16-2011
Code:
.... sed "s:^.*:grep & $ARCHIVENAME/clean_*:g"  ....

If your file is a "real" *.dat file, it may not be a good idea to process it as if it were an pure ASCII text file (i guess the first 1041 bits should be skipped or something like this.
Maybe you should use the "strings" command at first.

By the way using the global substition in that case seems to me quite erroneous since ^.* will match the entire line, only 1 substitution will occure so you should remove the g.

If your final goal is to grep a list of pattern to $ARCHIVENAME/clean_*, you may want to try to use fgrep or grep -f or egrep -f instead of building a file with multiple
grep <pattern> $ARCHIVENAME/clean_* in it.

Last edited by ctsgnb; 02-16-2011 at 03:00 PM..
# 5  
Old 02-16-2011
As has been mentioned, you could use a different regular expression delimiter, a character that you know will never appear in the pathname.

Instead of
Code:
sed "s/^.*/grep &/g"

you can use
Code:
sed "s@^.*@grep &@g"

if the at symbol will never be used.

That said, interpolating values like that is fragile when the tool does not support some form of quoting to strip special meaning from metacharacters (at least in this case it's in the replacement text, otherwise you'd have more special characters to worry about if the parameter expansion were ocurring in the regular expression section of the sed command. Even so, ampersand is still special in this context, though admittedly not very likely to appear in a pathname.).

To be safe, to simplify, and to minimize stress, I would tend to prefer something that avoids interpolating into special contexts as much as possible. Possible alternatives:

Code:
awk -F\| -v aname="$ARCHIVENAME" {print "grep " $1 " \"" aname "\"/clean_*" > (aname"/create_message_names")}'

or
Code:
while IFS=\| read -r pattern junk; do
    printf 'grep %s "%s"/clean_*\n' "$pattern" "$ARCHIVENAME" > "$ARCHIVENAME"/create_message_name
done

Regards,
Alister

Last edited by alister; 02-16-2011 at 03:35 PM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Bash variable expansion

Hello. The file /etc/fstab contains UUID=957c3295-9944-1593-82e2-2b90dede4312 / ext4 noatime,discard,acl,user_xattr 1 1 I fill a variable SOME_LINE=$( cat /etc/fstab | grep \/\..*ext4 | grep noatime,discard )echo $SOME_LINE... (3 Replies)
Discussion started by: jcdole
3 Replies

2. Shell Programming and Scripting

sed variable expansion fails for substitution in range

I'm trying to change "F" to "G" in lines after the first one: 'FUE.SER' 5 1 1 F0501 F0401 F0502 2 1 F0301 E0501 F0201 E0502 F0302 3 1 F0503 E0503 E0301 E0201 E0302 E0504 F0504 4 1 F0402 F0202 E0202 F0101 E0203 F0203 F0403 5 1 F0505 E0505 E0303 E0204 E0304 E0506... (10 Replies)
Discussion started by: larrl
10 Replies

3. Shell Programming and Scripting

Help required regarding variable expansion in UNIX

Hello, I have one variable coming from one file: abc=$xyz/filename.txt where $xyz is defined in .profile file as say, /usr/dev/src i am passing abc variable to one perl script as input parameter. perl 123.pl -s $abc But inside the perl script execution, the variable $abc is just... (1 Reply)
Discussion started by: vikas_trl
1 Replies

4. Shell Programming and Scripting

Protecting variable indicator ($) from expansion

Hello, I use a lot this command to edit a bunch of files at once find . -name filename" | xargs -ifoo sh -c 'echo foo ; sed "s/pattern1/pattern2/" foo > ./tmp ; mv -f ./tmp foo' I'm trying to put a function on my .bashrc file. function loopSed() { local filename=$1 local... (2 Replies)
Discussion started by: phollox
2 Replies

5. UNIX for Dummies Questions & Answers

Can I use a variable with brace expansion?

So, I was bored on the train today, and was thinking of ways to loop through elements of an array. I came up with the following simple script, but it doesn't work as brace expansion doesn't seem to work with variables. Is there something I'm missing, or does the shell just not work like this? ... (4 Replies)
Discussion started by: DeCoTwc
4 Replies

6. Shell Programming and Scripting

delay variable expansion

Hi forum, in my bash script I've many lines executing commands with redirection to log files. ... xyz_cmd 2>&1 > $BASENAME.$LINENO The trailing part of these lines doesn't look nice and I like to put it into a variable. The (not working) idea is something like that ... (3 Replies)
Discussion started by: wolfi089
3 Replies

7. Shell Programming and Scripting

Bash variable delayed expansion?

i write a batch file , here is the content. dirname='date +%Y-%m-%d' mkdir dirname but it doen's work, it just create a folder named date and +%Y-%m-%d. i have tried run the command seperately in the bash prompt. after the first sentence executed , i use $dirname to watch the value of... (4 Replies)
Discussion started by: premotheus
4 Replies

8. UNIX for Dummies Questions & Answers

sed insert command and variable expansion/command substitution

I know this script is crummy, but I was just messing around.. how do I get sed's insert command to allow variable expansion to show the filename? #!/bin/bash filename=`echo $0` /usr/bin/sed '/#include/ { i\ the filename is `$filename` }' $1 exit 0 (8 Replies)
Discussion started by: glev2005
8 Replies

9. UNIX for Dummies Questions & Answers

Variable brace expansion

I'm in the habit of using the following type of loop structure: for num in `seq $1 $2` do command doneWhile `seq $1 $2` is not exactly a huge resource hog, I would like to learn a better way. It seems that brace expansion is a good way to go: for num in {3..10}The problem, though, is... (2 Replies)
Discussion started by: treesloth
2 Replies

10. UNIX for Dummies Questions & Answers

ksh on HP-UX -- variable expansion

We have a script that runs in ksh on HP-UX 11.11. It takes three arguments. The last argument can be a filename or wildcard character. For example: script -s hello -t goodbye '*.d*' In a case such as this, I would wrap single quotes around the final argument because I dont want the shell to... (4 Replies)
Discussion started by: dangral
4 Replies
Login or Register to Ask a Question