AWK: Grep Pattern and print help


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting AWK: Grep Pattern and print help
# 1  
Old 02-27-2012
AWK: Grep Pattern and print help

I wanted to get outcome from a big file with pattern quoted:
Line FSP LSP SR RL
Test1 100 300 4 4000
Test2 1 300 2 300

Any help is greatly appreciated. Thank you.

Quote:
Line: Test1
FSP: 100 LSP: 300 SR: 4 RL: 4000
Line: Test2 FSP: 1 LSP: 300 SR: 2 RL: 3000
Line: Test3 FSP: 2 LSP: 400
# 2  
Old 02-27-2012
You want to grep three different lines, or one group of three lines?

Code:
awk '/^Line/ { print ; getline ; print; getline ; print }' filename


Last edited by Corona688; 02-27-2012 at 12:58 PM..
# 3  
Old 02-27-2012
Great!.. almost there. I wanted to get in a row for each unique Line instead of separate new line.

Thanks.
# 4  
Old 02-27-2012
I think you need to explain what you want better. This will do what you asked for, but probably not what you want.

Code:
awk '# Get list of columns from first line
NR==1 { for(N=1; N<=NF; N++) { A[N]=$N } next }

# Add them as column prefixes to all other lines
{ for(N=1; N<=NF; N++) $N = A[$N] ": " $N; } 1' file

# 5  
Old 02-27-2012
I wanted to get a result that I can put to excel eventually.
Header---> LineName FSP LSP SR RL
Result----> Test1 100 300 4 4000
Result----> Test2 1 300 3 3000
and so on...

Quote:
My data look like this in general:

A Line: 100 has FSP: 100 with LSP : 300 and SR : 4 and RL : 4000 .....
....bla..bla text.....
Line: 200 has FSP : 1 with LSP: 300 and SR : 3 and RL: 3000 ....and this go on with the same pattern outcome.
# 6  
Old 02-27-2012
I did get it exactly backwards then.

Your data is not consistent, some of it is "this: value", others are "this : value". Some has "A line", some has "Line". Which is true?

Also: Will columns always arrive in the same order, or not?

---------- Post updated at 12:01 PM ---------- Previous update was at 11:41 AM ----------

Assuming the order of columns isn't changing all the time:

Code:
$ cat data

Line: 100 has FSP: 100 with LSP: 300 and SR: 4 and RL: 4000
Line: 200 has FSP: 1 with LSP: 300 and SR: 3 and RL: 3000

$ cat rearrange.awk

BEGIN { FS="\t"; OFS="\t" }
{
        # Replace the various has/and/width and their spaces with a single tab.
        gsub(/ *(has|and|width) */, "\t");
        # Eliminate some more extra spaces.
        gsub(/ *: */, ":");

        # Now that we have them arranged in sane columns:
        PF=""
        for(N=1; N<=NF; N++)
        {
                # Split NR:1 into A[1]="NR", A[2]="1"
                split($N, A, ":");

                # Print row of titles if this is the first line
                if(NR == 1)
                {
                        printf(PF "%s", A[1]);
                        PF=FS;
                }

                # Set the column to the value and nothing else
                $N=A[2];
        }

        if(NR==1)       printf("\n");

        # Print the row of data for every single line
} 1

$ awk -f rearrange.awk data

Line    FSP     LSP     SR      RL
100     100     300     4       4000
200     1       300     3       3000

$


Last edited by Corona688; 02-27-2012 at 01:54 PM..
# 7  
Old 02-27-2012
My pattern to grep are essentially /Line:/ then print after Line: i.e 100, next pattern to grep /FSP:/ and print Value 100, next /LSP:/ print Value 300, /SR:/ print 4 & /RL:/ print 4000..then next line repeat. I don't really care for the number of spacing in between the result as long there are separation. Then upon final print I would echo title text : LINENAME FSP LSP SR RL followed by the results underneath the title would be my results from the pattern grep earlier.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Recursively grep for a pattern and print that whole word

