grep grab 19 letters from now or a full line


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting grep grab 19 letters from now or a full line
# 1  
Old 07-31-2012
grep grab 19 letters from now or a full line

Hi,

I have a file like this

Code:
>hg19_chr1_123_456_+
asndbansbdahsjdbfsjhfghjdsghjdghjdjhdghjjdkhfsdkjfhdsjkdkjghkjdhgfjkhjfkf
hasjgdhjsgfhjdsgfdsgfjhdgjhdjhdhjdfhjdfjgfdfbdghjbfjksdhfjsfdghjgdhjgfdjhgd
jhgdfj
>hg19_chr1_123_456_-
akjldshfuiewyruiewehbjhvbdcnmbfhdsjfjdbfhdbhjdbghjfdbghjbdfghjdfbjhbkk
jsdhfjdgjfdgjfdgjkfdhjkfhkjfhjkfjkhkjfskjdfhkjhgjkdgkjfdhgjkfhjkfhgkfhkfkjhgf
dshjghjdg
>hg19_chr2_234_456_+
skjfhdsjkfghdjkghdfjkhgjkfdghjkdfuiertytoierytuireyteiruytueriyteruytierutye
sjhdjashdjahjkdasjkhdajkshdkajshdkashdasruweyriweyrueiwryewrewurewuu
jdhfjkdshf

I want to grep the start of line to be '>' and grab the next 19 letters or the whole line

So, my output will be

Code:
>hg19_chr1_123_456_+
>hg19_chr1_123_456_-
>hg19_chr2_234_456_+

# 2  
Old 07-31-2012
Like this:
Code:
sed -n '/^>/s:\(.\{20\}\).*:\1:p' infile

This User Gave Thanks to elixir_sinari For This Post:
# 3  
Old 07-31-2012
Code:
awk '/^>/{print substr($0,1,20)}' input-file

This User Gave Thanks to neutronscott For This Post:
# 4  
Old 07-31-2012
@neutron scott. It worked for me. I have more numbers than 3 in the pattern. So, I used 25 instead of 20.

@elixir_sinari - Ur command works too. But, when I increase the 20 to 25, it omits some records. I donno why.

Thanks to both of u.
# 5  
Old 07-31-2012
yah the sed match there says exactly 20, rather than up to 20. I believe that'd need to be \{0,25\}
# 6  
Old 07-31-2012
Quote:
Originally Posted by jacobs.smith
Hi,

I have a file like this

Code:
>hg19_chr1_123_456_+
asndbansbdahsjdbfsjhfghjdsghjdghjdjhdghjjdkhfsdkjfhdsjkdkjghkjdhgfjkhjfkf
hasjgdhjsgfhjdsgfdsgfjhdgjhdjhdhjdfhjdfjgfdfbdghjbfjksdhfjsfdghjgdhjgfdjhgd
jhgdfj
>hg19_chr1_123_456_-
akjldshfuiewyruiewehbjhvbdcnmbfhdsjfjdbfhdbhjdbghjfdbghjbdfghjdfbjhbkk
jsdhfjdgjfdgjfdgjkfdhjkfhkjfhjkfjkhkjfskjdfhkjhgjkdgkjfdhgjkfhjkfhgkfhkfkjhgf
dshjghjdg
>hg19_chr2_234_456_+
skjfhdsjkfghdjkghdfjkhgjkfdghjkdfuiertytoierytuireyteiruytueriyteruytierutye
sjhdjashdjahjkdasjkhdajkshdkajshdkashdasruweyriweyrueiwryewrewurewuu
jdhfjkdshf

I want to grep the start of line to be '>' and grab the next 19 letters or the whole line

So, my output will be

Code:
>hg19_chr1_123_456_+
>hg19_chr1_123_456_-
>hg19_chr2_234_456_+


You said you want the next 19 letters or the whole line.

You have been given two ways to get the '>' and the next 19 letters, but now you seem to want the next 24 characters.

If you want the entirety of lines starting with '>', just use:
Code:
grep '^>' file

otherwise, you need to explain how we are supposed to determine when you want 19 characters, when you want 24 characters, and when you want the whole line.
This User Gave Thanks to Don Cragun For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Grab text after pattern on the same line

