How to extract the file name and change the ending in bash? [duplicate] - string

This question already has answers here:
Extract filename and extension in Bash
(38 answers)
How do I rename the extension for a bunch of files?
(28 answers)
Closed 3 years ago.
I have a bash script that accepts file names that end with .in, for example a1.in a2.in, and I want to take that argument and extract the a1 and add .out to it, how do I do that?
I know accepting an argument is $1 - but how do I extract the a1?

To remove a fixed suffix from an argument (or other variable) use ${1%.in} -- that will remove the trailing .in or do nothing if the argument does not end in .in. To add a suffix, just add it: ${1%.in}.out
To remove any suffix, you can use glob patterns after the % like so: ${1%.*}. This will remove the shortest matching suffix. You can remove the longest matching suffix with %%: ${1%%.*}

If your files have only one extension:
$ echo "a.in" | cut -d '.' -f1
a

Related

Linux bash - Find file with pattern containing spaces [duplicate]

This question already has answers here:
When to wrap quotes around a shell variable?
(5 answers)
Closed 21 days ago.
I'm using a shell script on Linux that is processing some files from a directory based on a pattern.
The pattern can contain spaces.
The question is how do I get the list of files that match the pattern?
Example:
This is the list of files:
file_without_spaces.vol-1.txt
file_without_spaces.vol-2.txt
file with spaces.vol-1.txt
file with spaces.vol-2.txt
file with spaces.vol-3.txt
Result when the pattern is "file_without_spaces":
file_without_spaces.vol-1.txt
file_without_spaces.vol-2.txt
Result when the pattern is "file with spaces":
file with spaces.vol-1.txt
file with spaces.vol-2.txt
file with spaces.vol-3.txt
The pattern comes in an env variable, let's call it PATTERN.
Grepping for the pattern does not work, as it may contain spaces which grep cannot handle. Same for using the pattern as a parameter to find, e.g. find <dir> -name $PATTERN
Grep and find can handle spaces just fine. You simply need to quote them:
find <dir> -name "$PATTERN"

Batch remove extra file extensions in bash [duplicate]

This question already has answers here:
How can I remove the extension of a filename in a shell script?
(15 answers)
bash - command substitution is omitting whitespaces
(1 answer)
Closed 12 months ago.
I converted some files to another format but in doing so added an extra extension. For example foo.bar.temp. I wrote a script to delete the .temp, but it doesn't work when the filenames have spaces.
for f in *; do mv "$f" $(basename "$f" .temp) ; done
If I double escape "'$f'" then basename won't read the extension. If I leave it as is then it will think that the second word in the title is the directory I want to move to.
How can I just remove the .temp?

How to remove date from filename linux [duplicate]

This question already has answers here:
Rename multiple files while keeping the same extension on Linux
(4 answers)
Closed 3 years ago.
I have a scenario where I want to remove date from filename
Lets take an example 1 :
ABC_2019_06_12.txt
Lets take an example 2 :
ABCDEF_202012040120456.txt
using cut I cannot delete required text
how to cut to get the required below output like below
ABC.txt
ABCDEF.txt
One command which should work for all scenario which ever filename it is
My solution which I worked is to read the number of position and cut that part but I don't find it effective any other solution will be appreciated
In bash you can cut off the part starting with underscore:
$ filename=ABC_2019_06_12.txt
$ filename=${filename%%_*}
$ echo $filename
ABC

Replace hyphens with underscores in bash script [duplicate]

This question already has answers here:
Replacing some characters in a string with another character
(6 answers)
Difference between sh and Bash
(11 answers)
Closed 4 years ago.
Trying to write a bash script and in one part of it I need to take whatever parameter was passed to it and replace the hyphens with underscores if they exist.
Tried to do the following
#!/usr/bin/env bash
string=$1
string=${string//-/_}
echo $string;
It's telling me that this line string=${string//-/_} fails due to "Bad substitution" but it looks like it should do it? Am I missing something?
There is nothing wrong with your script, and it should work in modern versions of Bash.
But just in case you can simplify that to :
#!/bin/bash
echo "$1" | tr '-' '_'
This is in case that parameter substitution does not work ( which seems to be your case ).
Regards!

finding specific pattern in linux [duplicate]

This question already has answers here:
Print only matching word, not entire line through grep
(2 answers)
Closed 5 years ago.
I want to find specific pattern in all the files in a directory and copy them to another line
For E.g
I want to find LOG_WARNING in one file XYZ and copy them to another file.
LOG_WARNING (abc, xyz,("WARNING: Error in sending concurrent_ to pdm\n"));
command i have used is :
grep -rin "LOG_WARNING.*" file_name.c > output.txt
but it is not copying till the semicolon, please note that other texts are available in next line. I want to copy till ;(semi-colon)
grep -rh "LOG_WARNING" * > out.txt
This will match the pattern in all the files inside the directory.
Since you mentioned that the texts that are present after the ';' are on the next line, I have provided this command.
This will match the pattern and print the entire line, till the ';'.
Else,
try this
grep -roPh 'LOG_WARNING[^;]*;' * > out.txt

Resources