LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   print second word in 1st line along with 5th word in all the lines after the first (https://www.linuxquestions.org/questions/programming-9/print-second-word-in-1st-line-along-with-5th-word-in-all-the-lines-after-the-first-751394/)

bangaram 08-31-2009 01:11 AM

print second word in 1st line along with 5th word in all the lines after the first
 
i have a file in this pattern

MATCH1 word1 IMAGE word3 word4
MATCH2 word1 word2 word3 word4
MATCH2 word1 word2 word3 word4
MATCH2 word1 word2 word3 word4
MATCH2 word1 word2 word3 word4
MATCH1 word1 IMAGE word3 word4
MATCH2 word1 word2 word3 word4
MATCH2 word1 word2 word3 word4
MATCH2 word1 word2 word3 word4
MATCH2 word1 word2 word3 word4
MATCH1 word1 IMAGE word3 word4

now where ever i find MACTH1 in the file i need to print all the word3's in MATCH2 line till next MATCH1 appears.My o/p should look like this

IMAGE word2,word2,word2

Hope my question is clear.Please help me on this

catkin 08-31-2009 01:50 AM

Your question is clear and sounds very much like homework. awk would be an ideal programming language to solve the problem. What have you tried so far? What resources do you know about for awk?

Edit:

After reading joeBuffer's post and re-reading the OP ...

The question is not clear but I get the general drift. Clearer to give the input as

word1.1 word1.2 word1.3 word1.4 word1.5
word2.1 word2.2 word2.3 word2.4 word2.5
word3.1 word3.2 word3.3 word3.4 word3.5
...
wordn.1 wordn.2 wordn.3 wordn.4 wordn.5

And then specify the output in similar terms

joeBuffer 08-31-2009 02:05 AM

The way it's phrased, the question isn't 100% clear (there are differences between the title and the actual post).

Here's an awk script that might not be written as well as it could be that might help (it doesn't include the commas you have in your output example, though):
Code:

BEGIN {
    OFS=""
    ORS=""
}

$1 ~ /MATCH1/ {
    if (NR == 1) {print $3, " "}
    else {print "\n", $3, " "}
}
$1 ~ /MATCH2/ {
    {print $3, " "}
}

END {print "\n"}

You could save that in an .awk file (your_file.awk) then use
Code:

awk -f your_file.awk your_other_file
I changed my first sentence to point out that what confused me was the difference between the title and the actual post, more than what should be done and what the output should look like. Also, I'd probably be able to offer better help, but I need to study awk and sed more.

bangaram 08-31-2009 03:22 AM

Hi Everybody

Thanks for ur i/p's.Below is the file i am trying to workaround

