Replacing filename with sed in bash script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Replacing filename with sed in bash script
# 1  
Old 12-15-2018
Replacing filename with sed in bash script

I need to cat two files with similar names. I am using the following script:
Code:
#!/bin/bash
if [[ -f $1 ]]
then
        file=$1
        file2="${file%R1.fastq}R2.fastq"
        echo fetching data from R2 file ...
        sleep 3
        cat $file $file2 > infile
else
        echo "Input_file passed as an argument $1 is NOT found."
        exit;
fi

However, I was expecting this to work too:
Code:
        file=$1
        file2=eval echo "$file" | sed -r 's/(.*)R1(.*)/\1R2\2/'

What am I doing wrong in my second script?
The second question would be, why I cannot use cat "${file*} to concatenate both files?
Thanks in advance for any help!
# 2  
Old 12-16-2018
Code:
file="${1%1.*}"
file2="${1/R1/R2}"
cat $file*
cat "$1" "${1/R1/R2}"
cat "$1" "$file2"
file2=$(sed 's/R1/R2/' <<<"$1")


Last edited by nezabudka; 12-16-2018 at 02:30 AM..
# 3  
Old 12-16-2018
Quote:
Originally Posted by Xterra
... However, I was expecting this to work too:
Code:
        file=$1
        file2=eval echo "$file" | sed -r 's/(.*)R1(.*)/\1R2\2/'

What am I doing wrong in my second script?

You need to deploy "command substitution":
Code:
file2=$(...)

However, using eval and sed is not the smartest way to get your task done.


Quote:
The second question would be, why I cannot use cat "${file*} to concatenate both files?
You cannot expect "globs" to work in a variable expansion. Use e.g.
Code:
cat ${file%R1*}*

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Perl one liner in bash script not replacing hours and minutes [HH:MM]

Hi I want to replace time stamp in the following line PROCNAME.Merge.exchMon.CODE.T_QSTART 08:45 read assuming the new time stamp is 09:45 ; the line is getting replaced as below :45 read I'm trying to use the perl one liner in bash script perl -pi... (4 Replies)
Discussion started by: charlie87
4 Replies

2. UNIX for Beginners Questions & Answers

Query on using command "sed" for replacing text in bash shell

Re: Query on using command "SED" for replacing text in bash shell While using the command "sed" for find and replace, I wanted to know how one could find a constant and replace it with a variable inside the quotation syntax of sed? I wanted to replace constant 3 with variable name... (3 Replies)
Discussion started by: achandra81
3 Replies

3. UNIX for Beginners Questions & Answers

Bash script - Remove the 3 top level of a full path filename

Hello. Source file are in : /a/b/c/d/e/f/g/some_file Destination is : /d/e where sub-directories "f" and "g" may missing or not. After copying I want /a/b/c/d/e/f/g/file1 in /d/e/f/g/file1 On source /a is top-level directory On destination /d is top-level directory I would like... (2 Replies)
Discussion started by: jcdole
2 Replies

4. Shell Programming and Scripting

How to Pass filename to AWK in bash script

I have written a script which works fine, to remove patterns contained in EXCLUDE.DAT from input.txt awk 'BEGIN {n=0;while (getline < "EXCLUDE.DAT" > 0){ex=$0;n++}} {for(var in ex){print var "-" ex $0 ;i++}}' input.txt The last problem I need to solve is how to pass the file... (3 Replies)
Discussion started by: nixie
3 Replies

5. Shell Programming and Scripting

Bash script to sort files into folder according to a string in the filename

Hi all. I am very new to linux scripting and i have a task i can only solve with a script. I need to sort files base on the date string in their filenames and create a folder using the same date string then move the files to their respective folders. Scenario: Folder Path:... (1 Reply)
Discussion started by: ace47
1 Replies

6. Shell Programming and Scripting

URGENT!!! bash script to sort files into folder according to a string in the filename

Hi all. I am very new to linux scripting and i have a task i can only solve with a script. I need to sort files base on the date string in their filenames and create a folder using the same date string then move the files to their respective folders. Scenario: Folder Path:... (1 Reply)
Discussion started by: ace47
1 Replies

7. Shell Programming and Scripting

Help parsing filename with bash script

Hi all! Looking for some help parsing filenames in bash. I have a directory full of files named "livingroom-110111105637.avi". The format is always date and time (yymmddhhmmss). I'm looking to parse the filenames so they are a little more easily readable. Maybe rename them to... (4 Replies)
Discussion started by: mtehonica
4 Replies

8. UNIX for Dummies Questions & Answers

bash script to increment a digit in filename

Hi guys, Can someone help me out with this: I have a directory with files like the following, GHost++ 2010-03-14 04-01 DotaCash RD us_ca LC #7 (44m19s).w3g GHost++ 2010-03-14 04-06 DotaCash AP us_ca LC #8 (42m24s).w3g GHost++ 2010-03-14 04-07 DotaCash AR us_ca LC #10 (08m23s).w3g ... (4 Replies)
Discussion started by: hbjlee17
4 Replies

9. Shell Programming and Scripting

Simple 'sed' script for replacing text

Hi All, So I found a simple sed command to replace text in a file (http://www.labnol.org/internet/design/wordpress-unix-replace-text-multiple-files/1128/): sed -e 's/OLDtext/NEWtext/' -i file(s) Because I'm lazy and don't want to remember this each time I want to do this, I wrote the following... (4 Replies)
Discussion started by: ScKaSx
4 Replies

10. Shell Programming and Scripting

replacing contents of files from a bash script

I'm writing a shell script and I need to replace the contents of a configuration file based on what is passed to the script...can I replace expressions in a file from a bash shell script? (2 Replies)
Discussion started by: HumanBeanDip
2 Replies
Login or Register to Ask a Question