Extract lines from text files


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Extract lines from text files
# 8  
Old 01-26-2014
Combining all the inputs of the posters in this thread, we would end up with something like this:
Code:
awk '$8>=-99 && $8<=-67 && $9>=100 && $9<=165 || NR==1' file

Note that the 2nd line in your output specification is out of range (>165).

----
*edit I see Akshay has already posted the same suggestion
# 9  
Old 01-26-2014
Hi scrutinizer,

Thank you. I have corrected it.
# 10  
Old 01-26-2014
Hi Edweena,
I repeat: The specification that PHI must be in the range "-67<=phi<=-99" means that there must be no output. There is no value of phi that is both greater than -67 and less than -99! The following code looks for PHI to be in the range -99<=PHI<=-67 and produces the output you requested. It reads from files in directory f1 and writes files into directory f2 with the final component of both paths being the same filename.

Code:
awk -v dest_dir=f2 '
FNR == 1 || ($8 >= -99 && $8 <= -67 && $9 >= 100 && $9 <= 165) {
        if(FNR == 1 && file != "")
                # Close previous output file.
                close(file)
        if(FNR == 1) {
                # Set output pathname for current file.
                # Start with input pathname.
                file = FILENAME
                # Strip off directories.
                sub(/.*\//, "", file)
                # Add destination directory.
                file = dest_dir "/" file
        }
        if(FNR != 1) # Delete this line to add header line to output files.
                print > file
}' f1/*

If you want to try this on a Solaris/SunOS system, use /usr/xg4/bin/awk, /usr/xpg6/bin/awk, or nawk instead of /usr/bin/awk.
# 11  
Old 01-31-2014
Hi Don Cragun,

Thank you very much for your help.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Extract the same lines from the two files

I used to use this script to extract the same lines from two files: grep -f file1 file2 > outputfile now I have file1 AB029895 AF208401 AF309648 AF526378 AJ444445 AJ720950 AJ851546 AY568629 AY591907 AY994087 BU116401 BU116599 BU119689 BU121308 BU125622 BU231446 BU236750 BU237045 (4 Replies)
Discussion started by: yuejian
4 Replies

2. Shell Programming and Scripting

extract lines from text after keyword

I have a text and I want to extract the 4 lines following a keyword! For example if I have this text and the keyword is AAA hello helloo AAA one two three four helloooo hellooo I want the output to be one two three four (7 Replies)
Discussion started by: stekanius
7 Replies

3. Shell Programming and Scripting

How to extract lines between tags into different files?

I have an xml file with the below data: unix>Cat address.xml <Address City=”Amsterdam” Street = “station straat” ZIPCODE="2516 CK " </Address> <Address City=”Amsterdam” Street = “Leeuwen straat” ZIPCODE="2517 AB " </Address> <Address City=”The Hauge” Street = “kirk straat” ... (1 Reply)
Discussion started by: LinuxLearner
1 Replies

4. UNIX for Dummies Questions & Answers

Help please, extract multiple lines from a text file

Hi all, I need to extract lines between the lines 'RD' and 'QA' from a text file (following). there are more that one of such pattern in the file and I need to extract all of them. however, the number of lines between them is varied in the file. Therefore, I can not just use 'grep -A' command.... (6 Replies)
Discussion started by: johnshembb
6 Replies

5. Shell Programming and Scripting

Extract two lines before and after the 'search text'

Hi Guys, I have a situation wherein I need to extract two lines from below the search string. Eg. Current: $ grep "$(date +'%a %b %e')" alert.log Mon Apr 12 03:58:10 2010 Mon Apr 12 12:51:48 2010 $ Here I would like the display to be something like Mon Apr 12... (6 Replies)
Discussion started by: geetap
6 Replies

6. Shell Programming and Scripting

AWK: How to extract text lines between two strings

Hi. I have a text test1.txt file like:Receipt Line1 Line2 Line3 End Receipt Line4 Line5 Line6 Canceled Receipt Line7 Line8 Line9 End (9 Replies)
Discussion started by: TQ3
9 Replies

7. Shell Programming and Scripting

extract particular lines from text file

I have two files file A which have a number in every row and file B which contains few hundred thousand rows with about 300 characters in each row (csv) What I need is to extract whole rows from B file (only these which numbers are indicated in A file) I also need to use cygwin. Any... (7 Replies)
Discussion started by: gunio
7 Replies

8. Shell Programming and Scripting

Extract lines from files

hi all, I have three files. The first file (FILE_INFO in my code) consists of four parameters for each line. 0.00765600 0.08450704 M3 E3 0.00441931 0.04878049 M4 E5 0.01904574 0.21022727 M5 E10 0.00510400 0.05633803 M6 E12 0.00905960 ... (11 Replies)
Discussion started by: my_Perl
11 Replies

9. Shell Programming and Scripting

Extract lines of text based on a specific keyword

I regularly extract lines of text from files based on the presence of a particular keyword; I place the extracted lines into another text file. This takes about 2 hours to complete using the "sort" command then Kate's find & highlight facility. I've been reading the forum & googling and can find... (4 Replies)
Discussion started by: DionDeVille
4 Replies

10. 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
Login or Register to Ask a Question