Extracting lines and saving - awk


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Extracting lines and saving - awk
# 1  
Old 08-28-2008
Extracting lines and saving - awk

Hi All,

I am trying to extract lines bsed on pattern matching../mp straight-flow/
Extracted output should be saved in meta_string , but the code is not working in that manner,saving repeated lines. can anyone please suggest where am i going wrong.


/mp straight-flow/ {
start=index($0,"straight-flow");
if ($2=="straight-flow") meta_string=(meta_string "\n" substr($0,start));
print(meta_string);

mp straight-flow Transformation_records_to_fit_COBOL_Requirement.Generate_DAG_Counts.Flow_24 Transformation_records_to_fit_CO
BOL_Requirement.Generate_DAG_Counts.Repl_Produce_3_flows.out Transformation_records_to_fit_COBOL_Requirement.Generate_DAG_Cou
nts.Rlup_Count_DAG_Occurence.in -metadata metadata4
mp straight-flow Transformation_records_to_fit_COBOL_Requirement.Generate_DAG_Counts.Flow_25 Transformation_records_to_fit_CO
BOL_Requirement.Generate_DAG_Counts.Repl_Produce_3_flows.out Transformation_records_to_fit_COBOL_Requirement.Generate_DAG_Cou
nts.Jn_Add_DAG_Count_to_Incoming_Records.in.in0 -metadata metadata4 -buffer
mp straight-flow Transformation_records_to_fit_COBOL_Requirement.Generate_DAG_Counts.Flow_23 Transformation_records_to_fit_CO
BOL_Requirement.Generate_DAG_Counts.Rlup_Count_DAG_Occurence.out Transformation_records_to_fit_COBOL_Requirement.Generate_DAG
_Counts.Jn_Add_DAG_Count_to_Incoming_Records.in.in1 -metadata metadata5 -buffer


Regards,
Madhavi
# 2  
Old 08-28-2008
Code:
awk '{
         if ($0 ~ /mp straight-flow/) 
         {
            start=index($0,"straight-flow");
            if ($2=="straight-flow") 
            {
               meta_string=sprintf ("%s\n%%s",meta_string,substr($0,start)) 
            }
          } 
         }
         END {print meta_string} ' inputfile

assuming you meant awk - however your output would be the same if just printed the substring from each line. Why do you want all the lines in meta_string?
# 3  
Old 08-28-2008
Thanks for your response.

I want to extract all these lines which contains /mp straight-flow/ into an aray and have to search a specific pattern .
example:
Search the pattern "Rlup_Count_DAG_Occurence.in " in the array of extracted lines.
# 4  
Old 08-28-2008
This example shows how you can store the lines in an array. The for loop in the END section loops through the array and prints the lines that match the pattern.

Code:
awk -v pattern="Rlup_Count_DAG_Occurence.in" '
/mp straight-flow/{sub("mp ","");arr[++i]=$0}
END{for(i in arr){if(match(arr[i],pattern){print arr[i]}}}' file

Regards
# 5  
Old 08-29-2008
Thanks for your reply

its not working..

but i will take the pattern from another array and search in the extracted lines array. the pattern is not hard coded.if it matches i want to pickup the corresponding metadata field.

ex: mp straight-flow Transformation_records_to_fit_COBOL_Requirement.Generate_DAG_Counts.Flow_25 Transformation_records_to_fit_CO
BOL_Requirement.Generate_DAG_Counts.Repl_Produce_3_flows.out Transformation_records_to_fit_COBOL_Requirement.Generate_DAG_Cou
nts.Jn_Add_DAG_Count_to_Incoming_Records.in.in0 -metadata metadata4 -buffer
mp straight-flow Transformation_records_to_fit_COBOL_Requirement.Generate_DAG_Counts.Flow_23 Transformation_records_to_fit_CO
BOL_Requirement.Generate_DAG_Counts.Rlup_Count_DAG_Occurence.out Transformation_records_to_fit_COBOL_Requirement.Generate_DAG
_Counts.Jn_Add_DAG_Count_to_Incoming_Records.in.in1 -metadata metadata5 -buffer

when i save these lines in array need to search one pattern like "Jn_Add_DAG_Count_to_Incoming_Records" this not contains .in or .out at the end of the pattern and need to pick up metadat4 for .in and metadata5 for .in1

please help me out..this is the first time i am working on awk..
seraching pattern saving the lines into an array and search some other pattern in the saved array , if it matches in the array the corresponding another pattern need to be saved ....
.....its very difficult to me to do this starting itself..

please ...
# 6  
Old 09-02-2008
Limit the search pattern to:

Code:
Rlup_Count_DAG_Occurence

# 7  
Old 09-23-2008
How to use String in AWK across the functions

Hi ,

I m facing one problem with String usage in AWK . let me put what i need .

i have a function and there i used one string ( meta_string)

function 1 {
...................
...................
meta_string = " this string got some value of 500 characters";
..........
......... }

Now i want to use meta_string value in another function say function 2.

function 2 {

print(meta_string);
...............
.............. };

Now problem is in using meta_string in fucntion 2 when i try printing meta_string its printing empty string .

could you please carify how to use meta_string in function 2 or any is there any other way to handle this sproblem.

Note: As i m using AWK first time :-)

