Help make a program in cshell that searches and prints in a specific format

say I have a file named phones
in that file every line is like that
lastname^firstname^phone
how can I make a program in cshell that searches for a specific string
in phones and echos the result (if found) like that:
lastname1
firstname1
phone1
------------------
lastname2
firstname2
phone2
and so on....
I only know grep and it prints
lastname1^firstname1^phone1
lastname2^firstname2^phone2
...well thank you anyway :slight_smile:

C shell is not recommended for scripting:

Top Ten Reasons not to use the C shell
Csh problems
Csh Programming Considered Harmful

grep NAME | tr '^' '\n'

cfajohnsom thank you it works!(but I am sure you already know that :p)
you're the best!:b:

how can I get the line "-------------------------"
to show between the results? thanks in advance!:slight_smile:

#!/bin/bash
i=1
for a in `cat namelist`
do
echo "Loop$i:"
echo "+++++++"
grep $a testfile | tr '^' '\n'
echo "----------------------------"
i=`expr $i + 1`
done

"namelist" will contain all the names that needs to be searched/formatted.
"testfile" is the file name

awk -F: -v srch="$1" '
BEGIN { OFS = "\n"; ORS = "\n------------------\n" }
index($0,srch) { $1 = $1; print }' "$FILE"

Please put code inside

 tags.



#!/bin/bash
i=1
for a in `cat namelist`

[/quote]

[indent]
That may work if there are no spaces in the file; it will definitely fail if there are.

To read a file line by line:

while IFS= read -r line
do
  : whatever
done < FILENAME

That will fail if there's a space in the search term, and grep and tr are both unnecessary.

case $line in
   *"$a"*) set -f; printf "%s\n" ${line//^/$'\n'} "---------------" ;;
esac

There is no need to use an external command to do integer arithmetic in any POSIX shell:

i=$(( $i + 1 ))

I don't understand how someof the above work...
can't I doanything abou that code

#!/bin/csh
if($#argv == 0)then
echo "usage is find <something>"
exit(1)
else
echo "USERS"
echo "----------------------------"
echo "Search for: $* "
foreach k ($*)
echo "----------------------------"
grep -i $k phones | tr '^' '\n'
if($status != 0)then 
echo not found
else
echo "----------------------------"
endif
end
exit(0)
endif

that I completely understand how it works,
to make the "--------------------" line to appear between results?
I also tried to use awk before I posted my question here,but couldn't get it right(and still can't:confused:)

In a real scripting shell (i.e., not csh), that would be:

if [ $# -eq 0 ]
then
  echo "usage is find <something>"
  exit 1
fi

echo "USERS"
echo "----------------------------"
echo "Search for: $* "

for k in "$@"
do
  echo "----------------------------"
  result=$( grep -i "$k" phones | tr '^' '\n' )
  if [ -z "$result" ]
  then 
    echo not found
  else
    printf "%s\n" "$result" "----------------------------"
  fi
done

Do yourself a favour and stop scripting in csh.