Script to extract certain lines

Hi
I have a text file with the following information:

# List 1 (first header)
test 1
test 2
test 3
...
 
# Trials (second header)
round 1
run 5
...
 

and so on

I want to create a script, which based on some criterias with return only the list of lines between the header. I tried "sed", but could not do it. Could you help please.

Can you please post required output ?

some criteria is very vague.

Hi
Let's assume that this is the file tha I am handling.

 
# Start
# List  (first header)
test 1
Lab 2
test 3
...
 
# Trials (second header)
round 1
run 5
...
# Users
John
Sally
Denise
# End

In this case, I have 3 to extract: List, Trials and Uses. Output for list should be:

 
test 1
Lab 2
test 3
...

Output for Users should be

 
John
Sally
Denise

See if this works for you:

sed -n '1,/^# Trials/d;/^#/q;p'  inp_file

Thank you. That works.

The above sed command need to change manually to get each session. If you have thousand sessions, how can you do that?

This awk command will generate txt files for each session directly.

awk '/^# /{file=$2;next}{print > file ".txt"}' infile

$ cat list.txt
test 1
Lab 2
test 3
...

$ cat Users.txt
John
Sally
Denise

$ cat Trials.txt
round 1
run 5
...

Both sed and awk work fine when I execute from command line against input file, but not when I run with variable from script. I tried to get the name of every category and the list

 
# list
test 1
Lab 2
test 3
...
# Users
John
Sally
Denise
# Trials
round 1
run 5
...

I wrote the follwoing script

 
NCAT=`grep :: INPUT | wc -l`
count=1
while [ $count -le $NCAT ]
do
CAT=`grep :: INPUT | sed -n ${count}'p' | awk '{ print $2 }'`
sed -e '/^$/d' -n -e '/^:: "$CAT"/,/::/p' INPUT | sed '/^::/d' > OUTPUT$count
count=`expr $count + 1`
done

---------- Post updated 05-30-11 at 07:18 AM ---------- Previous update was 05-29-11 at 01:45 PM ----------

I also tried to use AWK in the script but it did not work. Is there a difference between using these commands in script or command line?

# cat infile
# Start
# List  (first header)
test 1
Lab 2
test 3
...
# Trials (second header)
round 1
run 5
...
# Users
John
Sally
Denise
# End
# ./justdoit infile
input your value..= List
Dou you want to save results to a file [y/n]? n
 
**********  Results  **********
test 1
Lab 2
test 3
...
 
**********  End Of Results  **********
# ./justdoit infile
input your value..= Users
Dou you want to save results to a file [y/n]? y
result file is 'Users.txt' in cur dir..

regards
ygemici

Did I miss something here? I do not see the "justdoit" code?!

oh sorry :confused: due to sleeplessness :o

-- justdoit --

#!/bin/bash
[ ! -f $1 ] && echo "you must specify a current file e.g --> '$0 inputfile' " && exit 1
read -p "input your value..= " val
[[ ! $(grep -w "$val" "$1") ]] && echo -e "\nyour input does not appear in input file!!" && exit 1
x="sed -rn '/# '$val'/{n;:a;/# /!{N;ba};s/(.*)\n.*/\1/p}' $1"
read -p "Dou you want to save results to a file [y/n]? " c
case $c in
  y ) eval $x>${val}.txt ; echo "result file is '${val}.txt' in cur dir.." ;;
  n ) echo -e "\n**********  Results  **********\n$(eval $x)\n**********  End Of Results  **********" ;;
  * ) exit 1 ;;
esac

regards
ygemici