sed command within script wrongly deleting the last line


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting sed command within script wrongly deleting the last line
# 1  
Old 03-15-2018
sed command within script wrongly deleting the last line

Hi,

I have a shell script which has a for loop that scans list of files and do find and replace few variables using sed command. While doing this, it deletes the last line of all input file which is something wrong. how to fix this. please suggest. When i add an empty line in all my input file, it fixes the issue. but i look for any other suggestion that will avoid adding this empty line.

my code snippet is:

Code:
for j in `cat $tmp_tilda_var`

do
for i in `cat $tmpfile`
do

        tilda_value1=`grep ^${j} ${ENV_FILE}`
        echo $tilda_value1
        tilda_value=`echo "${tilda_value1}" | cut -d"=" -f2`
        tilda_var1=~${j}~
        sed -e 's#'"$tilda_var1"'#'"$tilda_value"'#g' $i >$i.tmp && mv $i.tmp $i
done
done

where i - is the input file
tilda_var1 - is the pattern to be replaced
tilda_value - is the final string will go in place of variable tilda_var1

Last edited by Scrutinizer; 03-15-2018 at 03:41 AM.. Reason: code tags
# 2  
Old 03-15-2018
Without more detailed, representative info on input files' and variables' contents it is very difficult for me - and mayhap others in here - to reproduce and understand (and even less comment on possible improvements of) the problem.
There's no obvious bug in your code snippet, so only generic suspicions can be uttered: non-*nix text files (DOS line terminators, missing terminator at end-of-file), unexpected behaviour of the for loops, regex special characters in the respective shell variables, ...
# 3  
Old 03-16-2018
RudiC,

Thanks for your response.

I digged into this issue, the input files are coming from accurev SCM tool to my unix location. somehow it is removing the last line when it processes all the input files through this for loop and sed. But if i edit any file in vi mode before running my script and saving back after adding an empty line and removing it again (means no actual change to the file) and saving. now when i run the script, that particular file contents are good. It is not removing the last line now. My input files are with extension .txt, .cfg, .properties, .ksh.

As a workaround what i did is, added the below code which is adding an empty line at the end of all my input files before it hits the for loop and sed command.

Code:
for k in `cat $tmpfile`
do
echo >> $k
done

I know it is not actual fix. Still i added this to move ahead.

For your comments,
i can say, my input file call it as a.txt contain some variables with ~, say ~variableA~ in some places.

a.txt:
Code:
line1
line2
~variableA~
line4

my other file call it as b.txt, contains this list of variables with values, say
b.txt:
Code:
variableA=/proj/apps/sun
variableB=/tmp

For loop will read all variables in b.txt one by one, and go to other for loop to read all input files one by one, find replace the variables with values.

Let me know if anything else i can try. Thanks again.
Moderator's Comments:
Mod Comment To keep the forums high quality for all users, please take the time to format your posts correctly.

First of all, use Code Tags when you post any code or data samples so others can easily read your code. You can easily do this by highlighting your code and then clicking on the # in the editing menu. (You can also type code tags [code] and [/code] by hand.)



Continued refusal to properly format your posts may result in you being placed in read-only mode for a few days or, in extreme cases, being permanently banned from this site.

Last edited by Don Cragun; 03-16-2018 at 03:22 AM.. Reason: Add CODE and ICODE tags.
# 4  
Old 03-16-2018
Quote:
Originally Posted by rbalaj16
. . .

Let me know if anything else i can try. Thanks again.

. . .
Can't. The specification is too sparse (for me) to analyse the error you describe, and the samples don't allow to reproduce it. So don't expect to learn what is going wrong.

Let me open my magic box, dig out my soft skills, and try to paraphrase what I infer from your data and what you say:
There is a file with replacement definitions in the shape varA=ValA, and a (or some) file(s) in which to-be-replaced entries are enclosed in ~ . Entries found are to be substituted by the defined new values.

SHOULD this be close to what you want, consider this proposal:
Code:
sed 's:^:s#~:; s:=:~#:; s:$:#:; ' b.txt | sed -f- a.txt
line1
line2
/proj/apps/sun
line4


Last edited by RudiC; 03-16-2018 at 08:17 AM..
# 5  
Old 03-16-2018
RudiC,

I get error with that sed command. once we fix this command, i need to put it in a for loop for processing list of all my input files and see if i do not get my original issue here too.

and other part is, my input file may contain other ~variables~ which are not part of a.txt. so those should be remain same.


Code:
$more b.txt
line1
line2
~variableA~
line4

$more a.txt
variableA=/proj/apps/sun
variableB=/tmp

$sed 's:^:s#~:; s:=:~#:; s:$:#:; ' b.txt | sed -f- a.txt
Cannot open pattern-file: -

