Help, while loop and sed not producing desired output


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Help, while loop and sed not producing desired output
# 8  
Old 12-27-2010
Thanks anurag.singh!

Your awk example definitively gives me the output I was after. Think there was a typo between the a and the $1, had to change the '{' to a '['. Would you know why my script does not work? As per DGPickett's remark, it appears that sed is not able to see $SEARCH and $REPLACE. sed just sees the whole output of each line as $SEARCH. Almost like its not seeing the format of "SEARCH[space]REPLACE". I can't seem to figure out how to get sed to see the expanded/variablized values of $SEARCH and $REPLACE. Thanks again.

---------- Post updated at 05:18 PM ---------- Previous update was at 05:02 PM ----------

Thanks again DGPickett but I get the same output with the usage of sed to clean up the control file. Here is a copy of the updated script:
Code:
#!/bin/bash

sed '
s/#.*//
s/[ \t]*$//
/^$/d
' controlfile | while read SEARCH REPLACE
    do
        sed  's/$"SEARCH"/$"REPLACE"/' sourcefile
    done


Here is its output:
Code:
search1


search2
search3



search4
search1


search2
search3



search4
search1


search2
search3



search4
search1


search2
search3



search4

I think you are correct with your statement that sed does not see the expanded values for $SEARCH and $REPLACE. If I do echo $SEACH, I get the whole line echoed (not what I would expect). If I do echo $"SEARCH" I only get the first column (what I would expect). Doing echo $"REPLACE" gives the second column. Doing just a echo $REPLACE gives me blank lines. I think the scripts logic is correct - figuring I am just missing some semantic in passing variables to sed.
# 9  
Old 12-27-2010
The side effect of putting sed inside of the while loop is that the sourcefile is processed for each search/replace pair in the control file. May I suggest:
Code:
# Args: Control-File Source-File

NAME=$(basename ${0})
FILE=/tmp/${NAME}.${$}
trap "rm -f '${FILE}'" EXIT

awk '/^#/ { next; } NF == 2 { print "s/" $1 "/" $2 "/"; }' "${1}" > "${FILE}"
sed -f "${FILE}" "${2}"


This User Gave Thanks to m.d.ludwig For This Post:
# 10  
Old 12-27-2010
In post #8, sed under while loop should be (-e not really needed here):
Code:
sed "s/$SEARCH/$REPLACE/" sourcefile

Also as mentioed in post #9, sed under while, will run for each search/replace pair in the control file (source file being processed that many times, that is why output is repeated that many times), so you need to use -i option if supported.
Code:
sed -i "s/$SEARCH/$REPLACE/" sourcefile

This will modify source file itself.
I believe in such scenarios (where fields in one file are being modified based on values of fields in another file), awk provides better solutions.

Last edited by anurag.singh; 12-27-2010 at 06:42 PM..
# 11  
Old 12-27-2010
One side effect of using awk as shown in post #6:
Quote:
awk 'NR==FNR && !/^#|^$/ {a[$1]=$2;next;} NR==FNR {next;} {if(a[$1]) $1=a[$1]; print;} Control_file Source_file
is that whitespace would not be preserved. This may, or may not be, important. But the request was to replace string "pattern" with "replacement", not replace a field with a given value with a new value. Or was it implied?

IMHO, if the request was for word/field replacements, or with more complexity, I would probably have gone with PERL with \b in the regex. I also have to admit I am not a fan of in-place updates. Yes, it is very useful at times, but it has bit me in the posterior more times than I care to count.
# 12  
Old 12-27-2010
Well, just take it in pieces, printing variables to stderr with delimiters so you can see what is up "'$xxxx'" ! Often an extra space or a tab not a space ruins the match, and then there is no translation.

---------- Post updated at 09:06 PM ---------- Previous update was at 09:04 PM ----------

Code:
 
sed "s/$SEARCH/$REPLACE/"
 
or 
 
sed 's/'"$SEARCH"'/'"$REPLACE"'/'

# 13  
Old 12-29-2010
Thanks everyone for your help. m.d.ludwig's combination of awk and sed in post #9 produces just what I was looking for.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Printing the output of sed using a loop

