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:
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
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, ...
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.
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:
my other file call it as b.txt, contains this list of variables with values, say b.txt:
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:
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.
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:
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.
OS version:
SunOS ahs2518 5.10 Generic_150400-27 sun4v sparc sun4v
Thanks
Moderator's Comments:
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.
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)
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)
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)
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)
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)
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)
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)
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)
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)
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)