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


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Help make a program in cshell that searches and prints in a specific format
# 1  
Old 04-05-2009
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 Smilie
# 2  
Old 04-05-2009
Quote:
Originally Posted by h4wk
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  
Old 04-06-2009
cfajohnsom thank you it works!(but I am sure you already know that Smilie)
you're the best!Smilie
# 4  
Old 04-06-2009
how can I get the line "-------------------------"
to show between the results? thanks in advance!Smilie
# 5  
Old 04-06-2009
#!/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  
Old 04-06-2009
Quote:
Originally Posted by h4wk
how can I get the line "-------------------------"
to show between the results? thanks in advance!Smilie

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

# 7  
Old 04-06-2009

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
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Program to search for the files with specific format

Hi All, Your help on the below requirement is highly appreciated. I am building a program which should pick the file from a source directory and place them into target directory. While selecting the file in source it should look for some pattern in the file name. Ex: File Name 1:... (10 Replies)
Discussion started by: bharath561989
10 Replies

2. UNIX for Dummies Questions & Answers

Remote Desktop Session Prints Warning Message Everytime I Hit One Specific Key

Hello All, My question has to do with a Remote Desktop Session going from my Linux HOST to a Windows GUEST. Linux OS: OpenSuSE 11.4 i586 Windows OS: Windows Server 2003 For some reason after I have run rdekstop to connect to the Windows Guest, whenever I click on the Shift key I get the... (0 Replies)
Discussion started by: mrm5102
0 Replies

3. Shell Programming and Scripting

Need a program that read a file line by line and prints out lines 1, 2 & 3 after an empty line...

Hello, I need a program that read a file line by line and prints out lines 1, 2 & 3 after an empty line... An example of entries in the file would be: SRVXPAPI001 ERRO JUN24 07:28:34 1775 REASON= 0000, PROCID= #E506 #1065: TPCIPPR, INDEX= 003F ... (8 Replies)
Discussion started by: Ferocci
8 Replies

4. UNIX for Dummies Questions & Answers

Make directory with todays date format

Not sure why this is not working. Please advice: #!/bin/sh DIR=`date +"%m-%d-%y"` echo $DIR ] && mkdir $DIR (2 Replies)
Discussion started by: iaav
2 Replies

5. Shell Programming and Scripting

How to change a number on a specific line with cshell or shell?

Hello all, I need to change a number in a file by adding some residuals respectively To make it clear, I need to add 0.11 to the number between 24-28 (which is below the SECON) for all the lines starting with FRR1 or I need to add 0.13 to the number between 24-28 (which is below the... (9 Replies)
Discussion started by: miriammiriam
9 Replies

6. Shell Programming and Scripting

Assigning a specific format to a specific column in a text file using awk and printf

Hi, I have the following text file: 8 T1mapping_flip02 ok 128 108 30 1 665000-000008-000001.dcm 9 T1mapping_flip05 ok 128 108 30 1 665000-000009-000001.dcm 10 T1mapping_flip10 ok 128 108 30 1 665000-000010-000001.dcm 11 T1mapping_flip15 ok 128 108 30... (2 Replies)
Discussion started by: goodbenito
2 Replies

7. Shell Programming and Scripting

How to make shell script interact with program

Hello all! I have a C program that runs on a loop, prompting the user for input until it is exited. I want to create a shell script that can run this program and provide input. How can I do this? I have investigated 'expect' and piping to stdin, but haven't had any success. Any help is... (2 Replies)
Discussion started by: radish04
2 Replies

8. Programming

Help with a C program that acts like the make utility

Hi, I'm trying to create a C program that functions like the make utility in UNIX for personal use. My program would read in from a generic makefile and execute whatever is in that makefile but I'm not sure as to where I should start coding. I would appreciate any help. Thanks. (1 Reply)
Discussion started by: kratos.
1 Replies

9. Shell Programming and Scripting

Make program only run by root

Hi all, i hope i got this in the right place, what i am trying to do is make a program only run by root, ie for instance user fred is logged in and uses firefox, what id like to do is change that so that when fred wants to use firefox he will be asked to enter root password before he is allowed to... (14 Replies)
Discussion started by: dave123
14 Replies

10. Shell Programming and Scripting

How to make a cshell (csh) script interactive

Experts, I tried to make my cshell script interactive by asking the user for password and trying to read the input using the "read" command. I have been unsuccessful so far. Do you have any idea ? Thanks Jim (2 Replies)
Discussion started by: jimmynath
2 Replies
Login or Register to Ask a Question