How to get specific lines?


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers How to get specific lines?
# 1  
Old 12-10-2014
How to get specific lines?

Hi all,

I am a beginner to bash shell. I am trying to get multiple lines from file1 using the line numbers listed in file2. For example, file2 looks like:
Code:
1
3
4
8
10
...

How may I get the corresponding lines in file1 please? Any comment will be appreciated.

Many thanks,
sxiong
# 2  
Old 12-10-2014
Try this:
Code:
while read number; do
 sed -n ${number}p file1
done < file2

This User Gave Thanks to junior-helper For This Post:
# 3  
Old 12-10-2014
Quote:
Originally Posted by sxiong
Hi all,

I am a beginner to bash shell. I am trying to get multiple lines from file1 using the line numbers listed in file2. For example, file2 looks like:
Code:
1
3
4
8
10
...

How may I get the corresponding lines in file1 please? Any comment will be appreciated.

Many thanks,
sxiong
Simple method might be something like:

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

not sure if there's a cleaner or more efficient way, though Smilie
This User Gave Thanks to Ditto For This Post:
# 4  
Old 12-10-2014
Hi

Have you tried while read N;do grep -n ^$N *spec;done<list.txt yet?

hth
This User Gave Thanks to sea For This Post:
# 5  
Old 12-10-2014
Try

Code:
sed 's/^/NR==/' linesfile | awk -f - datafile

This User Gave Thanks to senhia83 For This Post:
# 6  
Old 12-10-2014
Try:
Code:
awk 'NR==FNR{A[$1]; next} FNR in A' file2 file1

These 2 Users Gave Thanks to Scrutinizer For This Post:
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

How to print lines from a files with specific start and end patterns and pick only the last lines?

Hi, I need to print lines which are matching with start pattern "SELECT" and END PATTERN ";" and only select the last "select" statement including the ";" . I have attached sample input file and the desired input should be as: INPUT FORMAT: SELECT ABCD, DEFGH, DFGHJ, JKLMN, AXCVB,... (5 Replies)
Discussion started by: nani2019
5 Replies

2. Shell Programming and Scripting

Search and replace specific positions of specific lines

Hi, I have a file with hundreds of lines. I want to search for particular lines starting with 4000, search and replace the 137-139 position characters; which will be '000', with '036'. Can all of this be done without opening a temp file and then moving that temp file to the original file name. ... (7 Replies)
Discussion started by: dsid
7 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

Printing lines with specific strings at specific columns

Hi I have a file which is tab-delimited. Now, I'd like to print the lines which have "chr6" string in both first and second columns. Could anybody help? (3 Replies)
Discussion started by: a_bahreini
3 Replies

5. 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

6. Shell Programming and Scripting

Summing over specific lines and replacing the lines with the sum using sed, awk

Hi friends, This is sed & awk type question. I have a text file which has numbers spread all over the file. I want to sum the series of numbers whenever i find it and produce an output file with the sum. For example ###start of input text file #### abc def ghi 1 2 3 4 kjld random... (3 Replies)
Discussion started by: kaaliakahn
3 Replies

7. Shell Programming and Scripting

Print Specific lines when found specific character

Hello all, I have thousand file input like this: file1: $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$ $$ | | | |$$ $$ UERT | TTYH | TAFE | FRFG |$$ $$______|______|________|______|$$ $$ | | | |$$ $$ 1 | DISK | TR1311 | 1 |$$ $$ 1 |... (4 Replies)
Discussion started by: attila
4 Replies

8. Shell Programming and Scripting

Printing all lines before a specific string and a custom message 2 lines after

Hello all, I need to print all the lines before a specific string and print a custom message 2 lines after that. So far I have managed to print everything up the string, inclusively, but I can't figure out how to print the 2 lines after that and the custom message. My code thus far is:... (4 Replies)
Discussion started by: SEinT
4 Replies

9. Shell Programming and Scripting

substitute a string on a specific position for specific lines

I woud like to substitue a string on a specific position for specific lines I've got a file and I would like to change a specific string from "TOCHANGE" to "ABCABCAB" For every line (except 1,2, 3 and the last one) , I need to check between the 9th and the 16th digits. For the 3rd line, I... (7 Replies)
Discussion started by: BSF
7 Replies

10. UNIX for Dummies Questions & Answers

how to display specific lines of a specific file

are there any basic commands that can display lines 99 - 101 of the /etc/passwd file? I'm thinking use of head and tail, but I forget what numbers to use and where to put /etc/passwd in the command. (2 Replies)
Discussion started by: raidkridley
2 Replies
Login or Register to Ask a Question