Operating System - HP-UX
1833784 Members
3710 Online
110063 Solutions
New Discussion

Re: Getting those spaces back

 
SOLVED
Go to solution
Marty Metras
Super Advisor

Getting those spaces back

I have a script that reads the first line of a text file and removes the formform feed(blank page) writes it to a file. Then reads the rest of the file and writes lines.
The problem I have is when I write the lines to a file I do not get the leading spaces.
if a report looks like this:
============================
^L xxx 123
some header text
============================
The resolts will look like this:
============================
xxx 123
some header text
============================
How do I fix that?

Here is my script and how I use it.
Marty
--- strip_leading_ff.sh ---
#!/bin/sh
read a
a=`expr "$a" : ".\(.*\)"`
echo "$a"
while read a
do
echo "$a"
done

--- use ---
cat $the_text_file|$THE_PATH/strip_leading_ff.sh >$temp_file_name
mv $temp_file_name $the_text_file
The only thing that always remain the same are the changes.
8 REPLIES 8
John Palmer
Honored Contributor

Re: Getting those spaces back

sed can do this without a script:-

FF=$(print "\014") # FF character

sed -e "s/${FF}//" ${the_text_file} > ${temp_file_name}

Regards,
John
Mark Greene_1
Honored Contributor

Re: Getting those spaces back

replace the "echo" with print -r


mark
the future will be a lot like now, only later
Marty Metras
Super Advisor

Re: Getting those spaces back

John's idea is good except it removes all the formfeeds and I only want to remove the first one that is the first char on the first line on most files. Some do not have the forefeed at all.
Mark your idea does the same as 'echo'

Can you help some more?
Marty
The only thing that always remain the same are the changes.
curt larson_1
Honored Contributor

Re: Getting those spaces back

1) change the internal field seperator to be just the newline charcater
IFS='
'
2) use an awk script instead, which will be faster

#!/usr/bin/awk -f
BEGIN { ffcount=0;}
/^L/ { if ( ffcount == 0 ) {
sub("^L","",$0);
ffcount++;
}
print $0;
}
{ print $0;}
Marty Metras
Super Advisor

Re: Getting those spaces back

Curt,
I haven't use 'awk' for any thing. looks like your script says if ther is a ^L make it null.
ould I use this in place of my script something like this:
cat file.txt|awk_script.sh >new_file.txt

Oh! the IFS'cr' worked great for the leading space thing. The other script strips off the first letter what ever it is.
Marty
The only thing that always remain the same are the changes.
John Palmer
Honored Contributor

Re: Getting those spaces back

If you still wanted to use the shell script, you could change the first three lines to the following:

FF=$(print "\014")
read a
a=${a#${FF}}
print "${A}"

The a=... line only removes the first character of a if it's a FF.

Regards,
John
curt larson_1
Honored Contributor
Solution

Re: Getting those spaces back

could I use this in place of my script something like this:
cat file.txt|awk_script.sh >new_file.txt

yes that is how it would work.
an aside, awk scripts usually end in .awk, not .sh (they are not shell scripts).

an even easier way

ex -s +"/^L|s/^L//|wq" yourfile >/dev/null

looks like your script says if ther is a ^L make it null.

yes that is right

#/^L if the line contains a form feed
#if the counter equals zero
/^L/ { if ( ffcount == 0 ) {
# substitute nothing for the first formfeed
sub("^L","",$0);
#increment the counter so this is only done once
ffcount++;
}
#print the line without the formfeed if the first time
# or print the line
print $0;
}
curt larson_1
Honored Contributor

Re: Getting those spaces back

I only want to remove the first one that is the first char on the first line on most files. Some do not have the forefeed at all.

didn't realize this.

modify the awk to this

#!/usr/bin/awk -f

NR==1 { sub("^L","",$0);
print $0;
}
{ print $0;}