extracting lines based on condition and copy to another file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting extracting lines based on condition and copy to another file
# 1  
Old 09-05-2012
extracting lines based on condition and copy to another file

hi
i have an input file that contains some thing like this
Code:
aaa acc aa abc1 1232 aaa abc2....
poo awq aa abc1 aaa aaa abc2
bbb bcc bb abc1 3214 bbb abc3....
bab bbc bz abc1 3214 bbb abc3....
vvv ssa as  abc1 o09  aaa abc4....
azx aaq aa abc1 900 aqq abc19....
aaa aa aaaa abc1 899 aa abcZ....
mmm asa aa abc2 121 avv abc1....
mkm asa aa abc2 123 abb abc3....
m1m asa ua abc2 123 abb abc3....
poo awq aa abcX aaa aaa abc1....

abc[X,Y0-9] appears twice in each line. i want t0 get my output file some thing like this
file 1
Code:
aaa acc aa abc1 1232 aaa abc2....
poo awq aa abc1 aaa aaa abc2....

file2
Code:
bbb bcc bb abc1 3214 bbb abc3....
bab bbc bz abc1 3214 bbb abc3....

file#
Code:
mkm asa aa abc2 123 abb abc3....
m1m asa ua abc2 123 abb abc3....

and so on i,e., abc1 to abc[X,Y0-9] abc2 to abc[X,Y0-9] and so on till abcY to abc[X,Y0-9].
# 2  
Old 09-05-2012
Try this:
Code:
awk 's != $4 FS $7 {s=$4 FS $7; c++}{print > "file" c}' infile

# 3  
Old 09-05-2012
thankyou. but the field positions are not same always abc wont occur at 4/7 and more over with the code that was provided it is printing each line in seperate file i wanted to have all the line that have abc1 and abc2 in 1 file abc1 and abc 3 in another and so on

Last edited by anurupa777; 09-05-2012 at 06:05 AM..
# 4  
Old 09-05-2012
Quote:
Originally Posted by anurupa777
thankyou. but the field positions are not same always abc wont occur at 4/7 and more over with the code that was provided it is printing each line in seperate file i wanted to have all the line that have abc1 and abc2 in 1 file abc1 and abc 3 in another and so on
We are glad to help but if you post bad examples, you'll get wrong solutions.

Post realistic examples.
# 5  
Old 09-05-2012
hi
my file contains lines which are not uniform in terms of their positions so instead of taking position into consideration its better to consider abc1 in this example and look for abc2,abc3 etc in the same line. all those which have abc1 and abc2 in same line have to be copied to seperate file and abc1 and abc3 to seperate so on.
Code:
aaa acc abc1 1232 aaa abc2....
poo awq aa  aaa abc1 aaa abc3
bbb bcc bb abc1 3214 bbb abc3....


Code:
file1
aaa acc abc1 1232 aaa abc2....

Code:
file2
poo awq aa abc1 aaa abc3....
bbb bcc bb abc1 3214 bbb abc3...

# 6  
Old 09-05-2012
how about this?

Code:
grep "abc1.*abc2" yourinputfile > file1
grep "abc1.*abc3" yourinputfile > file2

# 7  
Old 09-05-2012
awk

Hi,

Try this out,

Code:
awk '/abc1.*abc2/{f="file2";}/abc1.*abc3/{f="file3";}f{print >f;}{f="";}' file

Cheers,
Ranga Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Copy down based on condition

Hello: I need to copy down some data from the previous record in to the next record based on the below conditions If position 41- 59 of the current record is same as the previous record and the value of position 62 is not equal to 1 then copy the previous records value for positions... (1 Reply)
Discussion started by: techedipro
1 Replies

2. Shell Programming and Scripting

Delete lines from file based on condition

I want to keep last 2 days data from a file and want to delete others data from the file. Please help me. Sample Input # cat messages-2 Apr 15 11:25:03 test1 kernel: imklog 4.6.2, log source = /proc/kmsg started. Apr 15 11:25:03 test1 rsyslogd: (re)start Apr 16 19:42:03 test1 kernel:... (2 Replies)
Discussion started by: makauser
2 Replies

3. Shell Programming and Scripting

Extracting rows and columns in a matrix based on condition

Hi I have a matrix with n rows and m columns like below example. i want to extract all the pairs with values <200. Input A B C D A 100 206 51 300 B 206 100 72 48 C 351 22 100 198 D 13 989 150 100 Output format A,A:200 A,C:51 B,B:100... (2 Replies)
Discussion started by: anurupa777
2 Replies

4. Shell Programming and Scripting

Extracting lines from text files in folder based on the numbers in another file

Hello, I have a file ff.txt that looks as follows *ABNA.txt 356 24 36 112 *AC24.txt 457 458 321 2 ABNA.txt and AC24.txt are the files in the folder named foo1. Based on the numbers in the ff.txt file, I want to extract the lines from the corresponding files in the foo1 folder and... (2 Replies)
Discussion started by: mohamad
2 Replies

5. Shell Programming and Scripting

compare 2 files and return unique lines in each file (based on condition)

hi my problem is little complicated one. i have 2 files which appear like this file 1 abbsss:aa:22:34:as akl abc 1234 mkilll:as:ss:23:qs asc abc 0987 mlopii:cd:wq:24:as asd abc 7866 file2 lkoaa:as:24:32:sa alk abc 3245 lkmo:as:34:43:qs qsa abc 0987 kloia:ds:45:56:sa acq abc 7805 i... (5 Replies)
Discussion started by: anurupa777
5 Replies

6. Shell Programming and Scripting

Extracting few lines from a file based on identifiers dynamically

i have something like this in a file called mysqldump.sql -- -- Table structure for table `Table11` -- DROP TABLE IF EXISTS `Table11`; /*!40101 SET @saved_cs_client = @@character_set_client */; /*!40101 SET character_set_client = utf8 */; CREATE TABLE `Table11` ( `id` int(11) NOT NULL... (14 Replies)
Discussion started by: vivek d r
14 Replies

7. Shell Programming and Scripting

Extracting specific lines of data from a file and related lines of data based on a grep value range?

Hi, I have one file, say file 1, that has data like below where 19900107 is the date, 19900107 12 144 129 0.7380047 19900108 12 168 129 0.3149017 19900109 12 192 129 3.2766666E-02 ... (3 Replies)
Discussion started by: Wynner
3 Replies

8. Shell Programming and Scripting

Copy input file based on condition

Hi, I am new to unix shell programming. I want to write a shell script for a functionality existing in mainframe system. I have one file as below as input 123456 &__987 &12yuq abcdef _ referes to blank condition:whenever the input file is having &__ ,it should be replaced... (4 Replies)
Discussion started by: charan0703
4 Replies

9. Shell Programming and Scripting

Extracting lines in file based on time

Hi, anyone has any ideas on how do we extract lines from a file with format similiar to this: (based on current time) Jun 18 00:16:50 .......... ............. ............ Jun 18 00:17:59 .......... ............. ............ Jun 18 01:17:20 .......... ............. ............ Jun 18... (5 Replies)
Discussion started by: faelric
5 Replies

10. UNIX for Advanced & Expert Users

Copy lines from a log file based on timestamp

how to copy lines from a log file based on timestamp. INFO (RbrProcessFlifoEventSessionEJB.java:processFlight:274) - E_20080521_110754_967: rbrAciInfoObjects listing complete! INFO (RbrPnrProcessEventSessionEJB.java:processFlight:197) - Event Seq: 1647575217; Carrier: UA; Flt#: 0106; Origin:... (1 Reply)
Discussion started by: ranjiadmin
1 Replies
Login or Register to Ask a Question