Help with matching pattern inside a file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Help with matching pattern inside a file
# 8  
Old 06-03-2011
Ernst,I assume the log_file ( cat of log1,log2..) have the data:A> reading is funB> hair sprayC> travel abroadA>the sky is blueB> train stationC>cup of waterfor this to get the desired output:awk -F">" '{f=$1;$1="";a[f]=$0"\n"a[f]} END { for(i in a) { print i"."a[i] }}' log_file | sed '/^$/d'
# 9  
Old 06-03-2011
Let me try to make it easier.
This is my input file:
Quote:
List13> I belong to list 13
List14> I belong to list 14
List23> I belong to list 23
List67> I belong to list 67
I also belong to list 13
I also belong to list 14
I also belong to list 23
I also belong to list 67
list 13 is my home
list 14 is my home
list 23 is my home
list 67 is my home
Put me under list 13
Put me under list 14
Put me under list 23
Put me under list 67
Like to go to 13 for vacation.
Like to go to 14 for vacation
Like to go to 23 for vacation
Like to go to 67 for vacation
sweet home 13
sweet home 14
sweet home 23
sweet home 67
13 could be a good numberr
14 could be a good numbe
23 could be a good number
67 could be a good number
This is my desired output file:
Quote:
List13> I belong to list 13
list 13 is my home
I also belong to list 13
Put me under list 13
Like to go to 13 for vacation
sweet home 13
13 could be a good number

List14> I belong to list 14
list 14 is my home
I also belong to list 14
Put me under list 14
Like to go to 14 for vacation
sweet home 14
14 could be a good number

List23> I belong to list 23
I also belong to list 23
list 23 is my home
Like to go to 23 for vacation
sweet home 23
23 could be a good number

List67> I belong to list 67
I also belong to list 67
list 67 is my home
Put me under list 67
Like to go to 67 for vacation
sweet home 67
67 could be a good number
How do I rearrange the input file to get the desired result?
# 10  
Old 06-03-2011
Brilliant Ernst, now that I know what you want, here is one possible solution:
Code:
#!/usr/bin/ksh
sed -n 's/List\(.*\)>.*/\1/p' inp_file | while read mString
do
  egrep "${mString}" inp_file
  echo ''
done

# 11  
Old 06-03-2011
Assuming the '>' sign only appear once per block :

Code:
nawk -v N="$(( $(grep '>' infile | wc -l) ))" '{A[NR]=$0}END{for(i=0;++i<=N;)for(j=0;++j<=NR;)if(j%N==i%N) print A[j]}' infile

or if you want block separated with an empty line add the red code:

Code:
nawk -v N="$(( $(grep '>' infile | wc -l) ))"  '{A[NR]=$0}END{for(i=0;++i<=N;){for(j=0;++j<=NR;)if(j%N==i%N) print  A[j]; print}}' infile

Code:
$ cat tst
List13> I belong to list 13
List14> I belong to list 14
List23> I belong to list 23
List67> I belong to list 67
I also belong to list 13
I also belong to list 14
I also belong to list 23
I also belong to list 67
list 13 is my home
list 14 is my home
list 23 is my home
list 67 is my home
Put me under list 13
Put me under list 14
Put me under list 23
Put me under list 67
Like to go to 13 for vacation.
Like to go to 14 for vacation
Like to go to 23 for vacation
Like to go to 67 for vacation
sweet home 13
sweet home 14
sweet home 23
sweet home 67
13 could be a good numberr
14 could be a good numbe
23 could be a good number
67 could be a good number

Code:
$ nawk -v N="$(( $(grep '>' tst | wc -l) ))" '{A[NR]=$0}END{for(i=0;++i<=N;){for(j=0;++j<=NR;)if(j%N==i%N) print A[j];print}}' tst
List13> I belong to list 13
I also belong to list 13
list 13 is my home
Put me under list 13
Like to go to 13 for vacation.
sweet home 13
13 could be a good numberr

List14> I belong to list 14
I also belong to list 14
list 14 is my home
Put me under list 14
Like to go to 14 for vacation
sweet home 14
14 could be a good numbe

List23> I belong to list 23
I also belong to list 23
list 23 is my home
Put me under list 23
Like to go to 23 for vacation
sweet home 23
23 could be a good number

List67> I belong to list 67
I also belong to list 67
list 67 is my home
Put me under list 67
Like to go to 67 for vacation
sweet home 67
67 could be a good number

$


