get out the lines by line index


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting get out the lines by line index
# 1  
Old 10-12-2011
get out the lines by line index

Hi,

I have two files and want to get out the lines from file2.txt by the line number in file1.txt. The first one includes the line index numbers

file1.txt

Code:
2
6
10
...

And the second file has lots of lines like:

file2.txt

Code:
A B C
F G H
R T P
K L T
T F F
C C G
Y T Y
F K K
L O T
G H Y
Z X Z
....

output.txt should be like

Code:
F G H
C C G
G H Y
...

Thanks for your time,

Senay

Last edited by radoulov; 10-12-2011 at 04:18 PM.. Reason: Code tags!
# 2  
Old 10-12-2011
Can you show what you have done so far

Here is a hint...

Code:
cat file2.txt | head -3 | tail -1

would give you the third line
# 3  
Old 10-12-2011
Code:
awk 'BEGIN { while(getline < "file1.txt") A[$1]=1 } A[NR]' < file2.txt

First, load the array A with A[2]=1, A[6]=1, and so forth. Then for each line, check if A[NR] (nr being the line number) is a nonzero value. If it's nonzero (i.e. was in file1.txt), print.
# 4  
Old 10-12-2011
I tried

Code:
awk 'BEGIN { while(getline < "file1.txt") A[$1]=1 } A[NR]'  file2.txt > output.txt

However, it results as one line. I want to see each line in the output.txt as the example i gave.

output.txt

Code:
F G H
C C G
G H Y
...

Thanks,

Last edited by radoulov; 10-12-2011 at 04:19 PM.. Reason: Code tags!
# 5  
Old 10-12-2011
You didn't edit your text files in Windows, did you? That will fill them with useless carriage returns which mangle their output.
# 6  
Old 10-12-2011
Yes, i am using windows to edit the text files.
# 7  
Old 10-12-2011
tr -d '\r' < junk.txt > fixed.txt to get rid of the windows junk, then your datafiles should work. You cannot use the same file as input and output. Replace the input file afterwards if you must.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Get an output of lines in pattern 1st line then 10th line then 11th line then 20th line and so on.

Input file: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 (6 Replies)
Discussion started by: Sagar Singh
6 Replies

2. Shell Programming and Scripting

awk to remove lines that do not start with digit and combine line or lines

I have been searching and trying to come up with an awk that will perform the following on a converted text file (original is a pdf). 1. Since the first two lines are (begin with) text they are removed 2. if $1 is a number then all text is merged (combined) into one line until the next... (3 Replies)
Discussion started by: cmccabe
3 Replies

3. Shell Programming and Scripting

How to get index values for multiple matches in the same line with awk?

Hi, I know that echo "bob alice robert alice" | awk '{print index($0,"alice")}' 5Will output the index of the first alice match, is there any way to get the index of all matches?, eg: echo "bob alice robert alice" | awk 'unknown magic' 5:18Thanks for your time. (6 Replies)
Discussion started by: chilicuil
6 Replies

4. Shell Programming and Scripting

Need a program that read a file line by line and prints out lines 1, 2 & 3 after an empty line...

Hello, I need a program that read a file line by line and prints out lines 1, 2 & 3 after an empty line... An example of entries in the file would be: SRVXPAPI001 ERRO JUN24 07:28:34 1775 REASON= 0000, PROCID= #E506 #1065: TPCIPPR, INDEX= 003F ... (8 Replies)
Discussion started by: Ferocci
8 Replies

5. UNIX for Dummies Questions & Answers

Reading specific lines from a file using index or keywords

Hello I want to read from a file which contains email addresses. The file format is like this. from@mail.com to1@mail.com to2@mail.com cc@mail.com bcc@mail.com I'll have to read from such file and assign the email addresses to respective variables. frommail =... (11 Replies)
Discussion started by: Kyaw Lwin Phyo
11 Replies

6. Shell Programming and Scripting

extract the lines by index number

Hi All, I want to extract the lines from file1 by using the index numbers from file2. In example, cat file1.txt 265 ABC 956 ... 698 DFA 456 ... 456 DDD 145 ... 125 DSG 154 ... 459 CGB 156 ... 490 ASF 456 ... 484 XFH 489 ... 679 hgt 481 ... 111 dfg 986 ... 356 vhn 444 ...... (7 Replies)
Discussion started by: senayasma
7 Replies

7. Shell Programming and Scripting

Break lines up into single lines after each space in every line

It sounds a bit confusing but what I have is a text file like the example below (without the Line1, Line2, Line3 etc. of course) and I want to move every group of characters into a new line after each space. Example of text file; line1 .digg-widget-theme2 ul { background: rgb(0, 0, 0) none... (7 Replies)
Discussion started by: lewk
7 Replies

8. Shell Programming and Scripting

Sort from start index and end index in line

Hi All, I have a file (FileNames.txt) which contains the following data in it. $ cat FileNames.txt MYFILE17XXX208Sep191307.csv MYFILE19XXX208Sep192124.csv MYFILE20XXX208Sep192418.csv MYFILE22XXX208Sep193234.csv MYFILE21XXX208Sep193018.csv MYFILE24XXX208Sep194053.csv... (5 Replies)
Discussion started by: krish_indus
5 Replies

9. UNIX for Dummies Questions & Answers

wh inode index starts from 1 unlike array index (0)

brothers why inode index starts from 1 unlike array inex which starts from 0 its a question from the design of unix operating system of maurice j.bach i need to know the answer urgently...someone help please (1 Reply)
Discussion started by: sairamdevotee
1 Replies

10. Filesystems, Disks and Memory

why the inode index of file system starts from 1 unlike array index(0)

why do inode indices starts from 1 unlike array indexes which starts from 0 its a question from "the design of unix operating system" of maurice j bach id be glad if i get to know the answer quickly :) (0 Replies)
Discussion started by: sairamdevotee
0 Replies
Login or Register to Ask a Question