[Solved] Pattern match and write to separate files


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting [Solved] Pattern match and write to separate files
# 1  
Old 08-28-2013
[Solved] Pattern match and write to separate files

I need to parse a file and depending on a patern match(in the insert job line) separate files have to be created with a line added (content in file2).

Mapping for pattern match and add line :
Code:
for Alpha 123
for Beta 234
for Gamma 345
no match (goes into another file)

Code:
File 1 
insert_job: Alpha_jaa    job_type: cmd
description: ABC
machine: XYZ

insert_job: Beta_baa    job_type: cmd
description: ABC
machine: XYZ
 
insert_job: Gamma_caa   job_type: cmd
description: ABC
machine: XYZ
 
insert_job: else_caa job_type: cmd
description: ABC
machine: XYZ

Code:
File 2
condition: 123
condition: 234
condition: 345

Output required(3 separate files created)
File Alpha.txt
insert_job: Alpha_jaa    job_type: cmd
description: ABC
machine: XYZ
condition: 123
File Beta.txt
insert_job: Beta_baa    job_type: cmd
description: ABC
machine: XYZ
condition: 234
 
File Gamma.txt
insert_job: Gamma_caa   job_type: cmd
description: ABC
machine: XYZ
condition: 345
 
File nomatch :
insert_job: else_caa job_type: cmd
description: ABC
machine: XYZ
condition: None

Code:
cat $FILENAME | while read LINE
do

case "$LINE" in
   "alpha") 
   echo $LINE >>alpha.txt
   echo  "condition: 123" >>alpha.txt

   ;;
   "beta") 
echo $LINE >>beta.txt
"echo condition: 234">>beta.txt

 
   ;;
   echo $LINE >>gamma.txt
   "gamma") echo "condition: 345">>gamma.txt

   ;;
   *) 
echo $LINE >>nomatch.txt
echo "condition: None" >>nomatch.txt
   ;;
   ;;
esac
done

I am still trying at my end and would like to mention where the problem is :
Writing the rest of the statements that follow into the respective files.

Last edited by vbe; 08-28-2013 at 12:55 PM.. Reason: Code tags not Icode tags (were more icodes...)
# 2  
Old 08-28-2013
Try this:
Code:
awk     'FNR==1         {f++}
         f==1           {A[$2]=$0; next}
         f==2           {B[$2]=A[$3]; next}
                        {for (x in B) if ($0 ~ x) {print $0, B[x] > x".txt"; next}}
                        {print $0, "condition: none" > "nomatch"}
        ' file2 file3 RS= FS="\n" OFS="\n" file1


Last edited by RudiC; 08-29-2013 at 01:54 PM..
# 3  
Old 08-28-2013
Quote:
Originally Posted by w020637
I need to parse a file and depending on a patern match(in the insert job line) separate files have to be created with a line added (content in file2).
This is a simple example that will accomplish what I believe you want to:

Input file:
Code:
$ cat t
insert_job: Alpha_jaa    job_type: cmd
description: ABC
machine: XYZ

insert_job: Beta_baa    job_type: cmd
description: ABC
machine: XYZ

insert_job: Gamma_caa   job_type: cmd
description: ABC
machine: XYZ

insert_job: else_caa job_type: cmd
description: ABC
machine: XYZ

SHELL script:
Code:
# function: cond is used to write 'condition' line for each group
cond ()
{
if   [[ $which_group == "A" ]]; then
  echo "condition: 123" >> file_1.txt
elif [[ $which_group == "B" ]]; then
  echo "condition: 234" >> file_2.txt
elif [[ $which_group == "G" ]]; then
  echo "condition: 345" >> file_3.txt
elif [[ $which_group == "N" ]]; then
  echo "condition: None" >> file_4.txt
fi
return
}

while read -r -a LineToArray
do
  if [[ -z "${LineToArray[0]}" ]]; then
    :
  else
    if   [[ ${LineToArray[0]} == *insert_job: && ${LineToArray[1]} ==  *Alpha_* ]]; then
      cond $which_group
      echo ${LineToArray[@]} > file_1.txt
      which_group="A"

    elif [[ ${LineToArray[0]} == *insert_job: && ${LineToArray[1]} ==  *Beta_* ]]; then
      cond $which_group
      echo ${LineToArray[@]} > file_2.txt
      which_group="B"

    elif [[ ${LineToArray[0]} == *insert_job: && ${LineToArray[1]} ==  *Gamma_* ]]; then
      cond $which_group
      echo ${LineToArray[@]} > file_3.txt
      which_group="G"

    elif [[ ${LineToArray[0]} == *insert_job: ]]; then
      cond $which_group
      echo ${LineToArray[@]} > file_4.txt
      which_group="N"

    fi

    if   [[ ${LineToArray[0]} != *insert_job: && $which_group = "A" ]]; then
      echo ${LineToArray[@]} >> file_1.txt
    elif [[ ${LineToArray[0]} != *insert_job: && $which_group = "B" ]]; then
      echo ${LineToArray[@]} >> file_2.txt
    elif [[ ${LineToArray[0]} != *insert_job: && $which_group = "G" ]]; then
      echo ${LineToArray[@]} >> file_3.txt
    elif [[ ${LineToArray[0]} != *insert_job: && $which_group = "N" ]]; then
      echo ${LineToArray[@]} >> file_4.txt
    fi
  fi