Last edited by ctsgnb; 06-03-2011 at 03:54 PM..
# 12  
Old 06-03-2011
What I really want is the following:
INPUT FILE:
Quote:
Alpha> lh ru warpA read DL_PM_PA0_C0
Beta> lh ru warpA read DL_PM_PA0_C0
Gamma> lh ru warpA read DL_PM_PA0_C0
Delta> lh ru warpA read DL_PM_PA0_C0
$ lhsh BXP_0_1 warpA read DL_PM_PA0_C0
$ lhsh BXP_0_1 warpA read DL_PM_PA0_C0
$ lhsh BXP_0_1 warpA read DL_PM_PA0_C0
$ lhsh BXP_0_1 warpA read DL_PM_PA0_C0
BXP_0_1: Value 0x01CC9739 (30185273) read from address 0x00000B8F.
BXP_0_1: Value 0x050A2F06 (84553478) read from address 0x00000B8F.
BXP_0_1: Value 0x02563DEF (39206383) read from address 0x00000B8F.
BXP_0_1: Value 0x01CB58B7 (30103735) read from address 0x00000B8F.
$ lhsh BXP_1_1 warpA read DL_PM_PA0_C0
$ lhsh BXP_1_1 warpA read DL_PM_PA0_C0
$ lhsh BXP_1_1 warpA read DL_PM_PA0_C0
$ lhsh BXP_1_1 warpA read DL_PM_PA0_C0
BXP_1_1: Value 0x05033922 (84097314) read from address 0x00000B8F.
BXP_1_1: Value 0x01CCEFB6 (30207926) read from address 0x00000B8F.
BXP_1_1: Value 0x01CED447 (30331975) read from address 0x00000B8F.
BXP_1_1: Value 0x0218E0BA (35184826) read from address 0x00000B8F.
$ lhsh BXP_2_1 warpA read DL_PM_PA0_C0
$ lhsh BXP_2_1 warpA read DL_PM_PA0_C0
$ lhsh BXP_2_1 warpA read DL_PM_PA0_C0
$ lhsh BXP_2_1 warpA read DL_PM_PA0_C0
BXP_2_1: Value 0x0236B631 (37140017) read from address 0x00000B8F.
BXP_2_1: Value 0x01CE0AF3 (30280435) read from address 0x00000B8F.
BXP_2_1: Value 0x050FAD30 (84913456) read from address 0x00000B8F.
BXP_2_1: Value 0x01CCCC5A (30198874) read from address 0x00000B8F.
OUTPUT FILE:
Quote:
Alpha> lh ru warpA read DL_PM_PA0_C0
$ lhsh BXP_0_1 warpA read DL_PM_PA0_C0
BXP_0_1: Value 0x01CC9739 (30185273) read from address 0x00000B8F
$ lhsh BXP_1_1 warpA read DL_PM_PA0_C0
BXP_1_1: Value 0x05033922 (84097314) read from address 0x00000B8F
$ lhsh BXP_2_1 warpA read DL_PM_PA0_C0
BXP_2_1: Value 0x0236B631 (37140017) read from address 0x00000B8F
Beta> lh ru warpA read DL_PM_PA0_C0
$ lhsh BXP_0_1 warpA read DL_PM_PA0_C0
BXP_0_1: Value 0x050A2F06 (84553478) read from address 0x00000B8F
$ lhsh BXP_1_1 warpA read DL_PM_PA0_C0
BXP_1_1: Value 0x01CCEFB6 (30207926) read from address 0x00000B8F
BXP_1_1: Value 0x01CED447 (30331975) read from address 0x00000B8F
$ lhsh BXP_2_1 warpA read DL_PM_PA0_C0
BXP_2_1: Value 0x01CE0AF3 (30280435) read from address 0x00000B8F
Gamma> lh ru warpA read DL_PM_PA0_C0
$ lhsh BXP_0_1 warpA read DL_PM_PA0_C0
BXP_0_1: Value 0x02563DEF (39206383) read from address 0x00000B8F
$ lhsh BXP_1_1 warpA read DL_PM_PA0_C0
$ lhsh BXP_2_1 warpA read DL_PM_PA0_C0
BXP_2_1: Value 0x050FAD30 (84913456) read from address 0x00000B8F
Delta> lh ru warpA read DL_PM_PA0_C0
$ lhsh BXP_0_1 warpA read DL_PM_PA0_C0
BXP_0_1: Value 0x01CB58B7 (30103735) read from address 0x00000B8F
$ lhsh BXP_1_1 warpA read DL_PM_PA0_C0
BXP_1_1: Value 0x0218E0BA (35184826) read from address 0x00000B8F.
$ lhsh BXP_2_1 warpA read DL_PM_PA0_C0
BXP_2_1: Value 0x01CCCC5A (30198874) read from address 0x00000B8F
When you write the code, keep in mind that the input file might contain more info from epsilon, theta, sigma and so on. I just need to know how to rearrange the file.