IMAGE ssyd0351vpor 8 ssyd0351vpor_1251558134 ORA_UNIX_UIB 0 UIB_ora 2 10 29 1566918134 0 0
FRAG 1 1 721664 0 2 6 18 AD1669 ssyd0118pbp 262144 167785 1251554178 3 0 *NULL* 1566918134 1 10 0 *NULL*
FRAG 1 2 197120 0 2 6 19 AD1669 ssyd0118pbp 262144 184295 1251554178 3 0 *NULL* 0 0 10 0 *NULL*
FRAG 1 3 299008 0 2 6 20 AD1669 ssyd0118pbp 262144 189611 1251554178 3 0 *NULL* 0 0 10 0 *NULL*
FRAG 1 4 122112 0 2 6 21 AD1669 ssyd0118pbp 262144 199434 1251554178 3 0 *NULL* 0 0 10 0 *NULL*
FRAG 1 5 1639936 0 2 6 22 AD1669 ssyd0118pbp 262144 202822 1251554178 3 0 *NULL* 0 0 10 0 *NULL*
FRAG 1 6 203776 0 2 6 23 AD1669 ssyd0118pbp 262144 239407 1251554178 3 0 *NULL* 0 0 10 0 *NULL*
FRAG 1 7 1769216 0 2 6 24 AD1669 ssyd0118pbp 262144 245358 1251554178 3 0 *NULL* 0 0 10 0 *NULL*
FRAG 1 8 1507840 0 2 6 25 AD1669 ssyd0118pbp 262144 285365 1251554178 3 0 *NULL* 0 0 10 0 *NULL*
FRAG 1 9 479744 0 2 6 26 AD1669 ssyd0118pbp 262144 321269 1251554178 3 0 *NULL* 0 0 10 0 *NULL*
FRAG 1 10 673280 0 2 6 27 AD1669 ssyd0118pbp 262144 334504 1251554178 3 0 *NULL* 0 0 10 0 *NULL*
FRAG 1 11 884480 0 2 6 28 AD1669 ssyd0118pbp 262144 350966 1251554178 3 0 *NULL* 0 0 10 0 *NULL*
FRAG 1 12 506483 512 2 6 29 AD1669 ssyd0118pbp 262144 375192 1251554178 3 0 *NULL* 0 0 10 0 *NULL*
IMAGE ssyd2131por 8 ssyd2131por_1251564868 ORA_UNIX_UIB 0 UIB_ora 2 10 23 1566924868 0 0
FRAG 1 1 2587904 0 2 6 51 AD1669 ssyd0118pbp 262144 849842 1251554178 6 0 *NULL* 1566924868 1 10 0 *NULL*
FRAG 1 2 270848 0 2 6 52 AD1669 ssyd0118pbp 262144 874468 1251554178 6 0 *NULL* 0 0 10 0 *NULL*
FRAG 1 3 750592 0 2 6 53 AD1669 ssyd0118pbp 262144 878232 1251554178 6 0 *NULL* 0 0 10 0 *NULL*
FRAG 1 4 10240000 0 2 6 54 AD1669 ssyd0118pbp 262144 892152 1251554178 6 0 *NULL* 0 0 10 0 *NULL*
FRAG 1 5 10240000 0 2 6 55 AD1669 ssyd0118pbp 262144 932154 1251554178 6 0 *NULL* 0 0 10 0 *NULL*
FRAG 1 6 6563805 512 2 6 56 AD1669 ssyd0118pbp 262144 972156 1251554178 6 0 *NULL* 0 0 10 0 *NULL*
IMAGE ssyd2150por 8 ssyd2150por_1251640706 ORA_UNIX_UIB 0 UIB_ora 2 10 25 1567000706 0 0
FRAG 1 1 3698944 0 2 6 12 AD1677 ssyd0118pbp 262144 346335 1251468005 5 0 *NULL* 1567000706 1 10 0 *NULL*
FRAG 1 2 597248 0 2 6 13 AD1677 ssyd0118pbp 262144 375266 12



From the line whereever i find IMAGE i need $5 i.e 5th word and from the line where it starts with FRAG i need $9 .

for abv lines it should come as

ORA_UNIX_UIB AD1669,AD1669,AD1669,AD1669,AD1669,AD1669,AD1669,AD1669
ORA_UNIX_UIB AD1677,AD1677

sorry for not making my question clear



Quote:

Originally Posted by joeBuffer (Post 3663554)
The way it's phrased, the question isn't 100% clear (there are differences between the title and the actual post).

Here's an awk script that might not be written as well as it could be that might help (it doesn't include the commas you have in your output example, though):
Code:

BEGIN {
    OFS=""
    ORS=""
}

$1 ~ /MATCH1/ {
    if (NR == 1) {print $3, " "}
    else {print "\n", $3, " "}
}
$1 ~ /MATCH2/ {
    {print $3, " "}
}

END {print "\n"}

You could save that in an .awk file (your_file.awk) then use
Code:

awk -f your_file.awk your_other_file
I changed my first sentence to point out that what confused me was the difference between the title and the actual post, more than what should be done and what the output should look like. Also, I'd probably be able to offer better help, but I need to study awk and sed more.


bangaram 08-31-2009 03:42 AM

You are the Man!!,its working

Quote:

Originally Posted by bangaram (Post 3663623)
Hi Everybody

Thanks for ur i/p's.Below is the file i am trying to workaround