Thanks in advance for your help.
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Saving first n lines of history

Say you want to clear your .bash_history except for the first 25 lines. Try: sed -i -e 26,500d .bash_historyI have a some frequently-used routines parked in the first few lines, and they kept getting overwritten by more recent commands. (2 Replies)
Discussion started by: Xubuntu56
2 Replies

2. Shell Programming and Scripting

Extracting lines from a file with sed and awk

My source file is structured with two words on each line word1 word2 word1 word2 I am using sed and awk to grab groups of specific lines line=`awk 'NR>=4 && NR<=7' file1`; echo $line line=` sed -n '1,5'p file1`; echo $line The resulting output is word1 word2 word1 word2 word1... (1 Reply)
Discussion started by: bash_in_my_head
1 Replies

3. Shell Programming and Scripting

exit after extracting range if lines - awk

Hello, I was wondering how is it possible if I use this command: awk 'NR >= 998 && NR <= 1000' file.txtTo exit after parsing the 1000th line ( last line targeted) ??? I observed that when executing this command for a large file, if the range of lines is at the beginning of the file it is... (2 Replies)
Discussion started by: black_fender
2 Replies

4. UNIX for Dummies Questions & Answers

Help with deleting lines and saving them

I have a directory question where I ask the user which entry he wants to delete... echo "Which entry?" read entry sed '/^'$entry'/d' file This code does in fact delete that particular entry... HOWEVER, when I go to inquire about that same entry, it still populates like it was never... (4 Replies)
Discussion started by: itech4814
4 Replies

5. Shell Programming and Scripting

AWK script - extracting min and max values from selected lines

Hi guys! I'm new to scripting and I need to write a script in awk. Here is example of file on which I'm working ATOM 4688 HG1 PRO A 322 18.080 59.680 137.020 1.00 0.00 ATOM 4689 HG2 PRO A 322 18.850 61.220 137.010 1.00 0.00 ATOM 4690 CD ... (18 Replies)
Discussion started by: grincz
18 Replies

6. Shell Programming and Scripting

AWK- extracting values from columns, saving them and gettins statistics

Hello, I am obviously quite new to unix and awk. I need to parse certain columns of a file (delimited by spaces), and somehow save the value of this column somewhere, together with the value of the column just after it (by pairs; so something like ). I'm then supposed to count the times that... (9 Replies)
Discussion started by: acsg
9 Replies

7. Shell Programming and Scripting

Saving an AWK match

Is it possible to save the result of an AWK match to use later in a BASH script. Thanks, Jordon (4 Replies)
Discussion started by: jhirshon
4 Replies

8. Shell Programming and Scripting

Saving ranges using awk

Hi All, Is there a way to save a range in variable for later printing? for example write somthing like this: awk ' /pattern1/,/pattern2/{f=range} /pattern3/{print f} ' I don't know excatly what "range" could be but is there a way to do this? (8 Replies)
Discussion started by: ghoda2_10
8 Replies

9. Shell Programming and Scripting

Extracting lines by using awk

Hello, I have a file including some lines as follows person (1): a d t person (2): f h j person (3): z x v . . . (8 Replies)
Discussion started by: rpf
8 Replies

10. Shell Programming and Scripting

awk : extracting unique lines based on columns

Hi, snp.txt CHR_A SNP_A BP_A_st BP_A_End CHR_B BP_B SNP_B R2 p-SNP_A p-SNP_B 5 rs1988728 74904317 74904318 5 74960646 rs1427924 0.377333 0.000740085 0.013930081 5 ... (12 Replies)
Discussion started by: genehunter
12 Replies
Login or Register to Ask a Question