data: 8iW5iPACIb5fafafEU24f3EOOjpakx6VwxBX+NafafxJMWX8iW5iPACIb5fafafEU24f3EOOjpakx6VwxBX+NafafxJMWX8iW5i PACIb5fafafEU24f3EOOjpakx6VwxBX+NafafxJMWX8iW5iPACIb5fafafEU24f3EOOjpakx6VwxBX+ 8nwR15UzfeZafaf2bGr8akx6VwxBX+NafafxJMWX8iW5iPACIb5fafafEU24f3EOOjp lVpOoMLXJ ... (19 Replies)
Discussion started by: SkySmart
19 Replies

2. UNIX for Beginners Questions & Answers

How do I use grep to grab prime number output from my factor program?

I have a factor program that runs and outputs to stdout all the prime numbers that are specified in the given paramters, in this case 30000000-31000000. Command: factor/factor 30000000-31000000 Sample output: 30999979 = 30999979 30999980 = 2^2 5 11 140909 30999981 = 3 10333327... (6 Replies)
Discussion started by: steezuschrist96
6 Replies

3. UNIX for Dummies Questions & Answers

Grep search for count of letters beginning with a certain letter

Say I have a text file (allWords.txt), that contains all the words in the dictionary, line by line, that I would like to search through. Here is a snippet of what it might looks like... Code: a aah aahed aahing aahs aardvark aardvarks aardwolf ab abaci aback abacus abacuses abaft ...... I... (1 Reply)
Discussion started by: blackvelvet
1 Replies

4. UNIX for Dummies Questions & Answers

Grep /Awk letters X - X in every line and print it as a mac address

hey i m kinda new to this so i will appreciate any help , i have this list of values: pwwn = 0x50012482009cd7a7 nwwn=0x50012482009cd7a6 port_id = 0x280200 pwwn = 0x5001248201bcd7a7 nwwn=0x5001248201bcd7a6 port_id = 0x280300 pwwn = 0x50012482009c51ad nwwn=0x50012482009c51ac port_id =... (4 Replies)
Discussion started by: boaz733
4 Replies

5. Shell Programming and Scripting

Grab characters following grep

Hi all, I'm trying to gather data directly following a keyword in a file but I have no guarantee where it will appear in said file so I can't use cut or anything else that assumes it will be located at a certain position. If anyone has any suggestions I'd be grateful. Here is an example of the... (7 Replies)
Discussion started by: Korn0474
7 Replies

6. UNIX for Dummies Questions & Answers

Grab Portion of Output Text (sed, grep, awk?)

Alright, here's the deal. I'm running the following ruby script (output follows): >> /Users/name/bin/acweather.rb -z 54321 -o /Users/name/bin -c Clouds AND Sun 57/33 - Mostly sunny and cool I want to just grab the "57/33" portion, but that's it. I don't want any other portion of the line. I... (5 Replies)
Discussion started by: compulsiveguile
5 Replies

7. Shell Programming and Scripting

Grab first or second line after a search string

In a shell script, I need to grab the first or second line after a search string in a file. For example: File.out: Random Info Manufacturer: XYZPDQ System Info Manufacturer: Hewlett-Packard Product Name: ProLiant I search for the word FILE, I want to be able to grab the line... (1 Reply)
Discussion started by: jwk1230
1 Replies

8. Shell Programming and Scripting

How to grab the value of field before the line reached

Hi. I have a qury whihc I hope someone could clarify. I have a file:- Num Measure 108 0.05 12 0.45 13 0.2 19 0.5 I wanted to grep the value of 19 from Column Num which will then take the minimum value of measure (not including 19's measure value).... (3 Replies)
Discussion started by: ahjiefreak
3 Replies

9. Shell Programming and Scripting

grab the line using awk

suppose u have a file AAAAAKKSKSKMSKMSKBNNSHNKSNJNMSYNMSBH This is exactly wht the input is like Question is i want to list wht is on the line 5 tht will be A but Remember u want to extract in between say from 100 to 300 i tried using awk 'BEGIN {FS=""} {print$100,$300}' file but it will... (1 Reply)
Discussion started by: cdfd123
1 Replies

10. Shell Programming and Scripting

grab next consecutive line or lines

Hello i'm writting a krn shell that will find the letter F and I would like to grab everything from F till 0:00. Is there a sed or awk command that i could use. Thank you. File is looks like this. 11/12 xxx xxxx xxx F xxxx xxxx some info entered here some info entered here xxxx... (1 Reply)
Discussion started by: wisher115
1 Replies
Login or Register to Ask a Question