Thanks.
# 13  
Old 06-03-2011
Ernst, you specified a sample input data and the expected output.

I wrote a solution based on your sample data.

Now you changed your input data in such a way that my solution will no longer work.

Please, decide on what you want and stick with it.
# 14  
Old 06-03-2011
---OOOppps ---.

I think i should reread th thread from the beginning ...Smilie

Last edited by ctsgnb; 06-03-2011 at 04:38 PM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Copy pattern inside the file

Hi all, I have files and have a missing record. I need copy the existing record and mark those values up. For example in the below file 11048 is missing. I need to copy 22001 and copy those create the values for 11048. I have 120 set of files and I need to do that on all files. Note the... (8 Replies)
Discussion started by: arunkumar_mca
8 Replies

2. Shell Programming and Scripting

Big pattern file matching within another pattern file in awk or shell

Hi I need to do a patten match between files . I am new to shell scripting and have come up with this so far. It take 50 seconds to process files of 2mb size . I need to tune this code as file size will be around 50mb and need to save time. Main issue is that I need to search the pattern from... (2 Replies)
Discussion started by: nitin_daharwal
2 Replies

3. Shell Programming and Scripting

Take only lastest file matching the pattern

Hi All , I have some fille names in a file which are sorted according to date. for example file1_123.log 23 Jul 0Kb file2_123.log 22 Jul 2Kb file3_123.log 20 Jul 0Kb file1_456.log 24 Jul 2Kb file2_678.log 22 Jul 0Kb file2_678.log 21 Jul 2Kb here 123 is a... (2 Replies)
Discussion started by: LoneRanger
2 Replies

4. Shell Programming and Scripting

Finding the pattern and replacing the pattern inside the file

i have little challenge, help me out.i have a file where i have a value declared and and i have to replace the value when called. for example i have the value for abc and ccc. now i have to substitute the value of value abc and ccc in the place of them. Input File: go to &abc=ddd; if... (16 Replies)
Discussion started by: saaisiva
16 Replies

5. Shell Programming and Scripting

Renumber position 88-94 inside all files matching criteria inside folder

There are 4 files inside one folder matching criteria i.e. File name = ABCJmdmfbsjopXXXXXXX_mm-dd-yyyy_XXX.data Here is the Code which find the files matching criteria:- TS=`date +"%m-%d-%Y"`| for fname in `find . -name "ABCJmdmfbsjop???????_${TS}*.data"` do # Matching File Processing Code.... (1 Reply)
Discussion started by: lancesunny
1 Replies

6. Shell Programming and Scripting

how to find the pattern inside the file and replace it

hello everybody, I have a group of file eg- sample1 sample2 sample3 sample4 each file contain this :- cat sample1 SEQ_NUM,1,UPESI1 My requirement is to change the value-UPESI1 to UPE10 in file which contain this pattern -UPESI1. any help is appreciated. (2 Replies)
Discussion started by: abhigrkist
2 Replies

7. Shell Programming and Scripting

Matching using Regex inside a file

I need scan through some files, then open the file one by one and scan inside the file using perl to see if it contain a start tag and end tag which the end tag is the mirror image of the start tag, the start tag and end tag only have 5 char. And inside the file there is "http://". It is just a... (5 Replies)
Discussion started by: blueblur
5 Replies

8. Programming

File Pattern Matching C++

Hi, I have large files with fixed length fields or fields seperated by delimeter. I would like to do validation on some or all fields to check for numeric or date or characters etc.. I would like to write this in C++. Please let me know if any one have any ideas on this. Thanks for all... (2 Replies)
Discussion started by: rameshmelam
2 Replies

9. Shell Programming and Scripting

Pattern matching for file

Hi All, I'm new to perl, My requirement is to check if particular file exists. e.g. filename.txt, filename1.txt, filename2.txt etc I tried the below code:- my $var1 = "filename.txt" if ( -e ($var1 = ~ /file\w/)) { print "File found \n"; } else { print "File not found \n"; } ... (0 Replies)
Discussion started by: doitnow
0 Replies

10. Shell Programming and Scripting

How to search a pattern inside a zipped file ie (.gz file) with out unzipping it

How to search a pattern inside a zipped file ie (.gz file) with out unzipping it? using grep command.. Bit urgent.. pls..help me (2 Replies)
Discussion started by: senraj01
2 Replies
Login or Register to Ask a Question