A simple script fail


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting A simple script fail
# 1  
Old 12-15-2010
A simple script fail

Hi, I am a programming newbie, I have started learning C++ a couple of months ago. Unfortunately I don't have too much time in my hands to learn it properly. I need to write a simple script for a project I am doing but I am failing quite hard Smilie (the fact that I am learning C++ is completely unrelated btw, I am not writing the code with C Smilie).
The script is supposed to change two words, I thought I could easily do it using sed (btw, I use Ubuntu). I tried to use a .sed file and use sed -f option. One problem is that the file name should stay the same. I couldn't figure out how to do this Smilie The other huge problem is that there are numerous files in numerous subfolders I need to change and they all have the same .sh extension. I coudn't figure out how to achieve this either. I am sorry to ask such a simple question but I really don't have too much time in my hands to really figure out a solution Smilie Help is really appreciated, thanks a lot in advance people Smilie
# 2  
Old 12-15-2010
This sounds a lot like home work therefore I will get you started and you
can resarch the rest.

cat /tmp/xx
hello


This command will change the word hello to bye in every occurance
in file /tmp/xx

sed 's/hello/bye/' /tmp/xx

As for the sub-directories try the find command | xargs

find . -name "*.sh" -print | xargs <what ever you want to do?>

Good luck
This User Gave Thanks to BeefStu For This Post:
# 3  
Old 12-15-2010
If it was not for the scale of the task I'd wonder if it was homework.

We gather that Ubundu is the Operating System.
Which Shell are you using? echo $SHELL .
Have you read about the "-i" option to "sed" ? It should be available on your Operating System.

Please post what you have tried, what you expected to happen, and what actually happened.

If you issued this command from the top-level directory in the directory tree does it find all the relevant .sh files?
Code:
find . -type f - name '*.sh' -print

If not, what commands do list all the relevant .sh files?

Last edited by methyl; 12-15-2010 at 05:23 PM..
This User Gave Thanks to methyl For This Post:
# 4  
Old 12-17-2010
Well I am going to be honest, I am actually a chemist Smilie I am trying to get into computational chemistry and this is NOT homework. I am trying to use a docking software and this is to customize a trillion .sh files for the linux cluster (I am going to write the path of the command in the .sh script).
Thanks for the advice, I have read about the -i option, I am experimenting with it but the main problem is that there is no recursive option for sed. I have found a couple of examples with xargs they didn't work either (and several more with other commands), I will try a couple more things and post results soon.
Thanks again people Smilie

---------- Post updated at 12:47 PM ---------- Previous update was at 01:21 AM ----------

Ah I figured it out Smilie I now realize this was incredibly easy but I needed more knowledge, thanks a lot for your help Smilie
What I did was I used grep as you've said to find the proper files and piped it through xargs to use sed on them. I didn't know both grep and xargs but I've read the manuals and I played around with them. Thanks again Smilie The exact code I used;

grep -rl 'string' */ | xargs sed -i 's: oldstring:newstring:'

The reason I used : s in sed was that I was using a string with a lot of / s (the path). I used */ because I am going to make a small .sh file using this and I didn't want to edit the file to set the path every single time. I am a happy man Smilie

Also I am going to play around with the find command as methyl said since I think listing .sh files is a better option in my case. But this works for now Smilie

-Edit

I have played around with find (thanks again methyl) and I think this is the best code for me;

find ./*/ -type f -name '*.sh' -print | xargs sed -i 's: oldstring : newstring :'

Works perfectly ^^ I will eventually add a backup feature (-i.bak maybe?) into this but I am done for now.

Now I need to actually do the chemistry bit of this SmilieSmilie

Last edited by Akhlore; 12-17-2010 at 08:04 AM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Fail Parent Script if any of the child fails

I have requirement where I need to fail parent if any one of the child process fails. Here is the code snippet for i in 1 2 3 4 5 6 7 8 9 10 do child_script $i & done wait I need to fail my main script if any one of my child process fails (8 Replies)
Discussion started by: gvkumar25
8 Replies

2. Shell Programming and Scripting

Need help to write to log file whether the shell script call pass fail

I have the below script triggered daily at 330am in the morning, since last 7 days job not writing anything to database. below impala shell calling shell file which has sql , it is extracting data and loads to a flat file txt file. which is going wrong for last 1 week. need help, echo... (2 Replies)
Discussion started by: cplusplus1
2 Replies

3. Shell Programming and Scripting

sed commands success / fail from commandline vs ksh script

solaris 5.10 Generic_138888-03 sun4v sparc SUNW,Sun-Fire-T200 I need a sed command that tests true when presented with lines that contain either forward and backslash. input file: c:/myFile.txt c:\yourFile.txt It doesn't appear that sed (in my environment anyway) supports... (4 Replies)
Discussion started by: msutfin
4 Replies

4. Emergency UNIX and Linux Support

HP-UX: Help to Change network configuration from APA manual mode (2Gbps) to simple fail over (1Gbps)

Hello HP-UX experts, Server = rx8640 Node partition OS = HP-UX 11.23 arch = IA64 Network switch = Foundry 16 port switch (1Gbps) Existing configuration: Tough to explain as it is very messy (see below for the link of zip of network related fles). 2 x 2Gbps aggregates configured some time... (1 Reply)
Discussion started by: prvnrk
1 Replies

5. Shell Programming and Scripting

Help making simple perl or bash script to create a simple matrix

Hello all! This is my first post and I'm very new to programming. I would like help creating a simple perl or bash script that I will be using in my work as a junior bioinformatician. Essentially, I would like to take a tab-delimted or .csv text with 3 columns and write them to a "3D" matrix: ... (16 Replies)
Discussion started by: torchij
16 Replies

6. Shell Programming and Scripting

Banner causes scp to fail from script but not command line.

Hi, We have an interesting issue which is similar to the one in this thread, but that never provided a full answer. - Ohh apparently I can't post URLs till I have 5 posts, sorry. We have a simple script that copies files from one shelf to the other. Both shelves have an ssh banner defined. ... (3 Replies)
Discussion started by: RECrerar
3 Replies

7. Shell Programming and Scripting

tr command fail to work in script

Hi, I has the following command in the script. This command works fine if I execute on command prompt. If I run the script, this is not working as expected (deleting CR). tr -d "\015" < ${FilePath}/${FileName} > ${FilePath}/${File_Prefix}.csv I could not figure out whats... (6 Replies)
Discussion started by: kavuri
6 Replies

8. Shell Programming and Scripting

The same script with extra variables seems to fail

Hey everyone, I have a problem with a certain shellscript. I have a script like this: #!/bin/sh template=$1 for values in {60,150,240,330}\ {60,150,240,330}\ {60,150,240,330}\ {60,150,240,330}; do set $values X=$1; Y=$2; Z=$3; M=$4 mkdir "X$X-Y$Y-Z$Z-M$M" cp... (2 Replies)
Discussion started by: mario8eren
2 Replies

9. Shell Programming and Scripting

fail a unix script

i am basically DWH professional. I want to write a script such that whenver the file size is greather than 0 the script fails (6 Replies)
Discussion started by: er_zeeshan05
6 Replies

10. UNIX for Advanced & Expert Users

Fail to run init-script from crontab

I have written a "simple" shell-script (BASH) to monitor the logs of our Resin-applicationserver. Whenever Resin runs out of available heap-space, the script then tries to restart the Resin-server. My problem is that my "autopilot"-script fails when it's run from crontab. Here's what the... (5 Replies)
Discussion started by: indo1144
5 Replies
Login or Register to Ask a Question