The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Checking mem usage at specific times in a program pl4u UNIX for Advanced & Expert Users 2 04-05-2009 05:26 PM
manipulate data with specific format tonet Shell Programming and Scripting 5 04-25-2008 11:24 AM
Make program only run by root dave123 Shell Programming and Scripting 14 03-13-2008 10:59 AM
extract specific data from xml format file. 60doses Shell Programming and Scripting 7 01-16-2008 03:26 AM
How to make a cshell (csh) script interactive jimmynath Shell Programming and Scripting 2 09-07-2005 01:28 PM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 04-05-2009
h4wk h4wk is offline
Registered User
  
 

Join Date: Apr 2009
Posts: 4
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
  #2 (permalink)  
Old 04-05-2009
cfajohnson's Avatar
cfajohnson cfajohnson is offline Forum Advisor  
Shell programmer, author
  
 

Join Date: Mar 2007
Location: Toronto, Canada
Posts: 2,361
Quote:
Originally Posted by h4wk View Post
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

C shell is not recommended for scripting:

Top Ten Reasons not to use the C shell
Csh problems
Csh Programming Considered Harmful
Quote:
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

Code:
grep NAME | tr '^' '\n'
  #3 (permalink)  
Old 04-06-2009
h4wk h4wk is offline
Registered User
  
 

Join Date: Apr 2009
Posts: 4
cfajohnsom thank you it works!(but I am sure you already know that )
you're the best!
  #4 (permalink)  
Old 04-06-2009
h4wk h4wk is offline
Registered User
  
 

Join Date: Apr 2009
Posts: 4
how can I get the line "-------------------------"
to show between the results? thanks in advance!
  #5 (permalink)  
Old 04-06-2009
reddybs reddybs is offline
Registered User
  
 

Join Date: Jan 2009
Location: Sriharikota-AP-India
Posts: 53
#!/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
  #6 (permalink)  
Old 04-06-2009
cfajohnson's Avatar
cfajohnson cfajohnson is offline Forum Advisor  
Shell programmer, author
  
 

Join Date: Mar 2007
Location: Toronto, Canada
Posts: 2,361

Please put code inside [code] tags.
Quote:
Code:
#!/bin/bash
i=1
for a in `cat namelist`

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:

Code:
while IFS= read -r line
do
  : whatever
done < FILENAME
Quote:
Code:
do
        echo "Loop$i:"
        echo "+++++++"
        grep $a testfile | tr '^' '\n'

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

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

        echo "----------------------------"
        i=`expr $i + 1`

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

Code:
i=$(( $i + 1 ))
Quote:
Code:
done
"namelist" will contain all the names that needs to be searched/formatted.
"testfile" is the file name
  #7 (permalink)  
Old 04-06-2009
cfajohnson's Avatar
cfajohnson cfajohnson is offline Forum Advisor  
Shell programmer, author
  
 

Join Date: Mar 2007
Location: Toronto, Canada
Posts: 2,361
Quote:
Originally Posted by h4wk View Post
how can I get the line "-------------------------"
to show between the results? thanks in advance!

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

Bookmarks

Tags
cshell, echo, search

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 05:47 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0