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.
Related
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' {}
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
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 6 years ago.
Improve this question
My Perl program creates the file
10001.ICNTL.20160602.20160603.OPR.GAAP.PROD.PFI.PRE.txt
Then in method I have a code:
if ( -e $report ) {
# we parse the filet here is some code, at the end
{
else
}
print "*** Skipping \\NYNAS\NYNDS\VOL\DATA\INVACCT\FUND_RECS_PFI\10001.ICNTL.20160603.PROD.GAAP.PFI\10001.ICNTL.20160602.20160603.OPR.GAAP.PROD.PFI.PRE.TXT
}
I cannot understand why the script doesn't see the file. I've checked it several times letter by letter. Can it be because of the Upper case TXT, but in reality it is lower case?
Is your file 10001.ICNTL.20160602.20160603.OPR.GAAP.PROD.PFI.PRE.txt in directory \\NYNAS\NYNDS\VOL\DATA\INVACCT\FUND_RECS_PFI?
At a guess you're not escaping the file path correctly. Even if you use single-quotes, there is no way of representing the two leading backslashes in Uniform Naming Convention (UNC) paths without escaping at least one of them
Check the output of print $report, "\n" to see what you've really written
My preference is to use four backslashes at the start of the path string, like this
my $report = '\\\\NYNAS\NYNDS\VOL\DATA\INVACCT\FUND_RECS_PFI\10001.ICNTL.20160603.PROD.GAAP.PFI\10001.ICNTL.20160602.20160603.OPR.GAAP.PROD.PFI.PRE.TXT';
print -e $report ? "Found\n" : "Not found\n";
And Perl allows you to use forward slashes in place of backslashes in a Windows path, so you could write this instead if you prefer, but paths like this aren't valid in other Windows software
my $report = '//NYNAS/NYNDS/VOL/DATA/INVACCT/FUND_RECS_PFI/10001.ICNTL.20160603.PROD.GAAP.PFI/10001.ICNTL.20160602.20160603.OPR.GAAP.PROD.PFI.PRE.TXT';
Or another alternative is to relocate your current working directory. You cannot cd to a UNC path on the Windows command line, but Perl allows you to chdir successfully
chdir '//NYNAS/NYNDS/VOL/DATA/INVACCT/FUND_RECS_PFI' or die $!;
Thereafter all relative file paths will be relative to this new working directory on your networked system
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 8 years ago.
Improve this question
So when I open a log-file, it’s all one long line. Notepad word wraps it, but it really is one line. When I open a Linux terminal, and I type "cat log-file" it formats the file and does line breaking. Is there a way to take this log-file and output it into a new file how cat displays it?
Sorry if I'm being vague, I'm new to bash, and I'm still trying to learn things.
Could you use the "sed" command to add a \n somewhere in there to break the line?
I am trying to display this file in an html file. so I wanted to replace any \n with an tag. so I'm doing sed's/\n/<br>/' <log-file >html-file but its not working. how would i go about that?
To replace all \n instances with <br>:
awk -v ORS='<br>' 1 <log-file >html-file
awk reads the input line by line (thanks to the default input record separator, RS, being \n.
ORS='<br>' sets the output record separator, ORS, to <br>
1 is merely a shorthand for {print}, which simply prints each input line terminated with ORS.
As for why your command doesn't work:
sed's/\n/<br>/' is missing a space between sed and its program, 's/...', which results in a single string that is not a valid command name.
Even if you correct that, the command will still not work as intended, because sed reads line by line, and each line is read without the terminating \n; therefore, as written, you cannot replace \n in the input, and a \n will again be appended on output - effectively, your command would be a no-op.
Update: # gniourf_gniourf points out that there is a straightforward sed solution, too, if it's OK that instead of replacing the \n instances, <br> is inserted before them - which should be fine, since the output is HTML:
sed 's/$/<br>/' <log-file >html-file
$ matches the end of each input line (without capturing anything) and "replaces" it with <br>, i.e., effectively appends <br> to the line before outputting it \n-terminated.
Should there still be a need to get rid of the \n instances, you could simply pipe to tr:
sed 's/$/<br>/' <log-file | tr -d $'\n' >html-file
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'