Hello Forum Members, I am trying to write a script for a requirement where i have to recursively search for a pattern and replace it with the new string in run time from user inputs grep -ohr "]*.xyz.com]*" $HOME/source/group/ | sort | uniq > $HOME/output.txt while read -r -u9 line; ... (4 Replies)
Discussion started by: raokl
4 Replies

2. AIX

Grep a pattern and print following n lines

Hi all, I am struck with the below requirement. I need to grep a particular pattern in a file and then print next n lines of it for further processing. I have used the below code grep -A 3 "pattern" filename But it is throwing error as below. grep: illegal option -- A Can... (14 Replies)
Discussion started by: ssk250
14 Replies

3. Shell Programming and Scripting

sed and awk usage to grep a pattern 1 and with reference to this grep a pattern 2 and pattern 3

Hi , I have a file where i have modifed certain things compared to original file . The difference of the original file and modified file is as follows. # diff mir_lex.c.modified mir_lex.c.orig 3209c3209 < if(yy_current_buffer -> yy_is_our_buffer == 0) { --- >... (5 Replies)
Discussion started by: breezevinay
5 Replies

4. Shell Programming and Scripting

How to print the lines between the pattern using awk/grep/sed?

Hi, I need a help to search a pattern and print the multiple lines between them. Input file: Tue May 29 12:30:33 EDT 2012:threadWebContainer : 357:com.travimp.hotelierlinks.abba.service.RequestHandler.requestService(String, ITICSDataSet): hotelCancelReservation request: ... (4 Replies)
Discussion started by: aroragaurav.84
4 Replies

5. Shell Programming and Scripting

grep based on pattern in a line and print the column before that

$ cat file.log Message Number = : Sending message 10:50:16^|^reqhdr.dummyid^=^02^|^reqhdr.timezone^=^GMT+05:30^|^DUMMYREQUEST^=^BH||||||||||||||||||$BD|OL|C|V||DummyAcctNo|02||24/12/2011|ST_DDM|DDM||||||||reqUUID110612105016$BT||||||||||||||||||$] Length I have the above line in the... (4 Replies)
Discussion started by: kalidass
4 Replies

6. Shell Programming and Scripting

Print the above and below lines for the grep pattern.

Hi, i would like to get the above and below lines of the grep pattern . For ex : file as below: chk1- aaaa 1-Nov chk2 -aaaa ########## chk1-bbbbbb 1-Nov chk2-bbbbbb ######### my search pattern is date : 1-Nov i need the o/p as below chk1- aaaa 1-Nov (6 Replies)
Discussion started by: expert
6 Replies

7. Solaris

Using grep to print just the pattern match

Hi all, Is it possible for grep to output just the pattern match and not the whole line when it comes across a match? I know you can adjust the number of trailing or leading lines that are printed, but am yet to find anything that outputs just the pattern match. Cheers, Tim (5 Replies)
Discussion started by: muzzaw
5 Replies

8. UNIX for Dummies Questions & Answers

Lynx Grep Pattern Match 2 conditions Print from Start to End

I am working on a scraping project and I am stuck at this tiny grep pattern match. Sample text : FPA List. FPA List. FPA List. FPA List. FPA List. FPA List. FPA List. FPA List. ABC Personal Planning Catherine K. Wat Cath Wat Catherine K. Wat Catherine K. Wat IFRAME:... (8 Replies)
Discussion started by: kkiran
8 Replies

9. Shell Programming and Scripting

Use to awk to match pattern, and print the pattern

Hi, I know how to use awk to search some expressions like five consecutive numbers, , this is easy. However, how do I make awk print the pattern that is been matched? For example: input: usa,canada99292,japan222,france59664,egypt223 output:99292,59664 (6 Replies)
Discussion started by: grossgermany
6 Replies

10. Shell Programming and Scripting

Grep for a pattern and print entire record

Hi friends, This is my very first post on forum, so kindly excuse if my doubts are found too silly. I am trying to automate a piece of routine work and this is where I am stuck at the moment-I need to grep a particular ID through a file containing many records(which start with <LRECORD> and end... (6 Replies)
Discussion started by: faiz1985
6 Replies
Login or Register to Ask a Question