Script for writing to files with sed


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Script for writing to files with sed
# 1  
Old 04-13-2005
Script for writing to files with sed

I have a problem with syntax methinks, when it comes to sed.

This is my script for a user to type sed commands:-
Code:
yes=y

function writefunction
{
$tool $command $newfilename $filename
}

echo
echo
echo "1. Type the name of the tool you are using i.e sed."
         read tool
echo
echo "2. Type any criteria you are searching for. And remember:-"
echo "   You can do this in conjunction with command options like -n or p to affect the search and view output."
echo "   You can also search characters or whole names by enclosing them in / /, but do not forget to tell the tool what to"
echo "   do with those characters - Use the 'w' write command!"
         read command
echo
echo "3. Type a name for a new file where the output is to be redirected."
         read newfilename
echo
echo "4. Type the name of the original file you searched i.e Myfile"
         read filename
#------------------------------------------------------------------------
#Call the function 'writefunction'
echo
echo "Your results are:-"
echo "--------------------------------------"
             writefunction
echo "--------------------------------------"
echo
echo "Do you want to see the contents of the file you just created?"
echo "Type 'y' for Yes. Press any key for No."
      read answer
    if [ "$answer" = "$yes" ]
     then
      clear
 cat $newfilename
    else
echo "New file $newfilename will not be displayed at this time."
    fi

echo "Where do you want to go?"
echo
echo "------------------"
echo "1. Previous Menu"
echo "2. Main Menu"
echo "3. QUIT"
echo
echo "Please select (1,2 or 3)................."
read menunumber
case $menunumber in
     1)./page2;;
     2)./page1;;
     3)echo "You have Quit the program. To begin again type:- ./page1"
esac
echo
echo

Basically, a user is prompted to type a sed command that will interrogate a file they specify and based on criteria they specify. That information is then written to a new file that they give a name to. All of their responses are stored in variables, that are located in a function to be executed later on in the program.

I think the problem lies in my sed syntax.

To test it I type this in the stages as set out by the program:-

Code:
1. sed (the command)
2. /Anderson/w (the criteria)
3. testfile (the file to be written to)
4. filetest(the file to be interrogated)

I have also tried:-
Code:
1. '/Anderson/w
2. testfile'

I get:-
Code:
sed: Couldn't open file : No such file or directory

Any syntax ideas?
Or any other advice as to what I am doing wrong?


Cheers,


trufla
# 2  
Old 04-13-2005
One obvious one I picked up on was:

Quote:
function writefunction
{
$tool $command $newfilename $filename
}
That should be

Code:
function writefunction
{
$tool $command $filename > $newfilename 
}

# 3  
Old 04-14-2005
aaah, I hadn't thought of that! Will give it a go!


Cheers!


trufla
# 4  
Old 04-14-2005
That works!

I was getting the user to type '>' so it was stored in the variable.

the script didn't like this at all!



Thanks for that!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Writing a script to print the number of lines in multiple files

Hi I have 1000 files labelled data1.txt through data1000.txt. I want to write a script that prints out the number of lines in each txt file and outputs it in the following format: Column 1: number of data file (1 through 1000) Column 2: number of lines in the text file Thanks! (2 Replies)
Discussion started by: evelibertine
2 Replies

2. Shell Programming and Scripting

help with writing a awk/sed script

Hi, I thought I am getting pretty good with sed and awk, but now I dont have a way out of this question. I have a table 0.5 16 1.3 14 0.25 15 0.85 16 I want to make a column 3 which contains values that are (corresponding $2 value/sum of all $2). Please help me out here. Thanks. (6 Replies)
Discussion started by: jamie_123
6 Replies

3. Shell Programming and Scripting

Writing a Perl Script that processes multiple files

I want to write a Perl script that manipulates multiple files. In the directory, I have files 250.*chr$.ped where * is from 1 to 1000 and $ is from 1-22 for a total of 22 x 10,000 = 22,000 files. I want to write a script that only manipulates files 250.1chr*.ped where * is from 1 to 22.... (10 Replies)
Discussion started by: evelibertine
10 Replies

4. UNIX for Dummies Questions & Answers

Writing awk script to read csv files and split them

Hi Here is my script that calls my awk script #!/bin/bash set -x dir="/var/local/dsx/csv" testfile="$testfile" while getopts " f: " option do case $option in f ) testfile="$OPTARG";; esac; done ./scriptFile --testfile=$testfile >> $dir/$testfile.csv It calls my awk... (1 Reply)
Discussion started by: ladyAnne
1 Replies

5. Shell Programming and Scripting

Need help in writing script for finding files in the unix machine?

I would like to find whether a file exists in the UNIX machine. That i can check using if ;then echo "exists" echo " `cat $file` " else echo "invalid file" fi. and i can find out using : find / -name "filename" . But it i have wanted to search in all directories. How to get... (3 Replies)
Discussion started by: rparsa001
3 Replies

6. Shell Programming and Scripting

writing script in UNIX for copying files in two server

can anyone help me in writing script in UNIX for copying files in two server from the third server after checking the files in the third server and if there is anything new in the third server automatically it should be added to the rest of the two servers and if same file is existing in the two... (4 Replies)
Discussion started by: REKHA09
4 Replies

7. Shell Programming and Scripting

Problems writing bash script to unzip files

I'm getting the following errors when I try to write a script to unzip some zip files. When I use the free trial copy of the commerical winzip program, however, they work fine. When I use -l or -t on unzip it indicates no errors. When I use the -o switch interactively from the bash command line it... (1 Reply)
Discussion started by: siegfried
1 Replies

8. UNIX for Dummies Questions & Answers

new to writing script files

thanks guys i managed to answer (0 Replies)
Discussion started by: bebop1111116
0 Replies

9. Shell Programming and Scripting

Writing a shell script to untar files

I am new to shell scripting and would appreciate any help I can get. I need to write a Unix shell script that I will run whenever I have a tar file to uncompress(Korn shell). Please put in mind that I have different environements that I will run it on. Thanks in advance ;) (4 Replies)
Discussion started by: nkem22
4 Replies

10. Shell Programming and Scripting

help writing script to read files names

Hi there, I am trying to do somehting similar, but on a wider scale. I am trying to write a script that would open the home directory, open the first (of 650) user's folder open the ?mail directory, which every user has Then I need the script to read each of the files and folder names with... (2 Replies)
Discussion started by: technett
2 Replies
Login or Register to Ask a Question