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
# 1  
Old 12-27-2010
Help, while loop and sed not producing desired output

Hello everyone,

I need some assistance with what I thought would have been a very simple script.

Purpose of Script:
Script will parse through a source file and modify (search/replace) certain patterns and output to stdout or a file. Script will utilize a "control file" which will contain what to search for and what to replace it with. "Control file" will be used against the "source file".

Script Goals:
  1. Comments (lines beginning with #) and blank lines in the "control file" should be ignored by the script. [DONE]
  2. "Control file" will contain structured data. Deliminator with be initially a blank space, will be changed to a semi-colon. [DONE]
  3. The script works Smilie [This is where I need help]
Example of "Control file"
Code:
#this is a comment, should be skipped



#blank lines should also be skipped


#data is structured as SEARCH REPLACE

#ok, here is some data
search1 replace1
search2 replace2


search3 replace3


#more data 
search4 replace4

Example of "Source file"
Code:
search1


search2
search3



search4

Example of the script
Code:
#!/bin/bash

while read SEARCH REPLACE
    do
        sed -e 's/$SEARCH/$REPLACE/' sourcefile
    done < <(awk '!/^#|^$/ { print $0}' controlfile)

Example of script output to stdout
Code:
search1


search2
search3



search4
search1


search2
search3



search4
search1


search2
search3



search4
search1


search2
search3



search4

Here I would have expected a bunch of replaces outputted (replace1, replace2, replace3, ...).

I can't seem to figure out what I am doing wrong. Any help would be greatly appreciated. Thanks in advanced.
# 2  
Old 12-27-2010
Single quotes after sed do not allow variable expansion, use double. Also, -e not necessary.
This User Gave Thanks to DGPickett For This Post:
# 3  
Old 12-27-2010
Thanks for the quick response DGPickett. I originally tried replacing the single quotes with double quotes for the sed portion of the script with no luck. I've gone ahead and replaced all single quotes with double quotes and still get the same exact output. Here is a copy of the updated scripted:
Code:
#!/bin/bash

while read SEARCH REPLACE
    do
        sed -e "s/$SEARCH/$REPLACE/" sourcefile
    done < <(awk "!/^#|^$/ { print $0}" controlfile)

And here is a copy of the output it yields:
Code:
search1


search2
search3



search4
search1


search2
search3



search4
search1


search2
search3



search4
search1


search2
search3



search4

# 4  
Old 12-27-2010
Will require more corrections. homework?
Rules for Homework & Coursework Questions Forum
# 5  
Old 12-27-2010
Hi,

This script is not homework.
# 6  
Old 12-27-2010
Code:
awk 'NR==FNR && !/^#|^$/ {a[$1]=$2;next;} NR==FNR {next;} {if(a[$1]) $1=a[$1]; print;} Control_file Source_file

This User Gave Thanks to anurag.singh For This Post:
# 7  
Old 12-27-2010
The while read and sed look OK, but take your < < ( ... ) and hang it on cat -vet to see what the control inputs are.

I am a sed man, so I would have used sed to clean the control up like this (\t s/b a real tab):
Code:
 
sed '
  s/#.*//
  s/[ \t]*$//
  /^$/d
 ' control_file | while read SEARCH REPLACE
 .
 .
 .
 .

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