So I am writing a bash script that will search a file line by line for unix timestamps, store all of the timestamps into an array, then check how many of those timestamps were created within the last hour, and finally increment a counter every time it finds a timestamp created within the last hour.... (6 Replies)
Discussion started by: jsikarin
6 Replies

2. Shell Programming and Scripting

awk script not producing output

Hi, I have a text file with some thousands of rows of the following kind (this will be referred to as the inputFileWithColorsAndNumbers.txt): Blue 6 Red 4 Blue 3 Yellow 4 Red 7 Colors in the left column and a number in the right one for each line. I want to run an awk... (5 Replies)
Discussion started by: Zooma
5 Replies

3. Shell Programming and Scripting

Help!! needed to get the desired output

Am in need of your help to get the desired output. nameSECURITY.SERVICES.CONFIG:GETVALUEisPrefetchedNsAccessLast2013-09-13 10:50:13 MESTsAccessTotal1sRunningcHitLastnamePUBLIC.SERVER:INVOKEisPrefetchedNsAccessLast2013-09-17 15:02:05... (5 Replies)
Discussion started by: rocky2013
5 Replies

4. Shell Programming and Scripting

How to grep the desired output and output to a file?

currently I have process from a raw file to this stage ALTER TABLE "EXCEL_ADMIN"."TC_TXN_VOID" ADD CONSTRAINT "PK_TC_TXN_VOID" PRIMARY KEY ("TC_TXN_IID") ALTER TABLE "EXCEL_ADMIN"."TC_TXN_AMT" ADD CONSTRAINT "PK_TC_TXN_AMT" PRIMARY KEY ("TC_TXN_AMT_IID") ALTER TABLE... (10 Replies)
Discussion started by: jediwannabe
10 Replies

5. Shell Programming and Scripting

need to get the desired output

Below is the my cide which is working fine but I am not getting the output indesired format.there is some problem in alignment.Can someone help me to correct this? if ]; then summary=$( echo -e "Please review the log file of auto coloclean utility.\n"; echo -e... (2 Replies)
Discussion started by: anuragpgtgerman
2 Replies

6. Shell Programming and Scripting

SED - output not desired

echo '0x3f 0xfa ae 0xeA' | sed '/0x/ y/abcdef/ABCDEF/' output: 0x3F 0xFA AE 0xEA echo '0x3f 0xfa ae 0xeA' | sed -r '/0x{2}/ y/abcdefg/ABCDEFG/' output: 0x3F 0xFA AE 0xEA my expected output: 0x3F 0xFA ae 0xEA What I want to achieve is change all hexadecimals to UPPER case(only those... (6 Replies)
Discussion started by: kevintse
6 Replies

7. Shell Programming and Scripting

sed in while loop producing arithmetic output

Hi all Just wondering if someone can help me with this. I'm trying to write a script that processes the output of another file and prints only the lines I want from it. This is only the second script I have written so please bare with me here. I have referred to the literature and some of the... (3 Replies)
Discussion started by: javathecat
3 Replies

8. Shell Programming and Scripting

Sed subsitution on loop output in shell script

I have a forloop which checks a log for a set of 6 static IP addresses and each IP found is logged to a file which is then mailed to me. After the forloop I always have a text file that may contain up to 6 IP addresses or may contain 0. What I want to do is substitute the IP addresses (if any)... (2 Replies)
Discussion started by: Moxy
2 Replies

9. Shell Programming and Scripting

how to get desired output after redirection

hi i am running script which contains the commmnds and i am redirecting the script output to a file. like ./script 1> result.txt 2>&1 the above redirection is not working for commands when run in background in a script. but the problem here result.txt containg output which is repeated.... (3 Replies)
Discussion started by: raji
3 Replies

10. Shell Programming and Scripting

Help me in getting the desired output

I wanted to put "|" this sign at starting and at end of every field but its not working with first field like Currently the out put is : abc | abc | abc | xyz | xyz | xyz | But I want the out put in this form: | abc | abc | abc | | xyz | xyz | xyz | plz help me. (2 Replies)
Discussion started by: akash
2 Replies
Login or Register to Ask a Question