How to select files based on a criteria?


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers How to select files based on a criteria?
# 1  
Old 06-07-2012
How to select files based on a criteria?

I have a file.....
Code:
 
xxx 2345 455
abc 345 555
cdf 456 777
fff 555 888

Now my requirement is, Say if, i want to select only those records prior to the record fff 555 888...

how do i go about doing this in unix....

The fff would be hardcoded as it wud be fixed and everytime when i run this script it shud only pick all the prior records i e
Code:
xxx 2345 455
abc 345 555
cdf 456 777

Moderator's Comments:
Mod Comment Please use next time
code tags for your code and data

Last edited by vbe; 06-07-2012 at 09:18 AM..
# 2  
Old 06-07-2012
Code:
perl -e ' while(<>){last if (!/^fff/); print }' filename

# 3  
Old 06-07-2012
Shorter.
Code:
perl -pe '/^fff/ && last' file

# 4  
Old 06-07-2012
Code:
perl -pe '/^fff/ && last' file

..........Worked........Simple Awesome...


I would love to know how this actually worked...some explaination on the code so that i can explain this to my team. Smilie

Moderator's Comments:
Mod Comment Please use next time
code tags for your code and data

Last edited by vbe; 06-07-2012 at 12:49 PM.. Reason: code tags...
# 5  
Old 06-07-2012
Code:
perl     # The language compiler
-e       # Allows you to write code on command line to be executed by compiler
-p       # Iterate over each line of file and print by default
/^fff/   # Pattern matching - match string that starts with "fff"
&&       # Logical AND operator
last     # Stop iterating and go to end of loop (in this case, eof)
file     # Input file name

To summarise: Iterate over each line of file and print it. If line starts with "fff", stop iterating.
This User Gave Thanks to balajesuri For This Post:
# 6  
Old 06-20-2012
Continuing on the same Question...

Now how we select records...Earlier, we selected prior records..but now, say
Code:
xxx 2345 455
abc 345 555
cdf 456 777
fff 555 888

I want to select whats below xxx and above fff......
my result shud be
Code:
abc 345 555
cdf 456 777

Moderator's Comments:
Mod Comment Please use next time code tags for your code and data

Last edited by vbe; 06-20-2012 at 09:15 AM..
# 7  
Old 06-20-2012
Code:
perl -ne '/^xxx/ && next; /^fff/ && last; print' file

 
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Select and copy .csv files based on row and column number

Dear UNIX experts, I'm a command line novice working on a Macintosh computer (Bash shell) and have neither found advice that is pertinent to my problem on the internet nor in this forum. I have hundreds of .csv files in a directory. Now I would like to copy the subset of files that contains... (8 Replies)
Discussion started by: rcsapo
8 Replies

2. Linux

Merge two files based on matching criteria

Hi, I am trying to merge two csv files based on matching criteria: File description is as below : Key_File : 000|ÇÞ|Key_HF|ÇÞ|Key_FName 001|ÇÞ|Key_11|ÇÞ|Sort_Key22|ÇÞ|Key_31 002|ÇÞ|Key_12|ÇÞ|Sort_Key23|ÇÞ|Key_32 003|ÇÞ|Key_13|ÇÞ|Sort_Key24|ÇÞ|Key_33 050|ÇÞ|Key_15|ÇÞ|Sort_Key25|ÇÞ|Key_34... (3 Replies)
Discussion started by: PK29
3 Replies

3. Shell Programming and Scripting

Comparing Select Columns from two CSV files in UNIX and create a third file based on comparision

Hi , I want to compare first 3 columns of File A and File B and create a new file File C which will have all rows from File B and will include rows that are present in File A and not in File B based on First 3 column comparison. Thanks in advance for your help. File A A,B,C,45,46... (2 Replies)
Discussion started by: ady_koolz
2 Replies

4. UNIX for Dummies Questions & Answers

Select all files in a folder based on creation date (ls command)

Hi All, <Re-posting in Correct group> I'm trying to select all the files in a folder that starts with a particular name format and are created in a gven date range using 'ls' command...but i'm not successful.... Example : I'm trying to see all the text files in a folder who names start... (6 Replies)
Discussion started by: Satya C1
6 Replies

5. Shell Programming and Scripting

Select lines from a file based on a criteria

Hi I need to select lines from a txt file, I have got a line starting with ZMIO:MSISDN= and after a few line I have another line starting with 'MOBILE STATION ISDN NUMBER' and another one starting with 'VLR-ADDRESS' I need to copy these three lines as three different columns in a separate... (3 Replies)
Discussion started by: Tlcm sam
3 Replies

6. UNIX for Dummies Questions & Answers

How to fetch files right below based on some matching criteria?

I have a requirement where in i need to select records right below the search criteria qwertykeyboard white 10 20 30 30 40 50 60 70 80 qwertykeyboard black 40 50 60 70 90 100 qwertykeyboard and white are headers separated by a tab. when i execute my script..i would be searching... (4 Replies)
Discussion started by: vinnu10
4 Replies

7. Shell Programming and Scripting

Archive files to different target folders based on criteria

Hi All, I am creting archive script in which i need to split the source file's to different target folder's based on the input file name first character. Input1.txt -- will contains file names that are needs to be Archive. Input1.txt A1213355 B2255666 C2254555 A6655444 C5566445 ... (2 Replies)
Discussion started by: kmsekhar
2 Replies

8. Shell Programming and Scripting

Need script to select multiple files from archive directory based on the date range

hi all, here is the description to my problem. input parameters: $date1 & $date2 based on the range i need to select the archived files from the archived directory and moved them in to working directory. can u please help me in writing the code to select the multiple files based on the... (3 Replies)
Discussion started by: bbc17484
3 Replies

9. UNIX for Dummies Questions & Answers

Select records based on search criteria on first column

Hi All, I need to select only those records having a non zero record in the first column of a comma delimited file. Suppose my input file is having data like: "0","01/08/2005 07:11:15",1,1,"Created",,"01/08/2005" "0","01/08/2005 07:12:40",1,1,"Created",,"01/08/2005"... (2 Replies)
Discussion started by: shashi_kiran_v
2 Replies
Login or Register to Ask a Question