1827876 Members
1200 Online
109969 Solutions
New Discussion

Delete

 
Vanquish
Occasional Advisor

Delete

 
8 REPLIES 8
Vanquish
Occasional Advisor

Re: Delete

My bad
I want to delete 1st 2 lines and 1st empty space and last empty space in a text file
in a script.

ABCDEFG
-------
AANHITW
ABEAWGI
ABEGIGI

i dont need 1st 2 lines and remove spaces before and after each line.
does
sed /^\ d works and how

Thanks
Stuart Browne
Honored Contributor

Re: Delete

So let me get this straight. You've got a file with contents like:

+-- start here --+
LINE_OF_JUNK
---------
keepthis
keepthis
keepthis

+-- end here --+

You want to get rid of everything before, and including the '-----' line, and any blank lines. Yes?
One long-haired git at your service...
Kiyoshi Miyake
Frequent Advisor

Re: Delete

Hi,

sed '1,2d;s/ //g'

or

sed -n 's/ //g;3,$p'

thx.
Francisco J. Soler
Honored Contributor

Re: Delete

Hi,

Try this:

awk '{
if (NR>2) {
for (i=1;i<=NF;i++)
printf($i)
printf("\n")
}
}' text_file

Or the pipe format:

cat text_file | awk '{
if (NR>2) {
for (i=1;i<=NF;i++)
printf($i)
printf("\n")
}
}'

Or the script format, you must create a file (for example script.awk) with this content:

---- script.awk ----
{
if (NR>2) {
for (i=1;i<=NF;i++)
printf($i)
printf("\n")
}
}
------ end script.awk ----------

Now you can do:
awk -f script.awk text_file

Hope this helps.

Frank.
Linux?. Yes, of course.
Francisco J. Soler
Honored Contributor

Re: Delete

Hi,
One appointment, with the awk solution, all spaces before and after the lines are removed but also removes all spaces between fields in the same line.

Frank.
Linux?. Yes, of course.
Hein van den Heuvel
Honored Contributor

Re: Delete


Any spaces in the text posted to the forum get massacred. So please use a (.txt) file attachement to display an example of the expected input AND the desired output.

In perl the solution might be:

perl -e '<>;<>;while(<>){s/^\s+//;s/\s+$/\n/;print unless ($_ eq ""
)}' < old-file > new-file


in english:
read and forget a line
read and forget an other line
loop while lines
replace leading spaces with nothing
replace trailing spaces with a newline
print unless it is an empty line


Be sure to reply with attached test if this is not what you wanted.

hth,
hein.
generic_1
Respected Contributor

Re: Delete

vi filename
dd
dd


:wq!

:)
Paul Cross_1
Respected Contributor

Re: Delete

Jeff, my "d" finger hurt just reading that!

:)