Issue deleting blank line from a file

I'm trying to delete the blank lines from the file $Sfile. tried the below set of commands. Both are giving the same error (: bad interpreter: No such file or directory)

awk 'NF > 0' $Sfile > $Tfile
cat $Tfile
sed -i '/^$/d' $Sfile
cat $Sfile

Not sure if there's any other problem with the program. But without the above scripts, the program is working fine. But I need this logic to be put, in order to remove the blank lines, as it is causing some issues.

Please help me.

echo $Sfile | sed -i.BKUP '/^$/d' 
1 Like

How do I write the output of this to a file?

---------- Post updated at 06:27 PM ---------- Previous update was at 06:22 PM ----------

With the below script also, I'm getting the same error

echo $Sfile | sed -i.BKUP '/^$/d'
: bad interpreter: No such file or directory

---------- Post updated at 06:31 PM ---------- Previous update was at 06:27 PM ----------

Below is the complete set of code :

#!/bin/sh
#
Sfile=/u01/scripts/Sfile.log
Pfile=/u01/scripts/Pfile.log
Tfile=/u01/scripts/Tfile.log

cat /dev/null > $Sfile
grep -v '-' $Pfile > $Tfile
grep -v 'PRCSINSTANCE' $Tfile > $Sfile
echo $Sfile | sed -i.BKUP '/^$/d'
Sfile=/path/filename
sed -i.BKUP '/^$/d' $Sfile

-i option is in place replacement, sed will take backup of your original file as "filename.BKUP"

1 Like

Thanks Pravin. It is still giving the error :

: bad interpreter: No such file or directory

Please let me know if I'm missing anything in the code.

Please explain the complete logic of what you want the script to do as in the initial posting you just mentioned removing blank lines from the filename contained in a variable.

Usually that would mean your hashbang is wrong. I would think /bin/sh is correct though...

Hi in2nix4life,

I have a file which is generated by a program. The file has blank lines at the beginning and end of it. I want that to be removed through program.

That seems straightforward enough, however, you list three different files (see below). Please explain further.

Sfile=/u01/scripts/Sfile.log
Pfile=/u01/scripts/Pfile.log
Tfile=/u01/scripts/Tfile.log
1 Like

Pfile is the one that has the data.
It has few lines with '-' and 'PRCSINSTANCE', which I'm deleting here with the grep commands.
The other files, I'm using it for temporary storage.
Here I'm trying to get the final result (without any blank space or lines with '-' or 'PRCSINSTANCE') into the $Sfile

If you're using GNU Sed

sed --version | grep GNU

this should take care of all three in the file:

sed -r '/^$|-|PRCSINSTANCE/d' file
1 Like

GNU sed version 4.1.2

I'm still getting the same error :
: bad interpreter: No such file or directory

Show what you're running that produces that error.

1 Like
[abc@DGDGDGDSH03 scripts]$ /u01/scripts/PITest.sh > /u01/scripts/PITest.log
: bad interpreter: No such file or directory

PITest.sh has the script that I've pasted earlier in the thread

Looks like a carriage return at the end of the shebang (probably a result of editing in Windows).

Regards,
Alister

2 Likes

Try adding set -x to the top of your script below the #!/bin/sh line, run it again, and provide the output.

#!/bin/sh
set -x
1 Like

If exec can't find the interpreter, none of the script executes, including set -x.

It's almost certainly a carriage return at the end of the interpreter line. Not only would it explain why the interpreter can't be found (it's looking for a file whose basename is sh<carriage return>), it also explains why there's no interpreter name before the first colon in the error message (after the bad interpreter name is printed, the cr returns the cursor to the beginning of the line and the subsequent characters in the error message overwrite it).

Regards,
Alister

1 Like

Thanks Alister and all.
You were dead right. I had edited in Windows. After I edited with vi editor, the error never appeared again.
Still the below code did not take any effect :

sed -r '/^$|-|PRCSINSTANCE/d' $P_FILE

After I gave the same code to print the result to another file, it worked.

sed -r '/^$|-|PRCSINSTANCE/d' $P_FILE > $O_FILE

Thanks again to everyone who had assisted me so patiently. You all made my day!!!