Tell me more ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

Scenario here is I need to replace "$$file_name=" value.. I have some 150 file names in a file as mentioned below..

Eg: $$file_name=abc.bbb.ccc to_be ---> $$file_name=abc.bbb.cccV1

AS_IS :

$$a=abc.txt
$$file_name=abc.ddd.aaa
$$directory=/uasdua/asdsas

$$a=c.txt
$$file_name=ac.dd.ac
$$directory=/uasdua/asdsas

$$a=b.txt
$$file_name=ee.d.a
$$directory=/uasdua/asdsas
.
.
.
.
.
.
$$a=b.txt
$$directory=/uasdua/asdsas
$$file_name=e.d.a

to_be:

$$a=abc.txt
$$file_name=abc.ddd.aaaV1
$$directory=/uasdua/asdsas

$$a=c.txt
$$file_name=ac.dd.acV1
$$directory=/uasdua/asdsas

$$a=b.txt
$$file_name=ee.d.aV1
$$directory=/uasdua/asdsas
.
.
.
.
.
.
$$a=b.txt
$$directory=/uasdua/asdsas
$$file_name=e.d.aV1

This is what i've tried

select NAME from xxx where DATE='07/12/1999' > Name_file

#version will be same for that day
FILE_VERSION=`db2 -x "select version from xxx where DATE='07/12/1999'"` > version_name
FILE_VER=`cat version_name | head -1`

while read filename
 do
    FILE_NAME1=`echo ${filename}${FILE_VER}`
    sed -f pattern.txt input > output
done < file_name11

pattern.txt

echo "$filename"
echo "$FILE_NAME1"
s/${filename}/${FILE_NAME1}/g ---> If I hardcode this one its not working..the value is not replaced..
s/ee.d.a/ee.d.aV1/g --> this is working

Do i need to explictly pass it..I have 150 such file names in a file ?


share|improve this question
1  
You are mixing sed script shell files and shell script files. In sed-script files you cannot use variable replacement or echo. Do not use an extra sed input file but write everything in one shell script. –  jofel Jul 16 at 8:54
add comment

6 Answers

Here's how I would accomplish this:

$ sed -e '/^$$file_name/ s|$|V1|' AS_IS

this finds strings that start with "$file_name..". For those strings we'll do a search and replace. The $ is the end of the string, so we're essentially appending a V1 to each of these matched strings. You can redirect these results to a file like so:

$ sed -e '/^$$file_name/ s|$|V1' AS_IS > to_be
share|improve this answer
add comment

You can use

sed -i -e 's/\$\$file_name=.*/&V1/' file.txt   
share|improve this answer
 
Thanks for the quick response..its not wrking.i have the below requirement. –  user43067 Jul 15 at 14:34
 
@user43067 I adapted the answer. –  jofel Jul 15 at 14:38
 
hey thanks for the quick responce bt need an info on the below sed -f pattern.txt input > output in pattern.txt i'm using the list of commands cant i harcode that values i mean if i giv in the below format its working s/a/b/g but if i gve in the below format.. s/$a/$b/g where $a=1 and $b=2 its not working..please suggest –  user43067 Jul 15 at 15:25
add comment

Try this solution. Worked for me:

sed -i 's/\$\$file_name.*/&V1/' foobar

where foobar is your input file.

share|improve this answer
 
Its generating error if i do the same in while loop –  user43067 Jul 15 at 15:30
 
What kind of error? Syntax error? –  unxnut Jul 15 at 17:05
add comment

I would write that like

sed -i '/^\$\$file_name=/ s/$/V1/' file
share|improve this answer
 
db2 -x "select NAME from xxx where ACCT_DT='07/12/2013'" > file_name11 FILE_VERSION=db2 -x "select Version from xxx where date='07/12/2013'" while read filename do echo "$filename" FILE_NAME1=echo ${filename}${FILE_VERSION} echo $FILE_NAME1 sed -f pattern.txt input > output done < file_name11 pattern.txt echo "$filename" echo "$FILE_NAME1" s/${filename}/${FILE_NAME1}/g but in the last line s/${filename}/${FILE_NAME1}/g is nt working..i need to explicitly pass the valueslike s/a/b/g –  user43067 Jul 15 at 15:34
3  
You can't put properly formatted code in a comment. Please edit your question. –  glenn jackman Jul 15 at 15:49
 
I tried to format your code but looks like you have errors in there. Can you format your code in the question so that we can see what mistakes you may be making and suggest a solution. –  unxnut Jul 15 at 17:04
add comment

If you want to use a "command file" with sed you can put the following in the file

s/\$\$file_name=.*/&V1/

The run the process as follow

sed -f pattern.txt <AS_IS >to_be
share|improve this answer
 
Error :sed: Function /^$$file_name/ s|$|V1 cannot be parsed. –  user43067 Jul 16 at 9:35
add comment

This was the solution I went with:

while read filename
 do
echo "s/${file_name}(0)/${file_name}(${file_ver})/g" >> replaceMFNames.txt    
done < file_name11
sed -f replaceMFNames.txt inputfile > outputfile
share|improve this answer
 
can you please mark this answer as the one that solved your question? –  Braiam Jul 21 at 4:47
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.