Visit Our UNIX and Linux User Community


unix: extract a specific list of lines from a file


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers unix: extract a specific list of lines from a file
# 1  
Old 11-24-2009
unix: extract a specific list of lines from a file

I would like to extract specific lines from a file and output them into another file.

Each line in the file has a unique ID, and I have a specific list of IDs (that are not consecutive) that I wish to extract.

for example:

Code:
1 aaaaaa bbbcb cccccc
2 aaaaaa bbbbb cccccd
3 aaaaaa bbbab cccccc
4 aaaaaa bbbxb cccccc
5 aaaaaa bbbbb cwcccc
6 aaaaaa bbbbb cqcccc

I wish to extract only lines 3 and 5 of this file, how do I do this?

Last edited by Scott; 02-23-2010 at 07:45 AM..
# 2  
Old 11-24-2009
If the list of id-s is not very very very long, it could be as follows:
Code:
cat file | egrep "id1|id2|id3|...|idk"

It must take some precautions if you could find the strings of identifiers elsewhere in the line. For example, if the ID is the beginning of the line, you can do like this:
Code:
cat file | egrep "^id1|^id2|^id3|...|^idk"

# 3  
Old 02-23-2010
MySQL

Using Sed: also we can achieve this requirement

Input file:

Code:
1 aaaaaa bbbcb cccccc
2 aaaaaa bbbbb cccccd
3 aaaaaa bbbab cccccc
4 aaaaaa bbbxb cccccc
5 aaaaaa bbbbb cwcccc
6 aaaaaa bbbbb cqcccc

Sample Code:
Code:
sed -n '/^[3|5]/p' input-file >output-file

Output file:
Code:
3 aaaaaa bbbab cccccc
5 aaaaaa bbbbb cwcccc


Last edited by ungalnanban; 02-23-2010 at 07:34 AM.. Reason: spell mistake correction
# 4  
Old 02-23-2010
Using grep command also you can achieve this.

consider that the data file is having the content as :

Code:
1 aaaaaa bbbcb cccccc
2 aaaaaa bbbbb cccccd
3 aaaaaa bbbab cccccc
4 aaaaaa bbbxb cccccc
5 aaaaaa bbbbb cwcccc
6 aaaaaa bbbbb cqcccc

grep 3  data > result_file && grep 5 data >> result_file

Now the result_file will contain the following contents

Code:
3 aaaaaa bbbab cccccc
5 aaaaaa bbbbb cwcccc


Last edited by Scott; 02-23-2010 at 08:12 AM.. Reason: Code tags, please.
# 5  
Old 02-23-2010
Grep:

Hello thillai,we can achieve it easily.

egrep "3|5" file


If you want to match more number of ids,you can give the ids with | operator in the grep pattern.
# 6  
Old 02-23-2010
Quote:
Originally Posted by vivekraj
Hello thillai,we can achieve it easily.

egrep "3|5" file


If you want to match more number of ids,you can give the ids with | operator in the grep pattern.
Dear vivek, Actually here you are matching the patterns from that file only. but that is not a actual requirement here. We need to Extract the pattern from the input file and we need to store it in a file separately.
# 7  
Old 02-23-2010
Using egrep command also you can achieve this.

The following contents are available in the file name file1. After execute the following command the output will go to the file new_file.

1 aaaaaa bbbcb cccccc
2 aaaaaa bbbbb cccccd
3 aaaaaa bbbab cccccc
4 aaaaaa bbbxb cccccc
5 aaaaaa bbbbb cwcccc
6 aaaaaa bbbbb cqcccc

Code:
egrep '3|5'  file1 > new_file

The output is

3 aaaaaa bbbab cccccc
5 aaaaaa bbbbb cwcccc
 

Previous Thread | Next Thread
Test Your Knowledge in Computers #871
Difficulty: Medium
Hello World in Lisp is coded as: [ print "Hello world" ]
True or False?

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Extract specific lines based on another file

