如何移除SQL脚本输出首行的换页(Form Feed)字符
Hey Mark, I’ve dealt with this exact annoying FF character issue in Oracle 10g SQL*Plus before—nothing ruins a good CSV faster than a hidden control character messing up the first line! Let’s go through three reliable fixes tailored to your setup:
Solution 1: Adjust SQL*Plus Settings (Cleanest 10g-Specific Fix)
Oracle 10g’s set newpage 0 doesn’t fully eliminate the FF character like newer versions do. Update your script’s header with these extra settings to block it entirely:
-- Add these first to suppress FF and extra noise set echo off set verify off set trimout on set trimspool on set newpage 0 -- Keep your original required settings set pagesize 1000 set scan off set feedback off set linesize 3000 set heading off
Pro tip: Make sure all these set commands run before your spool command (if you’re using one) — SQL*Plus applies settings in the order they’re executed.
Solution 2: Strip FF via Unix Shell Post-Processing
If tweaking SQL settings doesn’t do the trick, clean up the output directly in your shell script. Use sed to remove the leading ASCII 12 (FF) character:
If you’re piping SQL*Plus output directly to a file:
sqlplus your_user/your_pass@your_db @your_script.sql | sed 's/^\x0c//' > final_output.csv
If you’re spooling inside the SQL script:
# Run the SQL script first sqlplus your_user/your_pass@your_db @your_script.sql # Clean up the spooled file in-place sed -i 's/^\x0c//' your_spooled_file.csv
Note: For BSD/macOS systems, the sed in-place edit needs an empty string flag: sed -i '' 's/^\x0c//' your_spooled_file.csv.
Solution 3: Quick SQL Workaround (No Shell Changes Needed)
If you want to handle everything within your SQL script, add a blank prompt at the very top to overwrite the FF character:
-- Blank prompt to push FF to an unused line prompt -- Rest of your set commands and query logic here set pagesize 1000 ...
This works because the FF gets attached to the blank prompt line, which won’t interfere with your actual CSV data.
内容的提问来源于stack exchange,提问作者Mark

