How to delete strings in a file?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to delete strings in a file?
# 1  
Old 06-24-2014
How to delete strings in a file?

hi, i have a big file like this:
Code:
>s31 length=12 numreads=6 gene=isotig454 status=igo
ldfddfdfdfdkkkkkkfdfdkkkksdfdkkkkkkkkkksdfd
dfdfdfldfdkdffdlfddflfdjkkkkkkfdgkkgfhghfgkkk
ldfddfdfdfdkkkkkkfdfdkkkksdfdkkkkkkkkkksdfd
dfdfdfldfdkdffdlfddflfdjkkkkkkfdgkkgfhghfgkkk

>c2 length =344 numreads=4 gene=isotig343 status=igoere
ldfddfdfdfdkkkkkkfdfdkkkksdfdkkkkkkkkkksdfd
dfdfdfldfdkdffdlfddflfdjkkkkkkfdgkkgfhghfgkkk
ldfddfdfdfdkkkkkkfdfdkkkksdfdkkkkkkkkkksdfd
dfdfdfldfdkdffdlfddflfdjkkkkkkfdgkkgfhghfgkkk
ldfddfdfdfdkkkkkkfdfdkkkksdfdkkkkkkkkkksdfd
dfdfdfldfdkdffdlfddflfdjkkkkkkfdgkkgfhghfgkkk
ldfddfdfdfdkkkkkkfdfdkkkksdfdkkkkkkkkkksdfd
dfdfdfldfdkdffdlfddflfdjkkkkkkfdgkkgfhghfgkkk

>age3 length =32 
ldfddfdfdfdkkkkkkfdfdkkkksdfdkkkkkkkkkksdfd
dfdfdfldfdkdffdlfddflfdjkkkkkkfdgkkgfhghfgkkk
ldfddfdfdfdkkkkkkfdfdkkkksdfdkkkkkkkkkksdfd
dfdfdfldfdkdffdlfddflfdjkkkkkkfdgkkgfhghfgkkk
ldfddfdfdfdkkkkkkfdfdkkkksdfdkkkkkkkkkksdfd

.....

for each line with ">", i only want to keep the > and the name, like this:

Code:
>s31 
ldfddfdfdfdkkkkkkfdfdkkkksdfdkkkkkkkkkksdfd
dfdfdfldfdkdffdlfddflfdjkkkkkkfdgkkgfhghfgkkk
ldfddfdfdfdkkkkkkfdfdkkkksdfdkkkkkkkkkksdfd
dfdfdfldfdkdffdlfddflfdjkkkkkkfdgkkgfhghfgkkk

>c2 
ldfddfdfdfdkkkkkkfdfdkkkksdfdkkkkkkkkkksdfd
dfdfdfldfdkdffdlfddflfdjkkkkkkfdgkkgfhghfgkkk
ldfddfdfdfdkkkkkkfdfdkkkksdfdkkkkkkkkkksdfd
dfdfdfldfdkdffdlfddflfdjkkkkkkfdgkkgfhghfgkkk
ldfddfdfdfdkkkkkkfdfdkkkksdfdkkkkkkkkkksdfd
dfdfdfldfdkdffdlfddflfdjkkkkkkfdgkkgfhghfgkkk
ldfddfdfdfdkkkkkkfdfdkkkksdfdkkkkkkkkkksdfd
dfdfdfldfdkdffdlfddflfdjkkkkkkfdgkkgfhghfgkkk

>age3
ldfddfdfdfdkkkkkkfdfdkkkksdfdkkkkkkkkkksdfd
dfdfdfldfdkdffdlfddflfdjkkkkkkfdgkkgfhghfgkkk
ldfddfdfdfdkkkkkkfdfdkkkksdfdkkkkkkkkkksdfd
dfdfdfldfdkdffdlfddflfdjkkkkkkfdgkkgfhghfgkkk
ldfddfdfdfdkkkkkkfdfdkkkksdfdkkkkkkkkkksdfd

.....