I have a folder containing text files. I need to extract specific lines from the files of this folder based on another file input.txt. How can I do this with awk/sed? file1 ARG 81.9 8 81.9 0 LEU 27.1 9 27.1 0 PHE .0 10 .0 0 ASP 59.8 11 59.8 0 ASN 27.6 12 27.6 0 ALA .0 13 .0 0... (5 Replies)
Discussion started by: alanmathew84
5 Replies

2. Shell Programming and Scripting

Extract specific line in an html file starting and ending with specific pattern to a text file

Hi This is my first post and I'm just a beginner. So please be nice to me. I have a couple of html files where a pattern beginning with "http://www.site.com" and ending with "/resource.dat" is present on every 241st line. How do I extract this to a new text file? I have tried sed -n 241,241p... (13 Replies)
Discussion started by: dejavo
13 Replies

3. Shell Programming and Scripting

ksh sed - Extract specific lines with mulitple occurance of interesting lines

Data file example I look for primary and * to isolate the interesting slot number. slot=`sed '/^primary$/,/\*/!d' filename | tail -1 | sed s'/*//' | awk '{print $1" "$2}'` Now I want to get the Touch line for only the associate slot number, in this case, because the asterisk... (2 Replies)
Discussion started by: popeye
2 Replies

4. UNIX for Dummies Questions & Answers

Quick UNIX command to display specific lines in the middle of a file from/to specific word

This could be a really dummy question. I have a log text file. What unix command to extract line from specific string to another specific string. Is it something similar to?: more +/"string" file_name Thanks (4 Replies)
Discussion started by: aku
4 Replies

5. Shell Programming and Scripting

Extract error records based on specific criteria from Unix file

Hi, I look for a awk one liner for below issue. input file ABC 1234 abc 12345 ABC 4567 678 XYZ xyz ght 678 ABC 787 yyuu ABC 789 7890 777 zxr hyip hyu mno uii 678 776 ABC ty7 888 All lines should be started with ABC as first field. If a record has another value for 1st... (7 Replies)
Discussion started by: ratheesh2011
7 Replies

6. UNIX for Dummies Questions & Answers

Extract lines with specific words with addition 2 lines before and after

Dear all, Greetings. I would like to ask for your help to extract lines with specific words in addition 2 lines before and after these lines by using awk or sed. For example, the input file is: 1 ak1 abc1.0 1 ak2 abc1.0 1 ak3 abc1.0 1 ak4 abc1.0 1 ak5 abc1.1 1 ak6 abc1.1 1 ak7... (7 Replies)
Discussion started by: Amanda Low
7 Replies

7. Shell Programming and Scripting

Extract specific lines from a file

Hi, I have a file which contains DDL statements- CREATE TABLE, CREATE INDEX, ALTER TABLE etc. I have to only pick CREATE TABLE statements from the file- Source : ---------------------------------------------- --DDL for table abc -------------------------------------------- CREATE TABLE... (4 Replies)
Discussion started by: newb
4 Replies

8. Shell Programming and Scripting

extract the lines between specific line number from a text file

Hi I want to extract certain text between two line numbers like 23234234324 and 54446655567567 How do I do this with a simple sed or awk command? Thank you. ---------- Post updated at 06:16 PM ---------- Previous update was at 05:55 PM ---------- found it: sed -n '#1,#2p'... (1 Reply)
Discussion started by: return_user
1 Replies

9. UNIX for Dummies Questions & Answers

Extract specific lines for graphing

Hello, I have a very large text file with about 2 million lines. Each of the lines starts like.. SNP_12345678 A 1212, 121, 343, ... SNP_12345678 B 4567, 567, 454, ... and so on. I want to extract specific SNPs and plot them by GNUplot or excel. The file is too large to be opened by text... (1 Reply)
Discussion started by: genehunter
1 Replies

10. UNIX for Dummies Questions & Answers

extract specific lines from file

hi, how would i extract a range of lines in a file by using the line number? ex: file contains: 1 title 2 i want 3 this part 4 to be taken out 5 from this file 6 and sent to 7 another file 8 not needed 9 end of file In this case, i want to copy line number 2 to 7 on a new... (2 Replies)
Discussion started by: apalex
2 Replies

Featured Tech Videos