remove a line within a file


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers remove a line within a file
# 1  
Old 04-10-2008
remove a line within a file

Hi all, i have a text file similar to below
Code:
A1
A2
A3
B1 ... .... *** # first occurance
B1 ... .... *** # second occurance 
B1 ... .... *** # third occurance

My desired output is
Code:
B1 ... .... *** # second occurance
B1 ... .... *** # third occurance

I want to remove the first line that is after the A's.

I have a primitive method
Code:
 /cat myfile | grep B >> desired_file

I only managed to remove the lines beginning with A. What can i next do to remove the first line from the results of grep-ping B?
# 2  
Old 04-11-2008
Bug

Hi,
Hoping that only fourth line has to be removed, try out the given code below.

sed -e '4s/B1//g' myfile |grep "B1"

Hope this solves your problem. Smilie
# 3  
Old 04-11-2008
Or 'grep B myfile | tail +1' to omit the first occurrence (which is spelled with two r:s and two e:s and no a)
# 4  
Old 11-23-2008
Yet another alternative

You can also use csplit on the result of the grep-ped file

csplit (options) input-file-name 2

will produce two output files, one containing only the first record and the second file will contain the other stuff. Essentially you're splitting the file at line 2.

Use the options to specify a prefix and/or suffix if you want.

The files resulting from the csplit can be cat-ed together to recreate the file being split. Moreover, csplit leaves your input file intact.
 
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Remove first line of file

How can I use bash to remove the first line of a file? (3 Replies)
Discussion started by: locoroco
3 Replies

2. Shell Programming and Scripting

Remove line from file and read file line by line

I have a file output.txt. File looks like this name1 10 name2 2 name3 5 I get a number n and I need to remove all lines which has number (after name) smaller or equal to n number. After that I need to write lines from file and my output must be like this: Output: 'name1 10' Output: 'name2... (1 Reply)
Discussion started by: kubo12312
1 Replies

3. Shell Programming and Scripting

How to remove last line of the file

Hi, unix gurus, I need to remove last line of the file. anybody can help me thanks in advance (7 Replies)
Discussion started by: ken002
7 Replies

4. Shell Programming and Scripting

Remove first line of file

Is there an easy way to remove the first line of a file so that the file: aaron benjamin cecilia daniel elliot fernando would become benjamin cecilia daniel elliot fernando (4 Replies)
Discussion started by: locoroco
4 Replies

5. Shell Programming and Scripting

Remove line from file

Hi!!! When I use vi editor my file looks like this: aaa2: 123.45^M aaa1: 11.34^M aaa3: aaa3: 15.56^M How to remove only line 3 ??? Thanks!!! (7 Replies)
Discussion started by: tdev457
7 Replies

6. Shell Programming and Scripting

Remove a line from a file

Hi ,guys. I have one question: I want to write a script which removes a line with certain string from a file, for example The name of the file is: "passwd", the contents of it is below: ************************* ... brownj2:x:5000: hynesp:x:5001: leeb:x:5002 dioxna:x:5003 ... ... (2 Replies)
Discussion started by: daikeyang
2 Replies

7. Shell Programming and Scripting

How to remove last line of the file

hi sir, i need help..how to remove last line of the each file for example i have files a.txt ,b.txt and so on..i wanted to delete last list of each file..the patten not same for each file..any help? thanks in advance (3 Replies)
Discussion started by: mani_um
3 Replies

8. Shell Programming and Scripting

Remove header(first line) and trailer(last line) in ANY given file

Hi, I need some help in removing the header (first line) and the trailer (last line) in a give file... The data file actually comes in EBCDIC format and I converted it into ASCII.. Now I need to strip off the first line and the last line.. I think we can use sed to do something like this:... (2 Replies)
Discussion started by: madhunk
2 Replies

9. Shell Programming and Scripting

Remove every third line from a file

I need to remove every second and every third line from a file. My idea was to do it in two operations. First every third line, then every second line. The problem is that i can't find out how to do it. I tried to look for some sed oneliners, but couldn't find any. Suggestions? (4 Replies)
Discussion started by: bistru
4 Replies
Login or Register to Ask a Question