Script to cat and dd last line!!! of each file

hi Guys,
Am new to this awesome forum, and yea i need some help here asap thnx :slight_smile:

i have a directory with over 34000 text files, i need a script that will delete the last line of each of this file without me necessary opening the files.

illustration:-
file1 200 records
file2 130 records
file3 500 records
......etc

so i will need a script to go into each file and delete just the last line(last record) of each of this text files.

thnx for your help!!!

Using sed in a for loop:

#!/bin/ksh

for file in *.txt
do
        sed '$d' $file > tmp; mv tmp $file
done

Note: I recommend to copy few text files and test the result to verify the output.

1 Like

if you GNU sed then, go inside the directory :

sed -i '$d' *

hi Bipinajith
i get this error message "0403-057 Syntax error at line 3 : `for' is not matched" when i run the script.
i actually copied the few files in a different directory and all worked fine
but when i tried to run it on the main directory then i get that error message

If you have file names with blank spaces then wrap variable name in double quotes:-

sed '$d' "$file" > tmp; mv tmp "$file"

Thanks alot i found the error :slight_smile:
Merry Christmas!!