Problems on cat and substring replacement, shell


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Problems on cat and substring replacement, shell
# 1  
Old 07-30-2010
Problems on cat and substring replacement, shell

Hi

I'm new in the Shell world, and I've been trying the whole day to get my script to work.

I'm under Ubuntu 10.04.

Let me explain:
I want to list all the files matching some regex in a directory, and then make some some string replacement on the name of the files, and then a cat on the files.

Here's my code :
Code:
#!/usr/bin/sh
rep='/home/blablabla/'
for filename in `ls ${rep} | grep -i [0-9]-0.txt$`
do 
    if  [ ! -d ${rep}${filename} ]
    then
        filename1=`echo "${filename}" | sed -e "s/-0/-1/g"`
		filename2=`echo "${filename}" | sed -e "s/-0//g"`
		echo "${filename1} ${filename2}"
		cat ${rep}${filename} "${rep}${filename1}" > "${rep}${filename2}"
    fi
done

I would like to know if it's the best way to do that, because it seems to work but i'm not sure if that's really reliable.

Thanks in advance

Regards

Last edited by albh; 07-30-2010 at 10:05 AM.. Reason: solve some issues
# 2  
Old 07-30-2010
Hi,

variable substitution via ${var//exp1/exp2} is a feature of bash
or zsh, not the sh. Consequently your code will not work with
!#/usr/bin/sh. Change it to bash.

HTH Chris
# 3  
Old 07-30-2010
Thanks!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Script Shell Extract substring

Hi all, Please, i'd like to extract string just before '.fr'. Here is some lines of my file: g-82.text.text1.fr.worker1 g-xx.yyyyyy.zzzz.fr.worker2 i'd like to extract this text: g-82.text.text1 g-xx.yyyyyy.zzzz Please, which command i have to use in my script shell ? ... (16 Replies)
Discussion started by: chercheur111
16 Replies

2. UNIX for Dummies Questions & Answers

Grep or cat The Whole Directory PROBLEMS :(

Hi Guys This is my first post so I am not sure how things go here. I'm sorry if I'm breaking the rule or something. Feel free to correct me about that :) So as I was saying... I'd been trying to grep this folder containing 900,000 txt files but seems no luck. I get either "No such file... (6 Replies)
Discussion started by: Nexeu
6 Replies

3. Shell Programming and Scripting

XML id replacement with shell

Hi.. I have a problem with replacing of id-0f5435080b with some name daily, problem here is whenever I generate xml file it generates unique id for instance say for example today id-0f5435080b and tomorrow it may be id-0f68643508so basically I just want to replace this id with some name say... (5 Replies)
Discussion started by: nex_asp
5 Replies

4. Shell Programming and Scripting

using substring in shell script

This is the data I am having in a file Just for sample I have given 3 records. The file which I am having consists of n number of records. ABC123 10 01/02/2008 2008-01-03-00.00.00.000000 DYUU 22 02/03/2008 2008-01-04-00.00.00.000000 RF33 88 03/05/2008 2008-01-05-00.00.00.000000 ... (24 Replies)
Discussion started by: kmanivan82
24 Replies

5. Shell Programming and Scripting

Substring in Bourne shell

Shell used: Bourne shell Environment: UNIX in a Sun machine I found myself struggling to figure out how to split a string. I kept wishing it's in Java that I can do it real quick. Basically I have a string as follows: "http://machine1:8080/aaa/bbb" I need to get the value as follows to... (8 Replies)
Discussion started by: totziens
8 Replies

6. UNIX for Dummies Questions & Answers

Difference between cat , cat > , cat >> and touch !!!

Hi Can anybody tell the difference between Difference between cat , cat > , cat >> and touch command in UNIX? Thanks (6 Replies)
Discussion started by: skyineyes
6 Replies

7. Shell Programming and Scripting

Substring in shell script

I need a help in getting substring of each line in input file. I am writing a script that will read a file from a directory on daily basis, I mean everyday a new file will be stored in this directory, it will replace old file. I have to read contents of this file, the contents will be as... (5 Replies)
Discussion started by: jyotib
5 Replies

8. UNIX for Dummies Questions & Answers

Substring in Shell Script

Hi I'm new to Shell scripting. Someone please help me in extracting a portion of string from a file. Eg: I got a file like, Readme.txt and has the following name value pairs input1 : /homes/input1/ input2 : /homes/input2/ ... ... When I give the parameter input1, the value... (3 Replies)
Discussion started by: smartbuddy
3 Replies

9. Shell Programming and Scripting

variable= 'cat file|wc -l' String problems

Hi, does anybody knows about wc -l, how to transform it inot a just number? this script ALWAYS executes the command3!!, However, the value of BMU_RUNNING is 1 case $BMU_RUNNING in *0) command1 ;; *1) command 2;; *)command 3;; esac The... (3 Replies)
Discussion started by: Santiago
3 Replies

10. Shell Programming and Scripting

Substring in C shell script?

i am a new user of C-shell script. I want to know can i create a substring in a string. That means when i got a variable $input = "it is number 2" I want to get the "2" to be another variable. Can i do that in C-shell and how to ? Thank you so much dinodash (0 Replies)
Discussion started by: dinodash
0 Replies
Login or Register to Ask a Question