IMAGE ssyd0351vpor 8 ssyd0351vpor_1251558134 ORA_UNIX_UIB 0 UIB_ora 2 10 29 1566918134 0 0
FRAG 1 1 721664 0 2 6 18 AD1669 ssyd0118pbp 262144 167785 1251554178 3 0 *NULL* 1566918134 1 10 0 *NULL*
FRAG 1 2 197120 0 2 6 19 AD1669 ssyd0118pbp 262144 184295 1251554178 3 0 *NULL* 0 0 10 0 *NULL*
FRAG 1 3 299008 0 2 6 20 AD1669 ssyd0118pbp 262144 189611 1251554178 3 0 *NULL* 0 0 10 0 *NULL*
FRAG 1 4 122112 0 2 6 21 AD1669 ssyd0118pbp 262144 199434 1251554178 3 0 *NULL* 0 0 10 0 *NULL*
FRAG 1 5 1639936 0 2 6 22 AD1669 ssyd0118pbp 262144 202822 1251554178 3 0 *NULL* 0 0 10 0 *NULL*
FRAG 1 6 203776 0 2 6 23 AD1669 ssyd0118pbp 262144 239407 1251554178 3 0 *NULL* 0 0 10 0 *NULL*
FRAG 1 7 1769216 0 2 6 24 AD1669 ssyd0118pbp 262144 245358 1251554178 3 0 *NULL* 0 0 10 0 *NULL*
FRAG 1 8 1507840 0 2 6 25 AD1669 ssyd0118pbp 262144 285365 1251554178 3 0 *NULL* 0 0 10 0 *NULL*
FRAG 1 9 479744 0 2 6 26 AD1669 ssyd0118pbp 262144 321269 1251554178 3 0 *NULL* 0 0 10 0 *NULL*
FRAG 1 10 673280 0 2 6 27 AD1669 ssyd0118pbp 262144 334504 1251554178 3 0 *NULL* 0 0 10 0 *NULL*
FRAG 1 11 884480 0 2 6 28 AD1669 ssyd0118pbp 262144 350966 1251554178 3 0 *NULL* 0 0 10 0 *NULL*
FRAG 1 12 506483 512 2 6 29 AD1669 ssyd0118pbp 262144 375192 1251554178 3 0 *NULL* 0 0 10 0 *NULL*
IMAGE ssyd2131por 8 ssyd2131por_1251564868 ORA_UNIX_UIB 0 UIB_ora 2 10 23 1566924868 0 0
FRAG 1 1 2587904 0 2 6 51 AD1669 ssyd0118pbp 262144 849842 1251554178 6 0 *NULL* 1566924868 1 10 0 *NULL*
FRAG 1 2 270848 0 2 6 52 AD1669 ssyd0118pbp 262144 874468 1251554178 6 0 *NULL* 0 0 10 0 *NULL*
FRAG 1 3 750592 0 2 6 53 AD1669 ssyd0118pbp 262144 878232 1251554178 6 0 *NULL* 0 0 10 0 *NULL*
FRAG 1 4 10240000 0 2 6 54 AD1669 ssyd0118pbp 262144 892152 1251554178 6 0 *NULL* 0 0 10 0 *NULL*
FRAG 1 5 10240000 0 2 6 55 AD1669 ssyd0118pbp 262144 932154 1251554178 6 0 *NULL* 0 0 10 0 *NULL*
FRAG 1 6 6563805 512 2 6 56 AD1669 ssyd0118pbp 262144 972156 1251554178 6 0 *NULL* 0 0 10 0 *NULL*
IMAGE ssyd2150por 8 ssyd2150por_1251640706 ORA_UNIX_UIB 0 UIB_ora 2 10 25 1567000706 0 0
FRAG 1 1 3698944 0 2 6 12 AD1677 ssyd0118pbp 262144 346335 1251468005 5 0 *NULL* 1567000706 1 10 0 *NULL*
FRAG 1 2 597248 0 2 6 13 AD1677 ssyd0118pbp 262144 375266 12



From the line whereever i find IMAGE i need $5 i.e 5th word and from the line where it starts with FRAG i need $9 .

for abv lines it should come as

ORA_UNIX_UIB AD1669,AD1669,AD1669,AD1669,AD1669,AD1669,AD1669,AD1669
ORA_UNIX_UIB AD1677,AD1677

sorry for not making my question clear


catkin 08-31-2009 03:42 AM

Code:

BEGIN {
    image5 = ""
}

$1 ~ /IMAGE/ {
    if (image5 != "") {print image5 " " substr(buffer, 2)}
    image5 = $5
        buffer = ""
}
$1 ~ /FRAG/ {
    buffer = buffer "," $9
}



All times are GMT -5. The time now is 04:48 AM.