done < t
cond $which_group

Files Created:
Code:
$ cat file_1.txt  file_2.txt file_3.txt file_4.txt
insert_job: Alpha_jaa job_type: cmd
description: ABC
machine: XYZ
condition: 123
insert_job: Beta_baa job_type: cmd
description: ABC
machine: XYZ
condition: 234
insert_job: Gamma_caa job_type: cmd
description: ABC
machine: XYZ
condition: 345
insert_job: else_caa job_type: cmd
description: ABC
machine: XYZ
condition: None

# 4  
Old 08-28-2013
thanks everyone..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Awk; pattern match, remove and re write

the following pattern match works correctly for me awk '/name="Fruits"/{f=1;next} /"name=Vegetables"/{f=0} f' filename This works well for me. Id like to temporarily move the match out of the file ( > newfile) and be able to stick it back in the same place at a later time. Is this... (7 Replies)
Discussion started by: TY718
7 Replies

2. Shell Programming and Scripting

Rearrange or replace only the second line after pattern match or pattern match

Im using the command below , but thats not the output that i want. it only prints the odd and even numbers. awk '{if(NR%2){print $0 > "1"}else{print $0 > "2"}}' Im hoping for something like this file1: Text hi this is just a test text1 text2 text3 text4 text5 text6 Text hi... (2 Replies)
Discussion started by: invinzin21
2 Replies

3. UNIX for Dummies Questions & Answers

[Solved] Grep multiple files and display first match

I have a need to grep a large number of files, but only display the first result from each file. I have tried to use grep, but am not limited to it. I can use perl and awk as well. Please help! (9 Replies)
Discussion started by: dbiggied
9 Replies

4. Shell Programming and Scripting

Pattern match using grep between two files

Hello Everyone , I have two files. I want to pick line from file-1 and match with the complete data in file-2 , if there is a match print all the match lines in file 3. Below is the file cat test1.txt vikas vikasjain j ain testt douknow hello@vik@ # 33 ||@@ vcpzxcmvhvdsh... (1 Reply)
Discussion started by: mailvkjain
1 Replies

5. UNIX Desktop Questions & Answers

[SOLVED] find command match pattern

Hello, I would like to ask you, how to match directory names. I need to find only directories, which are created only from numbers and doesn't include any letters. I used command find $AC_WORKDIR/work_archive/test/$dirs_years -maxdepth 1 -name \\* -print If I have dirs like 12... (3 Replies)
Discussion started by: satin1321
3 Replies

6. Shell Programming and Scripting

Request to check: compare two files , match same entries, write data before it

Hi all, I have 2 files:Column1 of first file has to be matched with column 3 of second file first file contain DATA like this in 2 columns one with gene name second with whether CAD,HT,RA T2Dor any one column 1 column2 ARFGEF2 CAD DDEF2 CAD PSCD3 CAD PSCD4 CAD CAMK1... (5 Replies)
Discussion started by: manigrover
5 Replies

7. UNIX for Dummies Questions & Answers

awk to match multiple regex and create separate output files

Howdy Folks, I have a list that looks like this: (file2.txt) AAA BBB CCC DDD and there are 24 of these short words. I am matching these patterns to another file with 755795 lines (file1.txt). I have this code for matching: awk -v f2=file2.txt ' BEGIN { while(... (2 Replies)
Discussion started by: heecha
2 Replies

8. Shell Programming and Scripting

write to multiple files depending on file type (solved)

I want a script that will add comments to a file before check-in files. comments depend on type of files. i have a script to list files in the directory that will be checked-in There are xml, js, html, vm files. vm will use comments like c/c++ ( // or /*..*/) can you help me add a comment line... (0 Replies)
Discussion started by: pradeepmacha
0 Replies

9. Shell Programming and Scripting

Separating Pattern Into Separate Files

I am trying to separate a specific pattern match into separate files. Sometimes there is only one pattern match, but other times there could be multiple (up to 6 or 8). Pattern is as follows - its starts with NYZ or VTZ and ends with $$. Again looking to get those blocks of data from one big... (17 Replies)
Discussion started by: Double-E
17 Replies

10. Shell Programming and Scripting

match text from two files and write to a third file

Hi all I have two files X.txt and Y.txt. Both file contains same number of sentences. The content of X.txt is The filter described above may be combined. and the content of Y.txt is The filter describ+ed above may be combin+ed. Some of the words are separated with "+"... (2 Replies)
Discussion started by: my_Perl
2 Replies
Login or Register to Ask a Question