Combining lines between two specific lines


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Combining lines between two specific lines
# 1  
Old 12-26-2011
Combining lines between two specific lines

Hi,
I have a requirement like following:

I have input file like:

Question: 1 ----Multiple choice---
What is
your
favourite colour?
Options: a) red b) blue c) none of these
Question: 2 ---Multiple choice-----
In which month
did you join
your first job?
Options: a) Jan b) Feb c) none of these
----
----
and so on....

I want to capture only the questions in the following way:

1 What is your favourite colour?
2 In which month did you join your first job?


Can anyone please help me? Thanks!
# 2  
Old 12-26-2011
Homework ?? What you have tried ..
# 3  
Old 12-26-2011
Deleted. Lets wait for the answer to Jayan's question!

--ahamed
This User Gave Thanks to ahamed101 For This Post:
# 4  
Old 12-27-2011
Hi,

This is the script I wrote. I'm passing a rtf file as commandline parameter, after that using awk i am praparing another file which (newfile) look like above (which i mentioned in my first post.
Code:
#!/bin/ksh
rtf_file=$1
awk '/Question:/,/Options:/{print}' $rtf_file | sed '/^$/d' > ./newfile
print=xx
n=1
while read line; do
        typeset -L9 fch=$line
        typeset -L8 fch2=$line
        if [[ $fch = 'Question:' ]]; then
                print=no
  q_no=`echo $line | tr -d "[a-z][A-Z][:]"`
                ((n+=1))
       elif [[ $fch2 = 'Options:' ]]; then
                print=no
       else
               print=yes
        fi
        if [[ $print = 'yes' ]]; then
            echo "$q_no $line"
            print=yes
        fi
done < ./newfile


The above script is giving output like below:

1 What is
1 your
1 favourite colour?
2 In which month
2 did you join
2 your first job?

But I want output like:
1 What is your favourite colour?
2 In which month did you join your first job?

PLease let me knwo if I am not clear. Thanks!

Moderator's Comments:
Mod Comment How to use code tags when posting data and code samples.

Last edited by Franklin52; 12-27-2011 at 08:33 AM.. Reason: Please use code tags for data and code samples, thank you
# 5  
Old 12-27-2011
Good try .. Try with the below ..
Code:
i=1
while [ $i -le $(nawk -F'[:-]' '/Question:/{print $2|"tail -1"}' infile) ]
do
        sed -n "/Question: $i/,/Options/p" infile | sed '1d;$d' | xargs
        i=$((i+1))
done

This User Gave Thanks to jayan_jay For This Post:
# 6  
Old 12-27-2011
Try this:
Code:
perl -ane '
chomp;if(/^Question/../^Option/)
{(/^Que/)&&(print "$F[1] ");
(/^Question/||/^Options/) && next;
(/\?$/)?print "$_\n":print "$_ "}' inputfile


Last edited by balajesuri; 12-27-2011 at 02:37 AM..
This User Gave Thanks to balajesuri For This Post:
# 7  
Old 12-27-2011
Another simple shell script

Code:
awk '{
if($1 != "") {
if($1 == "Question:") {num=$2}
else if ($1 == "Options:") {print ""}
else {printf "%s %s", num, $0; num=""}
}
}' infile

This User Gave Thanks to siva shankar For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

How to print lines from a files with specific start and end patterns and pick only the last lines?

Hi, I need to print lines which are matching with start pattern "SELECT" and END PATTERN ";" and only select the last "select" statement including the ";" . I have attached sample input file and the desired input should be as: INPUT FORMAT: SELECT ABCD, DEFGH, DFGHJ, JKLMN, AXCVB,... (5 Replies)
Discussion started by: nani2019
5 Replies

2. Shell Programming and Scripting

How to find files containing two specific lines and delate those lines?

Hi I need to find files in a specified folder where are two specified lines of text and delate that lines. It looks like this" 35. ?>NL 36. <iframe>.......</iframe>NLThe problem is that "?>" is in the other lines and id should not be removed if the next line is not like "<iframe>....." So... (4 Replies)
Discussion started by: androwida
4 Replies

3. Shell Programming and Scripting

ksh sed - Extract specific lines with mulitple occurance of interesting lines

Data file example I look for primary and * to isolate the interesting slot number. slot=`sed '/^primary$/,/\*/!d' filename | tail -1 | sed s'/*//' | awk '{print $1" "$2}'` Now I want to get the Touch line for only the associate slot number, in this case, because the asterisk... (2 Replies)
Discussion started by: popeye
2 Replies

4. Shell Programming and Scripting

Summing over specific lines and replacing the lines with the sum

Hi friends, This is sed & awk type question. It is slightly different from my previous question. I have a text file which has numbers spread all over the file. I want to sum the series of numbers (but no more than 10 numbers in series) whenever i find it and produce an output file with the... (4 Replies)
Discussion started by: kaaliakahn
4 Replies

5. UNIX for Dummies Questions & Answers

Extract lines with specific words with addition 2 lines before and after

Dear all, Greetings. I would like to ask for your help to extract lines with specific words in addition 2 lines before and after these lines by using awk or sed. For example, the input file is: 1 ak1 abc1.0 1 ak2 abc1.0 1 ak3 abc1.0 1 ak4 abc1.0 1 ak5 abc1.1 1 ak6 abc1.1 1 ak7... (7 Replies)
Discussion started by: Amanda Low
7 Replies

6. Shell Programming and Scripting

Summing over specific lines and replacing the lines with the sum using sed, awk

Hi friends, This is sed & awk type question. I have a text file which has numbers spread all over the file. I want to sum the series of numbers whenever i find it and produce an output file with the sum. For example ###start of input text file #### abc def ghi 1 2 3 4 kjld random... (3 Replies)
Discussion started by: kaaliakahn
3 Replies

7. Shell Programming and Scripting

Reading two lines in a while loop and combining the lines

Dear all, I have a file like this: imput scaffold_0 1 scaffold_0 10000 scaffold_0 20000 scaffold_0 25000 scaffold_1 1 scaffold_1 10000 scaffold_1 20000 scaffold_1 23283 and I want the output like this: scaffold_0 1 scaffold_0 10000 scaffold_0 10000 scaffold_0 20000... (6 Replies)
Discussion started by: valente
6 Replies

8. Shell Programming and Scripting

Printing all lines before a specific string and a custom message 2 lines after

Hello all, I need to print all the lines before a specific string and print a custom message 2 lines after that. So far I have managed to print everything up the string, inclusively, but I can't figure out how to print the 2 lines after that and the custom message. My code thus far is:... (4 Replies)
Discussion started by: SEinT
4 Replies

9. Shell Programming and Scripting

Concatenate lines between lines starting with a specific pattern

Hi, I have a file such as: --- >contig00001 length=35524 numreads=2944 gACGCCGCGCGCCGCGGCCAGGGCTGGCCCA CAGGCCGCGCGGCGTCGGCTGGCTGAG >contig00002 length=4242 numreads=43423 ATGCCGAAGGTCCGCCTGGGGCTGG CGCCGGGAGCATGTAGCG --- I would like to concatenate the lines not starting with ">"... (9 Replies)
Discussion started by: s052866
9 Replies

10. Shell Programming and Scripting

need help appending lines/combining lines within a file...

Is there a way to combine two lines onto a single line...append the following line onto the previous line? I have the following file that contains some blank lines and some lines I would like to append to the previous line... current file: checking dsk c19t2d6 checking dsk c19t2d7 ... (2 Replies)
Discussion started by: mr_manny
2 Replies
Login or Register to Ask a Question