The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
return previous line for pattern match braindrain UNIX for Dummies Questions & Answers 4 12-08-2008 05:17 PM
print pattern line +2 without tabs or brackets repudi8or Shell Programming and Scripting 2 04-16-2008 08:39 PM
pattern match in each line and capture it question bsandeep_80 UNIX for Advanced & Expert Users 16 04-02-2008 02:39 PM
Concatenating multiple lines to one line if match pattern phixsius Shell Programming and Scripting 13 01-24-2008 11:02 PM
sed - Replace Line which contains the Pattern match with a new line kousikan Shell Programming and Scripting 2 03-24-2007 07:24 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 07-28-2005
nymus7 nymus7 is offline
Registered User
  
 

Join Date: Apr 2005
Posts: 47
match a pattern, print it and the next line

I have a file nbu_faq.txt (Question/answer) which looks like this
Quote:
Q: What is nbu?
A: blablabla...
Q: What is blablabla...?
A: blablabla...
Q: Why ....?
A: blablabla...
and so on...
What I am trying to do is write out each question in a file1.txt and than the question/answer in a file2.txt
like this
file1.txt
Q: What is nbu?
Q: What is blablabla...?
Q: Why ....?
file2.txt
Q: What is nbu?
A: blablabla...
Q: What is blablabla...?
A: blablabla...
Q: Why ....?
A: blablabla...
Code:
for QUESTIONS in $(cat nbu_faq.txt |grep "Q: " |awk '{print $0}' )
do
  echo $QUESTIONS >> TITLE
  echo $(awk '$1 == "$QUESTIONS" {
          print $0
          next
  }
  {print $0} ' nbu_faq.txt) >> QUEST_ANSWER
done
Where TITLE and QUEST_ANSWER are file1.txt and file2.txt
1. first problem is that the loop take argument $1,$2,$3,... for $0 and not the whole line (ex: Q: What is nbu?)!
2. second problem is to print the line where the pattern $QUESTION is matching and the next line!
Could someone help me?
  #2 (permalink)  
Old 07-28-2005
vino's Avatar
vino vino is offline Forum Staff  
Supporter (in vino veritas)
  
 

Join Date: Feb 2005
Location: Bangalore, India
Posts: 2,798
This is what I understand.

You need to extract the questions to a new file.1

In file.2, you need the Q&A from nbu_faq.txt. Which means, file.2 will be a copy of nbu_faq.txt.

So your problem is only in extracting the questions.

Try this for extracting questions

sed -n -e '/^Q:.*/p' nbu_faq.txt > file.1

Vino
  #3 (permalink)  
Old 07-28-2005
nymus7 nymus7 is offline
Registered User
  
 

Join Date: Apr 2005
Posts: 47
Hi Vino

I believe that I did not well explain my question! it's true that it is a little bit confused
So, I've still a shell script that make HTML page and I want to include this new script to the other one.

The result would like so:

Quote:
1.Q:What's nbu?
2.Q:What's up?
3.Q:Why so?
...
Q:What's nbu?
A:abc

Q:What's up?
A:def

Q:Why so?:
A:ghi
That's means that my script should like this:
Code:
1.for each line contains "Q:xyz"
2.do
3.   call function with parameter the whole line "Q:xyz"
4.   cat the whole line corresponding to "Q:xyz" and the whole line "A:xyz" to file2.txt5. done
My problem is that for points:
1. select the whole line containing Q:
4. for the line in 1. print this line + the next one.
I've tried to do that with sed or awk without find a good solutions!
Thanks
ny
  #4 (permalink)  
Old 07-28-2005
Perderabo's Avatar
Perderabo Perderabo is offline Forum Staff  
Unix Daemon
  
 

Join Date: Aug 2001
Location: Ashburn, Virginia
Posts: 9,119
I still don't see how nbu_faq.txt and file2.txt would differ. It lokks like vino's solution is what you want.
  #5 (permalink)  
Old 07-29-2005
vino's Avatar
vino vino is offline Forum Staff  
Supporter (in vino veritas)
  
 

Join Date: Feb 2005
Location: Bangalore, India
Posts: 2,798
I think it is still the same.

How about this ?

sed -n -e '/^Q:.*/p' nbu_faq.txt > file1.txt
cp nbu_faq.txt file2.txt


Then look at file1.txt and file2.txt and let me know if that is what you wanted.

This is what I got

Code:
[~/temp]$ cat ny.txt
Q: What is nbu?
A: blablabla...
Q: What is blablabla...?
A: blablabla...
Q: Why ....?
A: blablabla...


[~/temp]$ sed -n -e '/^Q:.*/p' ny.txt
Q: What is nbu?
Q: What is blablabla...?
Q: Why ....?

[~/temp]$ cp ny.txt file.2
[~/temp]$ cat file.2
Q: What is nbu?
A: blablabla...
Q: What is blablabla...?
A: blablabla...
Q: Why ....?
A: blablabla...
Vino

Last edited by vino; 07-29-2005 at 01:52 AM..
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 11:42 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0