Sponsored Content
Homework and Emergencies Homework & Coursework Questions Problem with Shell Scripts deleting text in files. Post 302686431 by Johnny2518 on Tuesday 14th of August 2012 01:19:45 PM
Old 08-14-2012
Problem with Shell Scripts deleting text in files.

Me and a friend are working on a project, and We have to create a script that can go into a file, and replace all occurances of a certain expression/word/letter with another using Sed. It is designed to go through multiple tests replacing all these occurances, and we don't know what they will be so we have to anticipate anything. We are having trouble on a certain test where we need to replace 'l*' with 'L' in different files using a loop. The code that i have is

Code:
#!/bin/sh
 p1="$1" #first parameter

shift

 p2="$1"

shift

for file in "$@" #for any file in the directory

do
   # A="$1"

    #echo $A
    #B="$2"
    echo "$p1" | sed -e 's/\([*.[^$]\)/\\\1/g' > temporary #treat all special characters as plain text
A="`cat 'temporary'`"
rm temporary
echo "$p1"
echo "$file"
sed "s/$p1/$p2/g" "$file" > myFile.txt.updated #replace occurances 
mv myFile.txt.updated "$file"
cat "$file"
done

I have tried testing this on practice files that contain different words and also 'l*' But whenever i test it, it deletes all the text in the file. Can someone help me with this,

Moderator's Comments:
Mod Comment edit by bakunin: For a number of reasons it is imperative for students posting here to use the template provided. This is not the case here.

I am going to close this thread, please create another thread and fill out the template correctly, i will merge it into this thread here and reopen it.

Last edited by bakunin; 08-15-2012 at 03:26 AM.. Reason: Code tags, please...
 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Converting Text File into XML using Unix Shell Scripts

Hi everyone, If someone out there could help me out with this problem. I would really appreciate it. I am trying to convert a file into xml format using Unix shell scripts. The file has fields with each field having a certain number of bytes, but the fields are not delimited by anything... (10 Replies)
Discussion started by: Laud12345
10 Replies

2. Shell Programming and Scripting

Shell scripts problem...

Hi, sprt.sh #!/bin/ksh ############################################################################### # ----------------------------------------------------------------------------- # # ------------------------------------------------------------------------------... (1 Reply)
Discussion started by: parola
1 Replies

3. UNIX for Dummies Questions & Answers

Parsing text from one line with shell scripts

Hi Gurus! I wonder if anyone can help me, I'm sure you guys can. I have a text file which contains a lot of data on the one line as follows: $ What I need to do is pull all of those id values out (eg 2549425) and write them to a list in a text file. Any help would be greatly... (3 Replies)
Discussion started by: th3g0bl1n
3 Replies

4. Shell Programming and Scripting

deleting lines from multiple text files

I have a directory full of text data files. Unfortunately I need to get rid of the 7th and 8th line from them all so that I can input them into a GIS application. I've used an awk script to do one at a time but due to the sheer number of files I need some kind of loop mechanism to automate... (3 Replies)
Discussion started by: vrms
3 Replies

5. Shell Programming and Scripting

Deleting files that don't contain particular text strings / more than one instance of a string

Hi all, I have a directory containing many subdirectories each named like KOG#### where # represents any digit 0-9. There are several files in each KOG#### folder but the one I care about is named like KOG####_final.fasta. I am trying to write a script to copy all of the KOG####_final.fasta... (3 Replies)
Discussion started by: kmkocot
3 Replies

6. Shell Programming and Scripting

Problem in deleting files

Hi Guys, In one of my folders i have 10000 log files starting with sqlfile..... Iam trying to delete the log files alone from the folder using for loop but i am getting an error ksh: /usr/bin/ls: 0403-027 The parameter list is too long. For loop i used for i in `ls sqlfile*`... (4 Replies)
Discussion started by: mac4rfree
4 Replies

7. Shell Programming and Scripting

Shell performance problem with locating scripts

I am currently trying out MKS Toolkit C Shell, and I've no problems with it until I try add directories to PATH that are located on a network drive. When I do that, the shell performance slows down significantly and no longer runs fast. In fact, it takes seconds for something that should take... (1 Reply)
Discussion started by: vas28r13
1 Replies

8. UNIX for Dummies Questions & Answers

problem deleting multiple files using rm in UNIX

I'm baffled..... the system I work on creates files every Mon-Friday I'm trying to delete all files older than 30 days old from a Unix prompt, the command I'm using is: find /directory/ -mtime +30 -exec rm {} \; however it returns /directory/filename: 644 mode ? (y/n) for every file! ... (1 Reply)
Discussion started by: bquattrone
1 Replies

9. Shell Programming and Scripting

Some scripts for text files needed

Hi, does anyone have some of those scripts or maybe have idea how to make them? ------------------------------------ 1) block of text multiplication n times: e.g. something1 something2 something3 something4 something5 something6 result: something1 something2 something3 something4... (2 Replies)
Discussion started by: spuzh
2 Replies

10. Shell Programming and Scripting

Shell : deleting only first 2 files in a directory

I have 4 files in a directory and want to delete only first 2 files only.. $ ls -ltr total 640 -rw-r--r-- 1 user other 148779 Oct 12 10:50 file1.xls -rw-r--r-- 1 user other 148779 Oct 12 10:50 file2.xls -rw-r--r-- 1 user other 148779 Oct 12 10:50 file3.xls... (4 Replies)
Discussion started by: giridhar276
4 Replies
CVSMGDIFF(1)							  Utility Scripts						      CVSMGDIFF(1)

NAME
cvsmgdiff - uses mgdiff to display differences between any two cvs revisions. SYNOPSIS
cvsmgdiff [-v|-h|-g gui|-r rev1 [-r rev2]] file ... DESCRIPTION
This manual page documents briefly the cvsmgdiff program. This manual page was written for the Debian GNU/Linux distribution (but may be used by others), because the original program does not have a manual page. This script lets you recursively diff the CVS directories you have checked out. To use, just pass in an optional revision levels and an optional file directory name. This script then will show you the differences you're interested in. OPTIONS
-v Print version information successfully -h Print help information -g gui Use the program gui as the user interface (default: /usr/bin/mgdiff) -r revision Specify the CVS revision to view. If just one -r option is given, view differences between that revision and the current file in the CVS working directory. If two -r options are given, compare those two revisions with each other. AUTHOR
cvsmgdiff appears to have been written by Paul Serice. This manual page was written by Ian Zimmerman <itz@speakeasy.org> for the Debian GNU/Linux project, but may be used by others. It was written with the assistance of pod2man(1). 3rd Berkeley Distribution Utility Scripts CVSMGDIFF(1)
All times are GMT -4. The time now is 06:24 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy