How to find that has a string and pull the record out of that file


 
Thread Tools Search this Thread
Special Forums UNIX Desktop Questions & Answers How to find that has a string and pull the record out of that file
# 1  
Old 11-04-2010
How to find that has a string and pull the record out of that file

Hello Experts !!!

Have some trouble finding a solution for the problem mentioned below.

Please help.
Thanks,
Lee.

I have set of input files as below

File1
Code:
MCMCNDD77  20100903:12:36:50        323-2322        BAC,MRP,NWER    CKDJKJK838838               234                              A
HHFDKKF  20100903:12:36:50        323-2322        BAC,MRP,NWER    CKDJKJK838838               234                              A
FKJDJDKJ  20100903:12:36:50        323-2322        BAC,MRP,NWER    CKDJKJK838838               234                              A

File2
Code:
MCMCNDD77  20100903:12:36:50        234        AC,MRP,NWER    CKDJKJK838838               GDDK-DKCJJ                              A
MCMCNDD77  20100903:12:36:50        323-2322        AC,MRP,NWER    CKDJKJK838838               234                              A
MCMCNDD77  20100903:12:36:50        234        CMC,MRP,NWER    CKDJKJK838838               GDDK-DKCJJ                              A

File3
Code:
MCMCNDD77  20100903:12:36:50        234        BAC,MRP,NWER    CKDJKJK838838               FHHF-DCCJJ                              A
MCMCNDD77  20100903:12:36:50        234        BAC,MRP,NWER    CKDJKJK838838               GDDK-DKCJJ                              A
MCMCNDD77  20100903:12:36:50        323-2322        BAC,MRP,NWER    CKDJKJK838838               GDDK-DKCJJ                              A

I wish to search for all files in the directory that have the number "234" in the sixth place as below
Code:
$cat file1 | awk '{print $6}'

and then YANK that record out and place in lets say an Outputfile.

So, in the above scenario with the files, file1, file2, file3, I want to see the four records out in Outputfile as below.
Code:
MCMCNDD77  20100903:12:36:50        323-2322        BAC,MRP,NWER    CKDJKJK838838               234                              A
HHFDKKF  20100903:12:36:50        323-2322        BAC,MRP,NWER    CKDJKJK838838               234                              A
FKJDJDKJ  20100903:12:36:50        323-2322        BAC,MRP,NWER    CKDJKJK838838               234                              A
MCMCNDD77  20100903:12:36:50        323-2322        AC,MRP,NWER    CKDJKJK838838               234                              A


Last edited by Scott; 11-04-2010 at 07:16 PM.. Reason: Please use code tags
# 2  
Old 11-04-2010
Code:
awk '$6 == 234' File[123] > OutputFile

# 3  
Old 11-04-2010
Scottn,

I was thinking this has to do with awk & there you were !!! Im so glad you helped me.

Just want to tag another request to this one...if I want to replace that 234 with MCCDK in the sixth column inside that file, how do I do this ?

Thanks,
Lee.
# 4  
Old 11-04-2010
Hi.
Code:
awk '$6 == 234 { sub($6, "MCCDK"); print }' File[123] > OutputFile

(you could also use { $6 = "MCCDK"; print } but you'd lose formatting)
This User Gave Thanks to Scott For This Post:
# 5  
Old 11-04-2010
Scottn,

Im so so thankful for your help. Im glad I joined here.

Lee.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Modifying text file records, find data in one place in the record and print it elsewhere

Hello, I have some text data that is in the form of multi-line records. Each record ends with the string $$$$ and the next record starts on the next line. RDKit 2D 15 14 0 0 0 0 0 0 0 0999 V2000 5.4596 2.1267 0.0000 C 0 0 0 0 0 0 0 0 0 0 0 ... (5 Replies)
Discussion started by: LMHmedchem
5 Replies

2. Shell Programming and Scripting

Replace a string for every record after the 1st record

I have data coming in the below format for each record <?xml version="1.0" encoding="UTF-8" standalone="no"?><test_sox xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"><testdetials>....</test_sox> <?xml version="1.0" encoding="UTF-8" standalone="no"?><test_sox... (8 Replies)
Discussion started by: dsravanam
8 Replies

3. Shell Programming and Scripting

Find string in file and find the all records by string

Hello I would like to get know how to do this: I got a big file (about 1GB) and I need to find a string (for instance by grep ) and then find all records in this file based on a string. Thanks for advice. Martin (12 Replies)
Discussion started by: mape
12 Replies

4. Shell Programming and Scripting

Extract timestamp from first record in xml file and it checks if not it will replace first record

I have test.xml <emp><id>101</id><name>AAA</name><date>06/06/14 1811</date></emp> <Join><id>101</id><city>london</city><date>06/06/14 2011</date></join> <Join><id>101</id><city>new york</city><date>06/06/14 1811</date></join> <Join><id>101</id><city>sydney</city><date>06/06/14... (2 Replies)
Discussion started by: vsraju
2 Replies

5. Shell Programming and Scripting

Find string in file and append new string after

Hi All, I'm trying to insert a string into a file at a specific location. I'd like to add a string after the parent::__construct(); in my file. <?php if (! defined('BASEPATH')) exit('No direct script access allowed'); class MY_Controller extends CI_Controller { function... (6 Replies)
Discussion started by: jjkilpatrick
6 Replies

6. UNIX for Advanced & Expert Users

how to find lenght of fixed width file record?

actually i am trying to find the lenght of fixed width file record reading from teradata db but its not working can u guys help me out? code which i wrote--- colmn_lngth=`cat $RPT_FILE | awk -F~ '{print $1}'` rm $RPT_FILE while read line do result=`echo $line | wc -m` ... (4 Replies)
Discussion started by: Seshendranath
4 Replies

7. Shell Programming and Scripting

find string and replace with string in other file

Dear all, I need your help, I have file like this: file1:23456 01910964830098775635 34567 01942809546554654323 67589 26546854368698023653 09778 58716868568576876878 08675 86178546154065406546 08573 54165843543054354305 . .file2: 23456 25 34567 26 67589 27 (2 Replies)
Discussion started by: attila
2 Replies

8. Shell Programming and Scripting

Find multiple string in one file using find command

Hi, I want find multiple string in one file using find coomand. And keeping it in one variable.grep is not working. (5 Replies)
Discussion started by: vivek1489
5 Replies

9. Linux

Find String in FileName and move the String to new File if not found

Hi all, I have a question.. Here is my requirement..I have 500 files in a path say /a/b/c I have some numbers in a file which are comma seperated...and I wanted to check if the numbers are present in the FileName in the path /a/b/c..if the number is there in the file that is fine..but if... (1 Reply)
Discussion started by: us_pokiri
1 Replies

10. Programming

Regex to pull out all words in apostrophes in a string

Hi, I have string like this: CHECK (VALUE::text = ANY (ARRAY)) and I am trying to get out the words in apostrophes ('). In this case"ACTIVE INACTIVE DELETE" Also the array may consist of one or more words (in given example 3). Also instead of word it can be only one LETTER. And... (4 Replies)
Discussion started by: neptun79
4 Replies
Login or Register to Ask a Question