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.
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..
#!/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