extract numbered line from file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting extract numbered line from file
# 1  
Old 01-13-2010
Question help extract numbered line from file

This is a simple question but couldn't figure out. I have a list of numbers stored in file1:

Code:
2
3
5

and wanted to extract these numbered lines from file2:

Code:
a
b
c
d
e
f

with output:
Code:
b
c
e

Tried awk but couldn't figure out Smilie Thanks in advance!Smilie

Last edited by Zoho; 01-13-2010 at 01:27 PM..
# 2  
Old 01-13-2010
Try:
Code:
awk 'NR==FNR{a[$1];next}FNR in a' file1 file2

# 3  
Old 01-13-2010
Code:
nawk 'NR==FNR{a[FNR]=$1 ;next} $1 in a{print a[$1]}' file2 file1

SmilieSmilie
# 4  
Old 01-13-2010
I bow to the AWKers here. I have gotten by only using awk in it's simpliest form so I am continuously amazed at what it could do. My way would have been:

Code:
for num in `cat file1`
do
  head -$num file2 | tail -1
done

An even nicer way would be to load file2 into an array and use the values in file1 as an index to access it.
# 5  
Old 01-13-2010
awk is cool. solved a lot of problems in a quick snap. thanks all you guys!
# 6  
Old 01-13-2010
Quote:
Originally Posted by 2pugs
An even nicer way would be to load file2 into an array and use the values in file1 as an index to access it.
This is the idea in my awk solution.
SmilieSmilieSmilieSmilie
# 7  
Old 01-13-2010
In the spirit of alternative solutions, efficiency be damned:

Code:
xargs -I. sed -n .p lines < numbers

Where "numbers" is the file containing the numeric indices and "lines" is the file whose lines are indexed.

Regards,
alister
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Perl to extract information from a file line by line

In the below perl code I am using tags within each line to extract certain information. The tags that are used are: STB >0.8 is STRAND BIAS otherwise GOOD FDP is the second number GO towards the end of the line is read into an array and the value returned is outputed, in the first line that... (1 Reply)
Discussion started by: cmccabe
1 Replies

2. Shell Programming and Scripting

HELP: Shell Script to read a Log file line by line and extract Info based on KEYWORDS matching

I have a LOG file which looks like this Import started at: Mon Jul 23 02:13:01 EDT 2012 Initialization completed in 2.146 seconds. -------------------------------------------------------------------------------- -- Import summary for Import item: PolicyInformation... (8 Replies)
Discussion started by: biztank
8 Replies

3. Shell Programming and Scripting

how to extract data from numbered files using linux in the numerical order-

Hi experts, I have a list of files containing forces as the only number as follows. Force1.txt Force2.txt Force3.txt Force4.txt Force5.txt . . . . . . . . . Force100.txt I want to put all the data(only a number ) in these forces files in the file with the same order like 1,2,3 ..100 .... (2 Replies)
Discussion started by: hamnsan
2 Replies

4. Shell Programming and Scripting

rename numbered files to numbered files with leading zeroes

Hi, I have some hundreds/thousands of files named logX.dat, where X can be any integer, and they are sequential, X ranges between 1 and any number: log1.dat log2.dat log3.dat log6.dat log10.dat ... log6000.dat I would like to rename them to scatter_params_0001.dat... (6 Replies)
Discussion started by: pau
6 Replies

5. Shell Programming and Scripting

extract a line from a file by line number

Hi guys, does anyone know how to extract(grep) a line from the file, if I know the line number? Thanks a lot. (9 Replies)
Discussion started by: aoussenko
9 Replies

6. Shell Programming and Scripting

get the fifth line of a text file into a shell script and trim the line to extract a WORD

FOLKS , i have a text file that is generated automatically of an another korn shell script, i want to bring in the fifth line of the text file in to my korn shell script and look for a particular word in the line . Can you all share some thoughts on this one. thanks... Venu (3 Replies)
Discussion started by: venu
3 Replies

7. Shell Programming and Scripting

extract line by line from file

hi dudes, I have a text file in the below format 1 s sanity /u02 2 r script1 /u02 3 s sanity /u02 Please tell me a script to read this file line by line, I wrote the below script , but it is printing only 1st line not printing rest... (7 Replies)
Discussion started by: shirdi
7 Replies

8. Shell Programming and Scripting

extract a line from a file using the line number

Hello, I am having trouble extracting a specific line from a file when the line number is known. My first attempt involved grep -n 'hi' (the word 'hi will always be there) to get the line number before the line that I actually want (line 4). Extra Notes: -I am working in a bash script. -The... (7 Replies)
Discussion started by: grandtheftander
7 Replies

9. Shell Programming and Scripting

Extract a line from a file using the line number

I have a shell script and want to assign a value to a variable. The value is the line exctrated from a file using the line number. The line number it is not fix, and could change any time. I have tried sed, awk, head .. See my script # Get randome line number from the file #selectedline = `awk... (1 Reply)
Discussion started by: zambo
1 Replies
Login or Register to Ask a Question