Question on grep command (extract a string)

 
Thread Tools Search this Thread
Top Forums UNIX for Beginners Questions & Answers Question on grep command (extract a string)
# 1  
Old 11-29-2016
Question on grep command (extract a string)

Dear all,
I have a file a.txt like below:
Code:
 1_234560_A_G_b37  1 2 1 2 2 2 ...
 1_35465767_C_T_b37  2 1 1 2 2 2 ...
 2_490638010_A_T_b37  1 2 1 2 2 2 ...
 10_4567899_T_G_b37  2 2 1 2 2 2 ...

...
what I want to do is extracting rows starting with "10_" like : 10_4567899_T_G_b37 2 2 1 2 2 2 ...
but if I use code below, I will also extract rows with '10_' in the middle of the row like: 2_490638010_A_T_b37 1 2 1 2 2 2 ...
I am wondering how I can modify my code to only extract rows starting with '10_'.
my current code:
Code:
grep '10_' a.txt>b.txt

thank you!
Lin
# 2  
Old 11-29-2016
Use code tags for code please.

You can use ^ to specify 'beginning of line' in regular expressions.

Ir your data really all begins with a single space as shown:

Code:
grep "^ 10_" a.txt > b.txt

If it does not have that space in front:

Code:
grep "^10_" a.txt > b.txt

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Awk/sed command to extract the string between 2 patterns but having some particular value

Hi - i have one file with content as below. ***** BEGIN 123 ***** BASH is awesome ***** END ***** ***** BEGIN 365 ***** KSH is awesome ***** END ***** ***** BEGIN 157 ***** KSH is awesome ***** END ***** ***** BEGIN 7123 ***** C is awesome ***** END ***** I am trying to find all... (4 Replies)
Discussion started by: reldb
4 Replies

2. Shell Programming and Scripting

Command to extract word from a string

Hi All, I have a word and from that word would like to search for certain set of string, is there any command to do so ? EX : Components from the above word, would like to search for strings set and extract the search string and then do if stmt... pon nen ent Com say... (2 Replies)
Discussion started by: Optimus81
2 Replies

3. Shell Programming and Scripting

Use grep sed or awk to extract string from log file and put into CSV

I'd like to copy strings from a log file and put them into a CSV. The strings could be on different line numbers, depending on size of log. Example Log File: File = foo.bat Date = 11/11/11 User = Foo Bar Size = 1024 ... CSV should look like: "foo.bat","11/11/11","Foo Bar","1024" (7 Replies)
Discussion started by: chipperuga
7 Replies

4. Shell Programming and Scripting

Question in the command of Grep

I have the following command using Grep in a complex shell script. Can you please advise what does the below expression mean? # grep ^${File1}\| $Textfile > /deve/null 2>&1 Questions, 1) Wanted to know the significance of "^" in this line 2)What does a backslash"\" before the pipeline... (2 Replies)
Discussion started by: sreedharust
2 Replies

5. UNIX for Dummies Questions & Answers

command to extract sub-string out of file names

I have these files in a directory. It may have more class than the sample below: DEPT_CHEM101LEC_D_20110301.DAT DEPT_CHEM101LAB_D_20110301.DAT DEPT_BIO105LEC_D_20110325.DAT DEPT_BIO105LAB_D_20110325.DAT DEPT_CSC308LEC_D_20110327.DAT DEPT_CSC308LAB_D_20110327.DAT Is there way to extract out... (5 Replies)
Discussion started by: lv99
5 Replies

6. Shell Programming and Scripting

Need help please with Grep/Sed command to extract text and numbers from a file

Hello All, I need to extract lines from a file that contains ALPHANUMERIC and the length of Alphanumeric is set to 16. I have pasted the sample of the lines from the text file that I have created. My problem is that sometimes 16 appears in other part of the line. I'm only interested to... (14 Replies)
Discussion started by: mnassiri
14 Replies

7. Shell Programming and Scripting

Usage of grep command to extract only the words.

Dear Folks, I am a newbee to UNIX. I want to extract the SQLSTATE from a log file. For example the log file content is SQL0010N The string constant beginning with "' from table1 a, table 2" does not have an ending string delimiter. SQLSTATE=42603 when I give the the command as ... (4 Replies)
Discussion started by: dinesh1985
4 Replies

8. UNIX for Dummies Questions & Answers

sed/grep string replace question

Hi all, I know this question has probably been answered before, but I am struggling with this problem, even after googling a million pages. In a file named rdmt.conf I need a single character replaced, the number in the line below CUR_OC4J_ID=1 It will always appear after... (3 Replies)
Discussion started by: Mike AAA
3 Replies

9. UNIX for Dummies Questions & Answers

Using GREP to extract variable following a string

Hello, I'm running calculations and I need to extract a specific number from a output file. So far I've only been able to GREP entire lines containing the string: '1 F=' . I would like to go a step further and extract just the number following '1 F='. The entire line looks like: 1 F=... (10 Replies)
Discussion started by: modey3
10 Replies

10. Shell Programming and Scripting

command/script to extract a substring from a string

I have a long string "<ID type="Oid">{}</ID>" I need to extract "GigabitEthernet0/1" from the above string. How can it be done? :) (5 Replies)
Discussion started by: girisha
5 Replies
Login or Register to Ask a Question