sed inside sed


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting sed inside sed
# 1  
Old 11-25-2011
sed inside sed

hey guys,

I'm going to make a sed file out of a list of words

words.dat:
Code:
802.11a
802.11b
802.11g
802.11n

my command:
Code:
awk '{print $0,"/spEC/g"}' words.dat |
awk '{print "s/"$0}'

current output
Code:
s/802.11a /spEC/g
s/802.11b /spEC/g
s/802.11g /spEC/g
s/802.11n /spEC/g
s/Player /spEC/g
s/Server /spEC/g

desired output
Code:
s/802.11a/spEC/g
s/802.11b/spEC/g
s/802.11g/spEC/g
s/802.11n/spEC/g
s/Player/spEC/g
s/Server/spEC/g

I don't want to have to have those extra spaces.

could you please help me with that.

tnx
# 2  
Old 11-25-2011
Code:
sed 's#^#s/#;s#$#/spEC/g#' myFile
awk '{print "s/" $0 "/spEC/g"} myFile

This User Gave Thanks to vgersh99 For This Post:
# 3  
Old 11-25-2011
Just remove the comma:
Code:
# awk '{print $0"/spEC/g"}' words.dat
802.11a/spEC/g
802.11b/spEC/g
802.11g/spEC/g
802.11n/spEC/g

Also, you don't need 2 awk commands:
Code:
# awk '{print "s/"$0"/spEC/g"}' words.dat
s/802.11a/spEC/g
s/802.11b/spEC/g
s/802.11g/spEC/g
s/802.11n/spEC/g

This User Gave Thanks to CarloM For This Post:
# 4  
Old 11-25-2011
Quote:
Originally Posted by vgersh99
Code:
sed 's#^#s/#;s#$#/spEC/g#' myFile
awk '{print "s/" $0 "/spEC/g"} myFile

thanks a lot it worked perfect Smilie

but there is one small problem, in the word.dat file I have some words like:
Code:
JPEG / JPG
TIFF
PNG
BMP
GIF
DivX
Xvid
MPEG1
MPEG2
MPEG4
WMV
H.263
3GP/3GPP

you see that some of them have "/" in the middle, so the problem is the output sed file like this would not work, because it has extra "/"
So what structure you suggest for the sed file:

Code:
s/JPEG / JPG/spEC/g
s/TIFF/spEC/g
s/PNG/spEC/g
s/BMP/spEC/g
s/GIF/spEC/g
s/DivX/spEC/g
s/Xvid/spEC/g
s/MPEG1/spEC/g
s/MPEG2/spEC/g
s/MPEG4/spEC/g
s/WMV/spEC/g
s/H.263/spEC/g
s/3GP/3GPP/spEC/g

for example "3GP/3GPP" has extra "/"
# 5  
Old 11-25-2011
The separator for sed does not have to be '/' - you can equally use '#' or whatever.
Code:
awk '{print "s#"$0"#spEC#g"}' words.dat

This User Gave Thanks to CarloM For This Post:
# 6  
Old 11-25-2011
I don't quite follow the logic...
Why are you creating a sed file with awk/sed, while you can do the actual substitution with awk directly?
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Using sed inside shellscript

Hi All, I am trying to use sed command inside a shell script. The same sed command is working on command line, however its not working while using inside a shell script. From various sources i found , it could be done by using -i option, but its not working as well. sed... (11 Replies)
Discussion started by: gotamp
11 Replies

2. Shell Programming and Scripting

Use of sed inside perl

$string="test.binary.value"; $toggle="test.binary.value=true"; host=test.server.com my $returnCode = system ( "ssh $host 'cp -p /tmp/testfile /tmp/testfile.bkup; sed -i 's/\$string.*/\$toggle/g' /tmp/testfile'" ); cat testfile test.binary.value=false Scenario: I am trying to... (3 Replies)
Discussion started by: Tuxidow
3 Replies

3. UNIX for Dummies Questions & Answers

Array inside sed

Hi guys Let me at first describe the whole thing that I'm trying to do. Lets say I have 100 files like the following one. Ow 1230 16.000000 -0.834000 16.083957 1.751652398 -17.20094528 -4.450623277 Hw 1231 ... (6 Replies)
Discussion started by: saleheen
6 Replies

4. UNIX for Dummies Questions & Answers

sed inside awk

What I want to do is delete everything upto the last underscore (_) in column 2. awk '{ $2=$(echo $2 | sed 's/.*_//'); print $0}' Sed works fine if I echo the string into it, this doesnt work inside awk. What am I doing wrong? Similarly, how do I store the substring starting with a... (4 Replies)
Discussion started by: senhia83
4 Replies

5. Shell Programming and Scripting

Variable inside sed

Problem: Need to read contents from a file and use that value inside sed as avariable. sample code below. THe below code replaces contents inside file123 for matched string with "$x" value only. but we want the value of x which is read from TextFile.txt to go in there. cat TextFile.txt|while... (3 Replies)
Discussion started by: cvsanthosh
3 Replies

6. Shell Programming and Scripting

Using commands inside sed substitution

Dear Friends, Please let me know how to use the date command inside the substitution flag replacement string. echo "01 Jan 2003:11:00:06 +0100" | sed 's/\(.*\)/`date -d \1 "+%Y%m%d%H%M%S"`/' I want to supply \1 string to Here mention below as part of replacement string, date -d <Here>... (5 Replies)
Discussion started by: tamil.pamaran
5 Replies

7. Shell Programming and Scripting

sed inside sed for replacing string

My need is : Want to change docBase="/something/something/something" to docBase="/only/this/path/for/all/files" I have some (about 250 files)xml files. In FileOne it contains <Context path="/PPP" displayName="PPP" docBase="/home/me/documents" reloadable="true" crossContext="true">... (1 Reply)
Discussion started by: linuxadmin
1 Replies

8. Shell Programming and Scripting

Need help using sed inside the loop.

Hi, i have written a script. it collects data based on the sql queries executed by it. i have multiple output files. after the output file is made i need to do some cosmetic changes in the files and then store them. i am unable to use sed conditions inside the loop. see below code for... (3 Replies)
Discussion started by: dazdseg
3 Replies

9. UNIX for Dummies Questions & Answers

SED inside while loop

Hi, im having problem creating a loop using my code: aside from the fact that the 1st variable (VAR) does not increment, it loops more than the expected output. for sample purposes, test csv contains 3 lines. #get number of lines in the file lines=$( wc -l < test.csv ) ... (5 Replies)
Discussion started by: paoie
5 Replies

10. Shell Programming and Scripting

Using variable inside 'sed'

Dear all, How can I amend the following command to use variable inside 'sed' ? str=`sed -e 's/orig_string/${var}/g' ${sourcefile}` Thanks, Rocky (3 Replies)
Discussion started by: Rock
3 Replies
Login or Register to Ask a Question