LinuxQuestions.org
Visit the LQ Articles and Editorials section
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
LinkBack Search this Thread
Old 08-31-2009, 01:11 AM   #1
bangaram
LQ Newbie
 
Registered: Aug 2009
Posts: 6

Rep: Reputation: 0
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
 
Old 08-31-2009, 01:50 AM   #2
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Servers: Debian Squeeze and Wheezy. Desktop: Slackware64 14.0. Netbook: Slackware 13.37
Posts: 8,512
Blog Entries: 27

Rep: Reputation: 1174Reputation: 1174Reputation: 1174Reputation: 1174Reputation: 1174Reputation: 1174Reputation: 1174Reputation: 1174Reputation: 1174
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

Last edited by catkin; 08-31-2009 at 02:58 AM.
 
Old 08-31-2009, 02:05 AM   #3
joeBuffer
Member
 
Registered: Jul 2009
Distribution: Ubuntu 9.04
Posts: 325

Rep: Reputation: 42
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.

Last edited by joeBuffer; 08-31-2009 at 03:13 AM.
 
Old 08-31-2009, 03:22 AM   #4
bangaram
LQ Newbie
 
Registered: Aug 2009
Posts: 6

Original Poster
Rep: Reputation: 0
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 View Post
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.
 
Old 08-31-2009, 03:42 AM   #5
bangaram
LQ Newbie
 
Registered: Aug 2009
Posts: 6

Original Poster
Rep: Reputation: 0
You are the Man!!,its working

Quote:
Originally Posted by bangaram View Post
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
 
Old 08-31-2009, 03:42 AM   #6
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Servers: Debian Squeeze and Wheezy. Desktop: Slackware64 14.0. Netbook: Slackware 13.37
Posts: 8,512
Blog Entries: 27

Rep: Reputation: 1174Reputation: 1174Reputation: 1174Reputation: 1174Reputation: 1174Reputation: 1174Reputation: 1174Reputation: 1174Reputation: 1174
Code:
BEGIN {
    image5 = ""
}

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


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
variable length string using GD (word wrap, carriage return, word/character count)? frieza Programming 1 02-14-2009 05:21 PM
Problems Copying & Pasting In Word When Word Closes - Ubuntu davidx Linux - Software 3 10-22-2008 08:21 PM
sed 1st word to replace all instances of ( donnied Programming 5 08-21-2008 06:43 PM
Reading and printing the 5th, 10th, 15th...word of any file dahr Linux - Newbie 2 07-28-2008 03:51 PM
[SOLVED] find a word in a file, and change a word beneath it ?? vikas027 Programming 10 02-14-2008 09:46 PM


All times are GMT -5. The time now is 01:28 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration