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


 
Thread Tools Search this Thread
Operating Systems AIX Script to cat and dd last line!!! of each file
# 1  
Old 12-24-2012
Linux 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 Smilie

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!!!
# 2  
Old 12-24-2012
Using sed in a for loop:
Code:
#!/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.
This User Gave Thanks to Yoda For This Post:
# 3  
Old 12-24-2012
if you GNU sed then, go inside the directory :

Code:
sed -i '$d' *

# 4  
Old 12-24-2012
Quote:
Originally Posted by bipinajith
Using sed in a for loop:
Code:
#!/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.
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
# 5  
Old 12-24-2012
If you have file names with blank spaces then wrap variable name in double quotes:-
Code:
sed '$d' "$file" > tmp; mv tmp "$file"

# 6  
Old 12-24-2012
Quote:
Originally Posted by bipinajith
Using sed in a for loop:
Code:
#!/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.
Thanks alot i found the error Smilie
Merry Christmas!!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Cat command does not respect new line

Here's my script echo "1" >>hello.txt echo "2" >>hello.txt echo "3" >>hello.txt mailx -s "Check Status" #myteam@mycomp.com<hello.txt In Outlook I see EMail body as when I want it to be can you please suggest ? (29 Replies)
Discussion started by: mohtashims
29 Replies

2. Shell Programming and Scripting

for loop with whole line using cat

Hi all, I need to create loop script to read full line and append a variable to each line. cat file I need the output like below 10.0.0.1,136 1 24 048800 id N4 No_Light 10.0.0.1,137 1 25 048900 id N4 No_Light 10.0.0.1,140 1 28 048c00 id N4 No_Light 10.0.0.1,262 1 38 048e80... (13 Replies)
Discussion started by: ranjancom2000
13 Replies

3. Shell Programming and Scripting

'for LINE in $(cat file)' breaking at spaces, not just newlines

Hello. I'm making a (hopefully) simple shell script xml parser that outputs a file I can grep for information. I am writing it because I have yet to find a command line utility that can do this. If you know of one, please just stop now and tell me about it. Even better would be one I can input... (10 Replies)
Discussion started by: natedawg1013
10 Replies

4. Shell Programming and Scripting

.sh script / Check for file in two directories then cat the files

Hi, Here is what i'm trying to do, -Check in two directories for a user inputed filename -Then cat all the version found of the file to the current screen I am a total nembie to programming, here what i have done so far.... #!/bin/bash #Check in /home/loonatic and /var/named/master... (2 Replies)
Discussion started by: Loonatic
2 Replies

5. Shell Programming and Scripting

split a line of a file and cat a file with another

Hi, I have two files one.txt laptop boy apple two.txt unix linux OS openS I want to split one.txt into one line each and concatenate it with the two.txt output files onea.txt laptop (4 Replies)
Discussion started by: avatar_007
4 Replies

6. Shell Programming and Scripting

shell script to read a line in gps receiver log file and append that line to new file

Hi, I have gps receiver log..its giving readings .like below Trying 127.0.0.1... Connected to localhost. Escape character is '^]'. GPSD,R=1 $GPGSV,3,1,11,08,16,328,40,11,36,127,00,28,33,283,39,20,11,165,00*71... (3 Replies)
Discussion started by: gudivada213
3 Replies

7. Shell Programming and Scripting

cat in the command line doesn't match cat in the script

Hello, So I sorted my file as I was supposed to: sort -n -r -k 2 -k 1 file1 | uniq > file2 and when I wrote > cat file2 in the command line, I got what I was expecting, but in the script itself ... sort -n -r -k 2 -k 1 averages | uniq > temp cat file2 It wrote a whole... (21 Replies)
Discussion started by: shira
21 Replies

8. Shell Programming and Scripting

cat file1 read line-per-line then grep -A 15 lines down in fileb

STEP 1 # Set variable FILE=/tmp/mainfile SEARCHFILE =/tmp/searchfile # THIS IS THE MAIN FILE. cat /tmp/mainfile Interface Ethernet0/0 "outside", is up, line protocol is up Hardware is i82546GB rev03, BW 100 Mbps Full-Duplex(Full-duplex), 100 Mbps(100 Mbps) MAC address... (6 Replies)
Discussion started by: irongeekio
6 Replies

9. Shell Programming and Scripting

Cat'ing a multiple line file to one line

I am writing a script that is running a loop on one file to obtain records from another file. Using egrep, I am finding matching records in file b, then outputing feilds of both into another file. **************************** filea=this.txt fileb=that.txt cat $filea | while read line do... (1 Reply)
Discussion started by: djsal
1 Replies

10. UNIX for Dummies Questions & Answers

using 'cat' to in 'while read line'

Hi, I'm having some trouble reading a file that was 'cat' through a while loop. Can anyone suggest alternatives? what i do is cat filename|grep *.stuff while read line do echo $line ... and other commands done The cat,grep line seems to work correctly, but the script hangs when i add in... (3 Replies)
Discussion started by: chugger06
3 Replies
Login or Register to Ask a Question