How to remove the last 3 lines from many files?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to remove the last 3 lines from many files?
# 8  
Old 06-27-2012
Quote:
Originally Posted by MaRiOsGR
radoulov thank you for the reply.
The js files are in random folders for example vhost/folder1/test.js
or vhost/folder213/my.js
etc etc
Code:
find /vhost -type f -name '*.js' -exec bash -c '
  f=$1
  tac "$f" | 
    tail -n +4 |
      tac > "$f"_tmp_ && 
        mv -- "$f"_tmp_ "$f" 
  ' - {} \;

# 9  
Old 06-27-2012
balajesuri The js files are in random folders for example vhost/folder1/test.js
or vhost/folder213/my.js
etc etc
so
/var/ww/vhost/*.js wont work
# 10  
Old 06-27-2012
in bash

take backup first as itkamaraj said



Code:
find /var/ww/vhost -name "*.js" | while read i ; do lines=$(cat ${i} | wc -l ) ; sed -i "$((${lines} - 2 )),${lines}d" $i  ; done

# 11  
Old 06-27-2012
@MaRiOsGR: I've edited my post. Please check again. (Before I could post a solution, lot many members posted and you had already asked for recursive searching of js files. I read your requirement after posting my solution)
# 12  
Old 06-27-2012
This one worked just perfect.
Is there any way to remove the lines only if the js file includes the text "km0ae9gr6m" ?

Quote:
Originally Posted by radoulov
Code:
find /vhost -type f -name '*.js' -exec bash -c '
  f=$1
  tac "$f" | 
    tail -n +4 |
      tac > "$f"_tmp_ && 
        mv -- "$f"_tmp_ "$f" 
  ' - {} \;

# 13  
Old 06-27-2012
Yes:

Code:
find /vhost -type f -name '*.js' -exec bash -c '
  f=$1
  grep -q km0ae9gr6m "$f" &&
    tac "$f" | 
      tail -n +4 |
        tac > "$f"_tmp_ && 
          mv -- "$f"_tmp_ "$f" 
  ' - {} \;

# 14  
Old 06-27-2012
This worked great.
And thank you.

but I've found a "bug" (i'm the fault),
I've found that in a 5% percent of the .js files the code I want to remove/erase
was not added in the last 3 lines but in the same line as the rest of the normal javascript code.
the exact hack code starts with
Code:
/*km0ae9gr6m*/

is it possible to remove everything after this code in all files that contain it ?

Quote:
Originally Posted by radoulov
Yes:

Code:
find /vhost -type f -name '*.js' -exec bash -c '
  f=$1
  grep -q km0ae9gr6m "$f" &&
    tac "$f" | 
      tail -n +4 |
        tac > "$f"_tmp_ && 
          mv -- "$f"_tmp_ "$f" 
  ' - {} \;

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Remove lines from output in files using awk

I have two large files (~250GB) that I am trying to remove the where GT: 0/0 or 1/1 or 2/2 for both files. I was going to use a bash with the below awk, which I think will find each line but how do I remove that line is that condition is found? Thank you :). Input 20 60055 . A ... (4 Replies)
Discussion started by: cmccabe
4 Replies

2. Windows & DOS: Issues & Discussions

Remove duplicate lines from text files.

So, I have text files, one "fail.txt" And one "color.txt" I now want to use a command line (DOS) to remove ANY line that is PRESENT IN BOTH from each text file. Afterwards there shall be no duplicate lines. (1 Reply)
Discussion started by: pasc
1 Replies

3. Shell Programming and Scripting

Two files, remove lines from second based on lines in first

I have two files, a keepout.txt and a database.csv. They're unsorted, but could be sorted. keepout: user1 buser3 anuser19 notheruser27 database: user1,2343,"information about",field,blah,34 user2,4231,"mo info",etc,stuff,43 notheruser27,4344,"hiya",thing,more thing,423... (4 Replies)
Discussion started by: esoffron
4 Replies

4. Shell Programming and Scripting

Can you extract (remove) lines from log files?

I use "MineOS" (a linux distro with python scripts and web ui included for managing a Minecraft Server). The author of the scripts is currently having a problem with the Minecraft server log file being spammed with certain entries. He's working on clearing up the spam. But in the meantime, I'm... (8 Replies)
Discussion started by: nbsparks
8 Replies

5. UNIX for Dummies Questions & Answers

Remove duplicates lines in a files

I have a file called FILE cat FILE 11/11/2012 11/11/2012 12/11/2012 15/11/2012 need to remove the duplicates dates ( ie 11/11/2012 is present two times i need remove one duplicates date ) Need outputs like this 11/11/2012 12/11/2012 15/11/2012 I have tried using awk... (8 Replies)
Discussion started by: Venkatesh1
8 Replies

6. Shell Programming and Scripting

Compare one files with strings from another + remove lines

Have two files and want to compare the content of file1 with file2. When matched remove the line. awk 'NR==FNR {b; next} !(b in $0)' file1 file2file1 1. if match 2. removefile2 1. this line has to be removed if match 2. this line has a match, remove 3. this line has no match, no removingThe... (3 Replies)
Discussion started by: sdf
3 Replies

7. Shell Programming and Scripting

Remove the files that have less than certain lines

Hi all, I'm a newbie and I'm sorry if my question is too simple. I'm having problem to delete the files that have less than certain lines, say 16. #!/bin/tcsh set filen = `sh -c 'ls *csv 2> /dev/null'` foreach fil (${filen}) if ]; then rm -f ${filen} fi end exit ... (2 Replies)
Discussion started by: GoldenFire
2 Replies

8. Shell Programming and Scripting

compare files and then remove some lines

Hi everyone I have a dilemma and I'm hoping someone has an answer for me. I have two files: # cat masterfile line3 line4 line5 line6 line7 # cat tempfile line1 line2 line3 line4 I want to compare tempfile with masterfile. (3 Replies)
Discussion started by: soliberus
3 Replies

9. Shell Programming and Scripting

compare two files and to remove the matching lines on both the files

I have two files and need to compare the two files and to remove the matching lines from both the files (4 Replies)
Discussion started by: shellscripter
4 Replies

10. Shell Programming and Scripting

How to remove certain lines in multiple txt files?

Hi , I have this type of files:- BGH.28OCT2008.00000001.433155.001 BGH.28OCT2008.00000002.1552361.001 BGH.28OCT2008.00000003.1438355.001 BGH.28OCT2008.00000004.1562602.001 Inside them contains the below: 5Discounts 6P150 - Max Total Usage RM150|-221.00 P150 EPP - Talktime RM150... (5 Replies)
Discussion started by: olloong
5 Replies
Login or Register to Ask a Question