How to delete files matching specific character placement? [closed] - bash

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
I'm trying to delete files with names that contain certain digits in specific placement. Using bash and text file that contains those specific digits.
I have a single directory with files in the following naming convention: 2019-08-06-11-35-13_2091232924_4569.mp3
I have a text file containing area codes that I'd like to match and delete. One of those area codes is 209. Reading from the right of the filename is always consistent. So I'd like to match characters 17, 18, 19 from the right, against the text file and then delete those files using bash. I've tried plain wildcard matching but it will delete files with those digits in other positions.

You can use the ? wildcard, which matches any single character.
rm ????-??-??-??-??-??_209???????_????.mp3
However, it appears that all the wild characters are digits, so you could use [0-9] instead of ? and be safer.
rm [0-9][0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9]-[0-9][0-9]-[0-9][0-9]-[0-9][0-9]_209[0-9][0-9][0-9][0-9][0-9][0-9][0-9]_[0-9][0-9][0-9][0-9].mp3
If you're getting the area code from a file, you can replace 209 in the pattern with the variable that you assigned from the file.
rm ????-??-??-??-??-??_"$code"??????_????.mp3

You could probably do something with xargs:
xargs -n1 <input.txt sh -c 'rm *_$1*_*.mp3' {}

Related

How to remove | substract directory name from bash? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 months ago.
Improve this question
Within my bash script, which is running on a Linux server, I have a variable that points to a specific path, let's call this my-path.
my-path="/home/vMX-ENV/vMX-21.1R1/"
echo "The path is: $my-path"
...
From the given variable my-path, I am looking for a way only to display the version number 21.1R1.
The following is an example of what I am trying to accomplish.
./script.sh
the path is: /home/vMX-ENV/vMX-21.1R1/
the version is: 21.1R1
Is there a way to do this?
Thanks!
Bash has a fairly wide variety of built-in mechanisms for manipulating variables' values. Of particular interest for the present problem are parameter expansion forms that remove prefixes or suffixes that match specified shell patterns. For example:
# Remove any trailing slash and store the result in DIRNAME_NORM
DIRNAME_NORM=${DIRNAME_MAIN%/}
# Emit the value of $DIRNAME_NORM, less the longest prefix matching shell
# pattern *vMX-
echo "${DIRNAME_NORM##*vMX-}"
There is no need to rely on an external program for this case.
Using sed grouping and back referencing
$ sed 's/[^0-9]*\([^/]*\).*/\1/' input_file
21.1R1
/[^0-9]* - Exclude anything up to the next occurance of a digit character. As this part is not within the parenthesis () to be grouped, it will be excluded.
\([^/]*\) - This will group everything from the first digit up to the next occurance of / slash.
.*/ - Exclude everything else
\1 - Return the group with backreference \1.
awk can also be used.
$ awk -F"[/-]" '{print $6}' input_file
21.1R1
-F"[/-]" - Set delimiter to / and - then print column 6 which will contain the string.

How to separate multiple parts of a line using ';' using shell script? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
I have a '.GS3' file with multiple lines like this one:
0123456789 aaa.aaa3456
I want to separate it like this:
01234;56789 ;aaa.aaa;3456
I know the start and end of each part of the line. Is it possible to do this considering multiple lines? For example:
0123456789 aaa.aaa3456
aaaaabbbbbbbxxxxxxxwwww
Into
01234;56789 ;aaa.aaa;3456
aaaaa;bbbbbbb;xxxxxxx;wwww
.. but if it does have fixed lengths you can do it this way:
$ sed -r 's/^(.{5})(.{7})(.{7})(.{4})$/\1;\2;\3;\4/' test.txt
01234;56789 ;aaa.aaa;3456
aaaaa;bbbbbbb;xxxxxxx;wwww
I think .{5} is self explanatory. Due to the -r option the first group (.{5}) can be referenced by \1. It's a group due to ( and ).
The characters ^ and $ represent the beginning and ending of every line in the file test.txt.

find a special character in file & remove the special character, before/after words of special character using shell script [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
A file contains email addresses. Requirement is to remove words present before/after a special character & also the special character from a file.
e.g.-
file contains email addresses such as:
abc.def*cap.com,abc.def.ghi*cap.com
file should contain the email addresses such as:
abc.com,abc.def.com
#(special character), word before # & one word after # need to be removed.only one-one word before/after special character need to be removed.
I'd use perl or sed
$ echo 'abc.def#cap.com,abc.def.ghi#cap.com' | sed 's/[[:alnum:]_]\+#[[:alnum:]_]\+//g; s/\.\././g'
abc.com,abc.def.com
$ echo 'abc.def#cap.com,abc.def.ghi#cap.com' | perl -pe 's/\w+#\w+//g; s/\.\././g'
abc.com,abc.def.com

fswatch to watch only a certain file extension [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about a specific programming problem, a software algorithm, or software tools primarily used by programmers. If you believe the question would be on-topic on another Stack Exchange site, you can leave a comment to explain where the question may be able to be answered.
Closed 2 years ago.
The community reviewed whether to reopen this question 1 year ago and left it closed:
Original close reason(s) were not resolved
Improve this question
I am using fswatch and only want it triggered if a file with extension .xxx is modified/created etc. The documentation and the second reference below indicate that:
All paths are accepted by default, unless an exclusion filter says otherwise.
Inclusion filters may override any exclusion filter.
The order in the definition of filters in the command line has no effect.
Question: What is the regular expression to use to exclude all files that do not match the .xxx extension?
References:
Is there a command like "watch" or "inotifywait" on the Mac?
Watch for a specific filetype
Platform:
MacOS 10.9.5.
I'm fswatch author. It may not be very intuitive, but fswatch includes everything unless an exclusion filter says otherwise. Coming to your problem: you want to include all files with a given extension. Rephrasing in term of exclusion and inclusion filters:
You want to exclude everything.
You want to include files with a given extension ext.
That is:
To exclude everything you can add an exclusion filter matching any string: .*.
To include files with a given extension ext, you add an inclusion filter matching any path ending with .ext: \\.ext$. In this case you need to escape the dot . to match the literal dot, then the extension ext and then matching the end of the path with $.
The final command is:
$ fswatch [options] -e ".*" -i "\\.ext$"
If you want case insensitive filters (e.g. to match eXt, Ext, etc.), just add the -I option.
You may watch for changes to files of a single extension like this:
fswatch -e ".*" -i ".*/[^.]*\\.xxx$" .
This will exclude all files and then include all paths ending with .xxx (and also exclude files starting with a dot).
If you want to run a command on the file change, you may add the following:
fswatch -e ".*" -i ".*/[^.]*\\.xxx$" -0 . | xargs -0 -n 1 -I {} echo "File {} changed"

Throw away all text after a blank line in bash [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance.
Closed 9 years ago.
Improve this question
Using bash, I want to throw away all of the text in a file after the first blank line. The blank line is used as a delimiter between records, and I only want the first record in the file. Unfortunately, the number of lines per record can change depending on what the record refers to exactly, so I can't just keep the first n lines as a global solution.
[EDIT] Here is a solution that works:
qstat -f > out.tmp
grep -A90 -B0 $1 out.tmp > out2.txt
awk '/^$/{exit}{print}' out2.txt
rm out.tmp out2.txt
where $1 points to the name of the text file to be analyzed (passed as an argument to the script that I'm writing). Thanks.
You can do this with pretty much any generic text processing tool, e.g.:
awk '$0==""{exit}{print}'
awk '/^$/{exit}{print}'
sed '/^$/q'

Resources