Need a help in shell script

Hi All

I am new to shell script .. can anyone help me on this

i have a file c1.txt

cool:niceone
revoke
similar
jeep
34343
34343

cool:notone
aefdfdd
sdsdsds
sdsdsds
sdsdsd
sdsds
sdsds

i have a parameter file b.txt
niceone

i need a shell which reads the parameter file and search the string in c1.txt once the string is found it should print those lines to an output file .

but a trick is here .. the string should print lines which start from "cool " and should print those lines but the script should stop before another cool is found

I suggest a "while read" loop with a variable to say whether how many cools had been encounted.

I also assume that an awk script does not count as a shell script.

can you give a sample code to work on this

Thanks a lot

BETWEEN=false
while read N
do
     case "$N" in
     cool:* )
            if $BETWEEN
            then
                   BETWEEN=false
                   ;;
            else
                   BETWEEN=true
                   ;;
            fi
            ;;
      * )
            if $BETWEEN
            then
                   echo "$N"
            fi
            ;;
      esac
done <file1.txt

Sorry for not being so clear in my information

The output which i need is to print all the lines

cool:niceone
revoke
similar
jeep
34343
34343

which should read the lines from the parameter lines and print it in an output file

Thanks .. awaiting for a reply for this one

Try this (assume that there is an empty line between each 'cool:' section)

awk -v RS='' '
   NR==1 { string = "cool:" $1 }
   NR!=FNR && $1==string
' b.txt c1.txt

Jean-Pierre.

With GNU awk:

awk '$0~"v"&&$0=RT$0{print;exit}' RS="cool:" v="$(<b.txt)" c1.txt

Or:

awk 'NR==1{p="cool:"$0};$0~p{x=1;print;next};/cool:/{exit};x' b.txt c1.txt

Use nawk or /usr/xpg4/bin/awk on Solaris.

When used the code i am getting the following error .. couldn't debug it
awk: syntax error near line 1
awk: bailing out near line 1

Yes got the issue .. please ignore my previous error message

hi,

input:

a:
cool:niceone
revoke
similar
jeep
34343
34343


cool:notone
aefdfdd
sdsdsds
sdsdsds
sdsdsd
sdsds
sdsds
b:
niceone

output:

cool:niceone
revoke
similar
jeep
34343
34343

code:

par=`cat b`
nawk -v var=$par 'BEGIN{n=1
}
{
if (index($0,var)!=0)
{
n=0
}
if (index($0,"cool")!=0 && index($0,var)==0)
n=1
if (n==0)
print $0
}' a

will this work for if i keep on adding the param file ..

the script works for only one line . and if i add more lines in b.txt it is unable to generate the file

Any one can help me on this

b.txt

niceone
notone

it would be great if you would explain this code .. since if i am trying to add more lines in b.txt this script is not functioning well ,, Please give a suggestion on this .. Thanks

A possible solution:

awk '
   NR==FNR {               # First file (keys)
      keys["cool:" $0]++;  # Memorize keys for second file
      next;                # Next record
   }
   /^cool:/ {              # Record start with "cool:"
      display = ($0 in keys ? 1 : 0);  # If memorized key display block (1)
   }
   display                 # Display line if block must be displayed
    ' b a

Jean-Pierre.

Thanks all for the replies .. i tried to add lines in b.txt paramater file and it didnot generate any output .. any clues ??

My testing result

$ cat a
cool:niceone
revoke
similar
jeep
34343
34343


cool:notone
aefdfdd
sdsdsds
sdsdsds
sdsdsd
sdsds

cool:unknown
xxx
yyyyy

cool:city
123
456
$ cat b
city
niceone
$ cat raghav.sh
awk '
   NR==FNR {
      keys["cool:" $0]++;
      next;
   }
   /^cool:/ {
      display = ($0 in keys ? 1 : 0);
   }
   display
    ' b a
$ raghav.sh
cool:niceone
revoke
similar
jeep
34343
34343


cool:city
123
456
$

Trailing spaces in records from files a or b may cause problems.
Try the following version:

awk '
   { gsub(/[[:space:]]*/, "") } # Removes trailing spaces from record
   NR==FNR {
      keys["cool:" $0]++;
      next;
   }
   /^cool:/ {
      display = ($0 in keys ? 1 : 0);
   }
   display
    ' b a

Jean-Pierre.

Thanks a lot .. it worked .. but when i tried to change the search word to someother word and the lines are not the same which i had earlier

there are no errors but also it didn't produce any output

Just need to search a string(word) which would read from a param file (b)
and prints all the lines in an output from a and stops printing the line till another search string is wound

the above script worked fine . but when i try to change the a file and b file of my criteria it didn't work

any help on this .. Thanks once again !!! waiting for a reply

Show us your input data files.

Jean-Pierre.

This is my input file a

/* -------------unixscripts ----------------- */

insert: unixscripts fixed_job: c
command: /bin/*.sh
machine: co234
owner: sysadm
permission: 775,675151,12121

/* ----------------- unix----------------- */

insert: unix job_type: c
command: /bin/*.sh
machine: co234
owner: sysadm
permission: 775,675151,12121

/* ----------------- ksh_input----------------- */

insert: ksh_input job_type: c
command: /bin/*.sh
machine: co234
owner: sysadm
permission: 775,675151,12121

param file : (b)
ksh_input
unix

Output file :

/* ----------------- unix----------------- */

insert: unix job_type: c
command: /bin/*.sh
machine: co234
owner: sysadm
permission: 775,675151,12121

/* ----------------- ksh_input----------------- */

insert: ksh_input job_type: c
command: /bin/*.sh
machine: co234
owner: sysadm
permission: 775,675151,12121

May i know why if we change the search string ti insert . the script is not working .. any clues ???

Thankyou!!