how could i obtain this?
# 2  
Old 06-24-2014
Sure.
Try awk:
Code:
awk 'substr($1,1,1)=="^" 
           {print $1; next}
       {print $0 } '   infile  > tmpfile
# review the tempfile to be sure it is okay.
mv tmpfile infile

# 3  
Old 06-24-2014
Code:
awk '/^>/{NF=1}1' file

OR
Code:
sed 's# .*##' file


Last edited by Yoda; 06-24-2014 at 05:00 PM..
# 4  
Old 06-24-2014
Code:
sed '/^>/s/ .*//' file

Code:
awk '/^>/{$0=$1}1'

Probably this would also work:
Code:
awk '{print $1}' file

# 5  
Old 06-24-2014
I have to wonder. If it's a big file, what's the point of using CPU and IO time to remove just a little bit of data?
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Use strings from nth field from one file to match strings in entire line in another file, awk

I cannot seem to get what should be a simple awk one-liner to work correctly and cannot figure out why. I would like to use patterns from a specific field in one file as regex to search for matching strings in the entire line ($0) of another file. I would like to output the lines of File2 which... (1 Reply)
Discussion started by: jvoot
1 Replies

2. UNIX for Dummies Questions & Answers

Delete specific strings in a file

Hi, My file has a numerous sttrings.I want to retain those strings which start with stt and delete entries with >C For eg: my infile is >C4603985... (7 Replies)
Discussion started by: sa@@
7 Replies

3. Shell Programming and Scripting

Delete lines starting with these strings

Platform : RHEL 5.8 I have text file called myapplication.log . In this file, I have around 800 lines which start with the followng three strings PWRBRKER-3493 PWRBRKER-7834 SCHEDULER-ERROR How can I delete these lines in one go ? (13 Replies)
Discussion started by: omega3
13 Replies

4. Shell Programming and Scripting

Delete 2 strings from 1 line with sed?

Hi guys, I wonder if it's possible to search for a line containing 2 strings and delete that line and perhaps replace the source file with already deleted line(s). What I mean is something like this: sourcefile.txt line1: something 122344 somethin2 24334 45554676 line2: another something... (6 Replies)
Discussion started by: netrom
6 Replies

5. Shell Programming and Scripting

Delete chunk of text if contains certain strings

Using awk how to delete chunk of text if it contains certain strings? As in the following, delete a reference chunk, i.e. everything from <reference attribute = "value"> to </reference> inclusive, if within it "Group ID" value is 7 or 96 or 103 or 1005. <reference attribute = "value"> ... (3 Replies)
Discussion started by: pioavi
3 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. UNIX for Dummies Questions & Answers

Delete strings in file1 based on the list of strings in file2

Hello guys, should be a very easy questn for you: I need to delete strings in file1 based on the list of strings in file2. like file2: word1_word2_ word3_word5_ word3_word4_ word6_word7_ file1: word1_word2_otherwords..,word3_word5_others... (7 Replies)
Discussion started by: roussine
7 Replies

8. Shell Programming and Scripting

Delete Strings that are present in another file

HI, if a String is present in file1.txt, i want to delete that String from file2.txt. How can i do this?? I am sure that the file1.txt is a subset of file2.txt. (2 Replies)
Discussion started by: jathin12
2 Replies

9. Shell Programming and Scripting

recursively delete the text between 2 strings from a file

i have 200000bytes size of a unix file i need to delete some text between two strings recursively using a loop with sed or awk . these two strings are : 1st string getting from a file :::2 nd string is fi...its constant . can anyone help me sed -n'/<1 st string >/,/fi/' <input_filename> is the... (2 Replies)
Discussion started by: santosh1234
2 Replies

10. Shell Programming and Scripting

Delete strings in a file

Hi, I have a file named status.txt that looks like the file below. What I want to do is to delete the part <status> and </status> and just leave the number and print each number per line. How can I do it? If I will use sed or awk how can I do it? I tried with sed but it didn't work. Maybe I... (8 Replies)
Discussion started by: ayhanne
8 Replies
Login or Register to Ask a Question