![]() |
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.
|
|
google unix.com
|
|||||||
| Forums | Register | Forum Rules | Links | Albums | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
| 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 |
![]() |
|
|
LinkBack | Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
||||
|
match a pattern, print it and the next line
I have a file nbu_faq.txt (Question/answer) which looks like this
Quote:
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
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? |
|
||||
|
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:
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 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 |
|
|||||
|
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... Last edited by vino; 07-29-2005 at 01:52 AM.. |
![]() |
| Bookmarks |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|