This question already has answers here:
How can I remove the extension of a filename in a shell script?
(15 answers)
Closed 5 years ago.
I have hundreds files need to loop through for an analysis using a bash script. One step I need to do is to split a long string and cat it as an output name. For example, suppose I have one string such like:
5018.a.Radiation_Induced_Lymphoma.Tumor__p53+_-.SL200300_SL200300.exome_1tier.mm10.kapa_re_cap_v6_3utr.final.bam
What I wanted is to rename it as two output file names such as:
5018.a.Radiation_Induced_Lymphoma.Tumor__p53+_-.SL200300_SL200300.exome_1tier.mm10.kapa_re_cap_v6_3utr.final_R1.fastq
5018.a.Radiation_Induced_Lymphoma.Tumor__p53+_-.SL200300_SL200300.exome_1tier.mm10.kapa_re_cap_v6_3utr.final_R2.fastq
The only changes are removing .bam from the original and cat _R1.fastq and _R2_fastq. Does somebody know how to realize it using bash commands?
somefile=blahblahblah.final.bam
foo "$somefile" "${somefile%.*}_R1.fastq" "${somefile%.*}_R2.fastq"
Related
This question already has answers here:
How do I pass on script arguments that contain quotes/spaces?
(2 answers)
Bash script to cd to directory with spaces in pathname
(14 answers)
Closed 24 days ago.
I have a script that appends the date and time to all files in a folder. I use the script like this...
bash append_date.sh /home/user/Documents/Podcasts/
and that will append the date to all files in the /home/user/Documents/Podcasts/ folder
Problem is that if there is a whitespace in the directory tree it fails to do anything. ie
bash append_date.sh /home/user/Documents/My Stuff/
I have tried passing the following, but that does not work
bash append_date.sh /home/user/Documents/My\ Stuff/
How do I get this script to play nice with whitespaces?
Many thanks for any help.
This question already has answers here:
How to concatenate string variables in Bash
(30 answers)
Closed 1 year ago.
So I'm trying to compose a string using script shell
#!/bin/sh
blNumber=1111619832
echo '*'$blNumber+='*.xlsx'
I expect the output to be: *1111619832*.xlsx
but as a result I get: *+=*.xlsx
Btw I tried to figure out why so I tried this code
#!/bin/sh
blNumber=1111619832
output="*${blNumber}"
and whenever I add something after *${blNumber} it get concatenated at the begging of the string
Why are you using += in the first place?
$ echo '*'$blNumber'*.xlsx'
*1111619832*.xlsx
Or put it inside double-quotes. It's best practice to quote all variables anyway.
$ echo "*$blNumber*.xlsx"
*1111619832*.xlsx
This question already has answers here:
Unix: How to delete files listed in a file
(13 answers)
Shell command/script to delete files whose names are in a text file
(7 answers)
Closed 2 years ago.
I have a file with several paths to other files.
/root/Desktop/test1/test2/f1
/root/Desktop/test1/test2/f2
I need to execute a command from the terminal to delete f1 and f2 by importing their paths from that file.
Something like "rm code"
code = line 1 of the file text
Is there a way to do so ?
rm root/Desktop/test1/test2/f*
should do if I understood your question.
This question already has answers here:
How to read a file into a variable in shell?
(9 answers)
Closed 4 years ago.
Is there a way to copy the content of a text file to a variable in Bash?
Let's say I have a file containing some text, and I would like to modify text but not a file itself. How can I copy the content of this file to a variable, and then modify the variable?
I'm not very clear on what you're asking, but I think this is what you're after.
if you have file.txt, you can do this
var1=$(cat /path/to/file.txt)
you can then manipulate it how you please.
edit:
You can access the variable by $var1, i.e. echo "$var1"
This question already has answers here:
Shell command to retrieve specific value using pattern
(3 answers)
Closed 8 years ago.
I have file test.txt contains the following
AA=testing
BB=help
CC=hello
How can i make a bash script that will get each value and assign to a new variable?
#!/bin/bash
var1=testing
var2=help
var3=hello
thanks for the help
First of all a = value is not correct syntax in shell. In shell the spaces are important.
When you have a valid file, you can use the eval function to evaluate that file as a string, or simply source it.