Delete lines starting with these strings


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Delete lines starting with these strings
# 8  
Old 07-26-2013
Or with awk
Code:
awk 'NR>3' myapplication.log

@Little
This:
sed '1,3d' myapplication.log > myapp;mv myapp myapplication.log
can be replaced by this:
sed -i '1,3d' myapplication.log
-i deletes it directly in the file, no need print, move etc
This User Gave Thanks to Jotne For This Post:
# 9  
Old 07-26-2013
Dear Little and Jotne,

I don't think that this was the requirement, but thanks for the code. I will use those in other things where I've previously messed about with head & tail Smilie


Many thanks for smartening my code! It shows we can all learn. Smilie



Robin
# 10  
Old 07-26-2013
Not sure about that:

I have around 800 lines which start with the followng three strings
PWRBRKER-3493
PWRBRKER-7834
SCHEDULER-ERROR
# 11  
Old 07-26-2013
Yes, I suppose it depends on interpretation. Mine was that there were 800 or so unwanted lines in the file and each of them started with the given strings, and needed to be removed to leave the rest. I can see how it can also be read that there is an 800 line file and the first three records need to be removed.

Oh well, I suppose that between us, we've answered both. Smilie

omega3 can you confirm that you have an answer one-way or another?



Thanks,
Robin

Last edited by rbatte1; 07-26-2013 at 05:01 AM.. Reason: Clarity
# 12  
Old 07-26-2013
Quote:
Originally Posted by Jotne
Or with awk
Code:
awk 'NR>3' myapplication.log

@Little
This:
sed '1,3d' myapplication.log > myapp;mv myapp myapplication.log
can be replaced by this:
sed -i '1,3d' myapplication.log
-i deletes it directly in the file, no need print, move etc
thanks i didnot knew this option.
# 13  
Old 07-26-2013
You should be careful when moving new files over old ones if the files permissions, metadata, inode number, etc. are important.
Code:
blah .... > newfile && cp newfile oldfile && rm newfile

# 14  
Old 07-26-2013
Quote:
Originally Posted by Jotne
@Little
This:
sed '1,3d' myapplication.log > myapp;mv myapp myapplication.log
can be replaced by this:
sed -i '1,3d' myapplication.log
-i deletes it directly in the file, no need print, move etc
sed -i does not do anything directly in the file. Just like using > and mv, it writes a new file which it then moves.

The most notable side-effect of using -i is that an otherwise portable script such as 1,3d is rendered unusable with most sed implementations. In such cases, -i is worse than worthless.

Regards,
Alister
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Delete all lines except a line starting with string

Shell : bash OS : RHEL 6.8 I have a file like below. $ cat pattern.txt hello txt1 txt2 txt3 some other text txt4 I want to remove all lines in this file except the ones starting with txt . How can I do this ? (4 Replies)
Discussion started by: omega3
4 Replies

2. Shell Programming and Scripting

Delete all lines starting with # character

Tool used : VIM editor that comes with RHEL 7.4 I have a file like below. It has around 300 lines like below. All the lines starting with # are comments. For readability, I removed all lines starting with # from vi (vim editor) using the command :g/^#/d . It seemed to have worked. But, which... (8 Replies)
Discussion started by: John K
8 Replies

3. Shell Programming and Scripting

How to delete lines starting with specific string?

Dear all, I would like to delete even lines starting with "N" together with their respective titles which are actually odd lines. Below is the example of input file. I would like to remove line 8 and 12 together with its title line, i.e., line 7 and 11, respectively.... (2 Replies)
Discussion started by: huiyee1
2 Replies

4. Shell Programming and Scripting

Delete multiple lines starting with a specific pattern

Hi, just tried some script, awk, sed for the last 2 hours and now need help. Let's say I have a huge file of 800,000 lines like this : It's a tedious job to look through it, I'd like to remove those useless lines in it as there's a few thousands : Or to be even more precise : if line1 =... (6 Replies)
Discussion started by: Zurd
6 Replies

5. Shell Programming and Scripting

delete lines starting with a pattern

i have a file sample.txt containing i want to delete lines starting with 123 neglecting spaces and tabs. but not lines containing 123. i.e. i want files sample.txt as help me thanxx (4 Replies)
Discussion started by: yashwantkumar
4 Replies

6. Shell Programming and Scripting

Delete lines in file containing duplicate strings, keeping longer strings

The question is not as simple as the title... I have a file, it looks like this <string name="string1">RZ-LED</string> <string name="string2">2.0</string> <string name="string2">Version 2.0</string> <string name="string3">BP</string> I would like to check for duplicate entries of... (11 Replies)
Discussion started by: raidzero
11 Replies

7. Shell Programming and Scripting

Search the word to be deleted and delete lines above this word starting from P1 to P3

Hi, I have to search a word in a text file and then I have to delete lines above from the word searched . For eg suppose the file is like this: Records P1 10,23423432 ,77:1 ,234:2 P2 10,9089004 ,77:1 ,234:2 ,87:123 ,9898:2 P3 456456 P1 :123,456456546 P2 abc:324234 (2 Replies)
Discussion started by: vsachan
2 Replies

8. UNIX for Dummies Questions & Answers

Delete lines with duplicate strings based on date

Hey all, a relative bash/script newbie trying solve a problem. I've got a text file with lots of lines that I've been able to clean up and format with awk/sed/cut, but now I'd like to remove the lines with duplicate usernames based on time stamp. Here's what the data looks like 2007-11-03... (3 Replies)
Discussion started by: mattv
3 Replies

9. Shell Programming and Scripting

Grep and delete lines except the lines with strings

Hi I am writing a script which should read a file and search for certain strings 'approved' or 'removed' and retain only those lines that contain the above strings. Ex: file name 'test' test: approved package waiting for approval package disapproved package removed package approved... (14 Replies)
Discussion started by: vj8436
14 Replies

10. Shell Programming and Scripting

Delete lines starting with XX or YY or ZZ or ....

Hi There! My final task for today is to delete lines starting with certain numbers for e.g., my text block is and i want to delete all lines starting with 11 or 17 or 21 I know i can use multiple sed commands like sed '/^11,/d' <filename> sed '/^17,/d' <filename> sed '/^21,/d'... (2 Replies)
Discussion started by: orno
2 Replies
Login or Register to Ask a Question