OS version:
SunOS ahs2518 5.10 Generic_150400-27 sun4v sparc sun4v

Thanks


Moderator's Comments:
Mod Comment Seriously: Please use CODE tags as required by forum rules!

Last edited by RudiC; 03-16-2018 at 12:44 PM.. Reason: Changed CODE tags.
# 6  
Old 03-16-2018
Please use code tags in your posts!
--
If you want all variable=value pairs from a.txt then you better process it in a while loop.
Code:
filenames=`cat $tmpfile`
while IFS="=" read var value
do
  [ -n "$value" ] || continue
  for f in $filenames
  do
    echo "replacing '~$var~' by '$value' in '$f'"
    sed 's#~'"$var"'~#'"$value"'#g' "$f" >"$f.tmp" && mv -f "$f.tmp" "$f"
  done
done < a.txt

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

sed working on command line but file unchanged when execute with Shell script

I have a simple task to replace unix line feed end of line characters with carriage returns. When I run the following “change file in place” sed instruction from the command line all the Line feeds are successfully replaced with Carriage returns. sed -i 's/$/\r/' lf_file.txt But that same... (1 Reply)
Discussion started by: hawkman2k
1 Replies

2. Shell Programming and Scripting

Sed: deleting last line prevents '$' address from working in the multi-script invocation

It looks like if matching and deleting the last line confuses 'sed' so it does not recognize '$' address. Consider: sed -e '/^3/d' -e '$ a text' supposed to delete a line starting with '3' and then append 'text' after the last line of input. But, if it is the last line of input which starts... (2 Replies)
Discussion started by: msz59
2 Replies

3. Shell Programming and Scripting

Using sed for deleting the first word of each line?

sed /'1-2'/&^/ filename suppose there is a file containing three lines , how do we do delete the word from each line? hyter efr frf rerfer efe ewd cdcf evrfgf erfv the output has to look like frf ewd erfv (2 Replies)
Discussion started by: Rajeev Nukala
2 Replies

4. Shell Programming and Scripting

sed command throwing error while deleting a line from a file

Hi all, I ahve a program which has to delete a line in a file... if i run the sed command through shell prompt it works fine. But if run it using code its throwing error. May i know where i am doing wrong. the file has 3 lines # cat /root/.ssh/known_hosts... (4 Replies)
Discussion started by: vivek d r
4 Replies

5. Shell Programming and Scripting

sed - deleting each line up to a word

Hi there, I'd like to delete the beginning of a line up until it finds a certain word or character string: in this case, I'd like to delete each line up to the word "mounting". Thanks ;) Susan (12 Replies)
Discussion started by: kitykity
12 Replies

6. Shell Programming and Scripting

deleting blank line and row containing certain words in single sed command

Hi Is it possible to do the following in a single command /usr/xpg4/bin/sed -e '/rows selected/d' /aemu/CALLAUTO/callauto.txt > /aemu/CALLAUTO/callautonew.txt /usr/xpg4/bin/sed -e '/^$/d' /aemu/CALLAUTO/callautonew.txt > /aemu/CALLAUTO/callauto_new.txt exit (1 Reply)
Discussion started by: aemunathan
1 Replies

7. Shell Programming and Scripting

small sed script on command line.

Can anyone help me get this small sed script to work in shell on the command line? I need it in a one liner really as i want to edit many scripts in a for loop and dont want to have to invoke a separate script each time. #!/bin/sh sed '/mailx\ -s.*$ { i\ #Comment above mailx line ... (5 Replies)
Discussion started by: lavascript
5 Replies

8. Shell Programming and Scripting

Deleting a line from a file with sed and awk?

cat file.txt fvnuiehuewf ruevhxncvkjrh zxjvurhfuwe jkhvBEGINvfnvf ijrgioe Trying to delete a line that has the pattern "BEGIN" cat sedtest filename=file.txt pattern=BEGIN sed "/^$pattern/d" "$filename" (9 Replies)
Discussion started by: cola
9 Replies

9. Shell Programming and Scripting

deleting particular lines and moving a line up using perl/sed

Hi, I need convert a dump file in the following format : (please note that line numbers are provided for easy look) Original file: 1 2007-10-2482.90 No trade 0 0.00 100000.00 2 100000.00 3 0.00 4 HOLD 5 2007-10-2589.75 Bought 1114 1114 100000.00 0.00 ... (5 Replies)
Discussion started by: sabyasm
5 Replies

10. UNIX for Advanced & Expert Users

Deleting timestamp using sed command

Hi, I'm trying to compare Actual.html with a baseline.html However, everytime it fails b'coz of the timestamp differences between the two. So, thought of stripping off the timestamp from both the *html files before comparing using below sed command over Solaris Unix platform:... (3 Replies)
Discussion started by: elearn.latha
3 Replies
Login or Register to Ask a Question