output only some lines of a file


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers output only some lines of a file
# 1  
Old 04-12-2012
output only some lines of a file

Hi,

I would like to know how can I output only the lines of a file (file1) corresponding to the numbers present in another file (file2).

Example file1:
Code:
aetteg
bgrhet
cgegeg
ddfggg

Example file2:
Code:
1
3
4

Desired output file:
Code:
aetteg
cgegeg
ddfggg


Last edited by Scrutinizer; 04-12-2012 at 07:27 PM.. Reason: Code tags
# 2  
Old 04-12-2012
Try awk:
Code:
awk 'NR==FNR{A[$1];next} FNR in A' file2 file1

# 3  
Old 04-12-2012
also try this:

Code:
$$ for var in `paste -s -d " " < file2`
do
head -n +$var < file1 | tail -n -1
done

# 4  
Old 04-13-2012
Thanks a lot!
# 5  
Old 05-02-2012
output only some lines of a file

Hi Srut,

Code:
 
awk 'NR==FNR{A[$1];next} FNR in A' file2 file1

Can I print value of the array in second action just to see what is being stored in array?

Code:
 
awk 'NR==FNR{A[$1];next} print A' file2 file1

It is not working. Shall i start new thread for this?


Thanks
Krsnadasa
# 6  
Old 05-02-2012
Hi, try:

Code:
awk 'NR==FNR{A[$1];next} END{for(i in A)print i, A[i]}' file2 file1

or
Code:
awk 'NR==FNR{A[$1];next}{print FNR, A[FNR]}' file2 file1

# 7  
Old 05-02-2012
output only some lines of a file

Hi Srcut,

Thanks

Code:
 
awk 'NR==FNR{A[$1];next} END{for(i in A)print i, A[i]}' file2 file1

I got out put : 2 4 1 if i replace
Code:
print i to print A[i]

then it shows only blanks. Why is it so?

Thanks
Krsna
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk to add lines with symbol to output file

In the awk below which does execute I get output that is close, except for all the lines that start with a # are removed. Some lines have one others two or three and after the script adds the ID= to the fields below the pattern in the awk, I can not seem to add the # lines back to the output. ... (5 Replies)
Discussion started by: cmccabe
5 Replies

2. UNIX for Dummies Questions & Answers

Grep -B used with -f? (Searching a file using a list of terms, output is lines before each match)

(1 Reply)
Discussion started by: Twinklefingers
1 Replies

3. Shell Programming and Scripting

Output block of lines in a file based on grep result

Hi I would appreciate your help with this. I have a output file from a command. It is broken based on initial of the users. Exmaple of iitials MN & SS. Under each section there is information pertaining to the user however each section can have different number of lines. MY challenge is to ... (5 Replies)
Discussion started by: mnassiri
5 Replies

4. UNIX for Dummies Questions & Answers

Cat command drops lines in output file

I use the cat command to concatenate text files, but one of the rows I was expecting doesn't display in the output file. Is there a verbose mode\logging mechanism for the cat command to help me investigate where the lines I was expecting are going?? cat 7760-001_1_*_06_*.txt | grep -v... (1 Reply)
Discussion started by: Xin Xin
1 Replies

5. Shell Programming and Scripting

Split: File into multiple and keeping the same 3 lines from input into all output files

The following code will split the infile into multiple files. However, I need it to insert the same first 3 lines from the original input file into each splitted file. How do I modify my script below to do so: print -n "Enter file name to split? " ; read infile if then echo "Invalid file... (4 Replies)
Discussion started by: mrn6430
4 Replies

6. Shell Programming and Scripting

Read 2 lines from File, Run Command based off output

Okay, so I have a file containing line after line of three digit numbers. I need a script that does an action based on the last two numbers in this list. So.... To get the last two numbers, I can have the script do tail -2 filename.txt But where I run into trouble is as follows. If... (6 Replies)
Discussion started by: UCCCC
6 Replies

7. UNIX for Dummies Questions & Answers

Count the lines with the same values in a column and write the output to a file

Hey everyone! I have a tab delimited data set which I want to create an output contained the calculation of number of those lines with a certain value in 2nd and 3rd column. my input file is like this: ID1 1 10M AAATTTCCGG ID2 5 4M ACGT ID3 5 8M ACCTTGGA ID4 5 ... (7 Replies)
Discussion started by: @man
7 Replies

8. Shell Programming and Scripting

awk file comparison, x lines after matching as output

Hello, I couldn't find anything on the Forum that would help me to solve this problem. Could any body help me process below data using awk? I have got two files: file1: Worker1: Thomas Position: Manager Department: Sales Salary: $5,000 Worker2: Jason Position: ... (5 Replies)
Discussion started by: killerbee
5 Replies

9. Shell Programming and Scripting

incrementing lines in the file & format output.

Hi All, I need read the file and out put format as below using ksh, I wrote below script its keep on repeating first line in the file. may i know the best way to get the below out put while incrementing line in the file. cat b.txt |awk '{print $0}' |while read line do aa=`cat $line |head -1... (7 Replies)
Discussion started by: ashanabey
7 Replies

10. Shell Programming and Scripting

Merge lines in a file with Awk - incorrect output

Hi, I would like: FastEthernet0/0 is up, line protocol is up 0 input errors, 0 CRC, 0 frame, 0 overrun, 0 ignored 0 output errors, 0 collisions, 0 interface resets Serial1/0:0 is up, line protocol is up 0 input errors, 0 CRC, 0 frame, 0 overrun, 0 ignored, 0 abort 0... (14 Replies)
Discussion started by: mv652
14 Replies
Login or Register to Ask a Question