Need help using sed inside the loop.


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Need help using sed inside the loop.
# 1  
Old 06-03-2010
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 storing the files.
Code:
 
 
backup_files()
{
for file in `ls tmp*`
do
mv $file `echo $file |sed 's/tmp/fnl/'`
if [[! -d "${BACKUP_DIR}" ]]
then
mkdir "${BACKUP_DIR}" && chmod 744 "${BACKUP_DIR}"
fi
mv fnl* "${BACKUP_DIR}"
done
}

nw see the code for doing the cosmetic changes in the output file, - this has to be done on each and every file. before taking its backup

Code:
 
cleanup()
{
sed -n '1,$s/^ *//gp' INFORM_${DATE}_${id} | sed -n '1,$s/ *|/|/gp' | sed -n '1,$s/| */|/gp' | sed -n '1,$s/ *$//gp' >  INFORM_${DATE}_${id}.tmp
mv INFORM_${DATE}_{id}.tmp INFORM_${DATE}_{id}
sed -n '1,$s/NULL//gp' INFORM_${DATE}_{id} > INFORM_${DATE}_{_id}.tmp
mv INFORM_${DATE}_{id}.tmp INFORM_${DATE}_{id}
}

can some1 please help me with this. i am not sure how will i implement all the sed operations on all the files.
moreover, i am putting date here at the end of the file as suffix i want date in the below format but i unable to get tht .. please help me out with tht also.

Code:
 
date "+%Y%m%d%H%M%S"

not able to get this format at the end of the file.
# 2  
Old 06-03-2010
In the first case "backup_files" are you getting any error?

The same works fine with me.

Please also post , the input and the output you are expecting.

Regards
Ravi Sastry
# 3  
Old 06-03-2010
You have got lots of typos ( not sure they also appear in the actual code).


Code:
cleanup()
{
sed -n '1,$s/^ *//gp' INFORM_${DATE}_${id} | sed -n '1,$s/ *|/|/gp' | sed -n '1,$s/| */|/gp' | sed -n '1,$s/ *$//gp' >  INFORM_${DATE}_${id}.tmp
mv INFORM_${DATE}_${id}.tmp INFORM_${DATE}_${id}
sed -n '1,$s/NULL//gp' INFORM_${DATE}_${id} > INFORM_${DATE}_${id}.tmp
mv INFORM_${DATE}_${id}.tmp INFORM_${DATE}_${id}
}


Not sure what you are trying to replace..
but If you do want to apply this to each file, make this function to accept filename as the argument.

then call it inside the loop in this way,

Code:
cleanup $file

Also, there is "useless use of ls",

Code:
for file in `ls tmp*`

should be changed to:

Code:
for file in tmp*

there must be a space before the "!" in the test command.

regarding date, use something like this

Code:
DATE=$(date "+%Y%m%d%H%M%S")

# 4  
Old 06-04-2010
anchal sorry for typos. they are not in actual code, i always change the code variables before i pase them here. thats why all these typos.
i m going to run what all u said and will tell u the output soon.

thanks a lot
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Write a while loop inside for loop?

I'm taking a unix class and need to countdown to 0 from whatever number the user inputs. I know how to do this with a while or until loop but using the for loop is throwing me off.... I know I can use an if-then statement in my for loop but can I include a while loop in my for loop? (3 Replies)
Discussion started by: xxhieixx
3 Replies

2. Shell Programming and Scripting

If inside If loop

Hi All, Below is the very simple code snippet but it si giving me syntax error #!/bin/bash #To ensure If JMS directory exists or not ServerName=$(hostname) #To ensure If JMS directory exists or not echo $ServerName if ; then echo "Inside First If" if ; then echo 'JMS... (4 Replies)
Discussion started by: sharsour
4 Replies

3. Shell Programming and Scripting

help using EOF inside a for loop

I'm having problems with this example: #!/bin/bash for i in server1 server2 server3 do ssh $i <<EOF >> logfile.txt dat1=`date '+%m/%d/%y'` hst=`hostname` df -Ph | awk -v OFS=',' -v dat="$dat1,$hst" ' {print dat,$1,$6,$3,$4 } ' exit EOF done I'm... (1 Reply)
Discussion started by: kuliksco
1 Replies

4. 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

5. Shell Programming and Scripting

BASH loop inside a loop question

Hi all Sorry for the basic question, but i am writing a shell script to get around a slightly flaky binary that ships with one of our servers. This particular utility randomly generates the correct information and could work first time or may work on the 12th or 100th attempt etc !.... (4 Replies)
Discussion started by: rethink
4 Replies

6. Shell Programming and Scripting

Using 'su' inside a loop

Hi, I am using su within a for loop. As you might expect, it prompts for password during each loop execution. Here is my piece of code: for i in $LIST do if then DATABASE=`echo $i | awk -F "|" '{ print $1 }'` USER_ID=`echo $i | awk -F "|" '{ print $2 }'` su - apstage -c... (1 Reply)
Discussion started by: sugan
1 Replies

7. 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

8. Shell Programming and Scripting

Using variables created sequentially in a loop while still inside of the loop [bash]

I'm trying to understand if it's possible to create a set of variables that are numbered based on another variable (using eval) in a loop, and then call on it before the loop ends. As an example I've written a script called question (The fist command is to show what is the contents of the... (2 Replies)
Discussion started by: DeCoTwc
2 Replies

9. Shell Programming and Scripting

variable inside variable inside loop headache

Hi Gurus I have a file called /tmp/CMDB which looks like this serial: 0623AN1208 hostname: server1 model: x4100 assetID: 1234 I am writing a for loop that will go through this file line by line creating a variable of itself. Using the first iteration of the loop (i.e. the first line) as... (6 Replies)
Discussion started by: hcclnoodles
6 Replies

10. UNIX for Advanced & Expert Users

remsh inside of while loop

I have a space delimited file containing: hostname OracleSID connectstring I want to loop through the file and execute remsh to check the database processes. cat $filename | while read HOST SID CONNECT do { result=`remsh $HOST "ps -ef | grep pmon_${SID}$| grep -v grep"` if ........ (1 Reply)
Discussion started by: joettacm
1 Replies
Login or Register to Ask a Question