Unix: Removing date from a string in single command - shell

For satisfying a legacy code i had to add date to a filename like shown below(its definitely needed and cannot modify legacy code :( ). But i need to remove the date within the same command without going to a new line. this command is read from a text file so i should do this within the single command.
$((echo "$file_name".`date +%Y%m%d`| sed 's/^prefix_//')
so here i am removing the prefix from filename and adding a date appended to filename. i also do want to remove the date which i added. for ex: prefix_filename.txt or prefix_filename.zip should give me as below.
Expected output:
filename.txt
filename.zip
Current output:
filename.txt.20161002
filename.zip.20161002

Assumming all the files are formatted as filename.ext.date, You can pipe the output to 'cut' command and get only the 1st and 2nd fields :
~> X=filename.txt.20161002
~> echo $X | cut -d"." -f1,2
filename.txt

I am not sure that I understand your question correctly, but perhaps this does what you want:
$((echo "$file_name".`date +%Y%m%d`| sed -e 's/^prefix_//' -e 's/\.[^.]*$//')

Sample input:
cat sample
prefix_original.txt.log.tgz.10032016
prefix_original.txt.log.10032016
prefix_original.txt.10032016
prefix_one.txt.10032016
prefix.txt.10032016
prefix.10032016
grep from start of the string till a literal dot "." followed by digit.
grep -oP '^.*(?=\.\d)' sample
prefix_original.txt.log.tgz
prefix_original.txt.log
prefix_original.txt
prefix_one.txt
prefix.txt
prefix
perhaps, following should be used:
grep -oP '^.*(?=\.\d)|^.*$' sample

If I understand your question correctly, you want to remove the date part from a variable, AND you already know from the context that the variable DOES contain a date part and that this part comes after the last period in the name.
In this case, the question boils down to removing the last period and what comes after.
This can be done (Posix shell, bash, zsh, ksh) by
filename_without=${filename_with%.*}
assuming that filename_with contains the filename which has the date part in the end.

% cat example
filename.txt.20161002
filename.zip.20161002
% cat example | sed "s/.[0-9]*$//g"
filename.txt
filename.zip
%

Related

Remove/replace a dynamic String in file using unix

I have File containing the data like below
File Name :- Test.txt
TimeStamp_2017-12-43 09:09:14.0999/-ext-10100/Year/Month/Day
TimeStamp_2000-12-43 07:09:14.0999/-ext-10200/Year/Month/Day
TimeStamp_2015-12-43 06:09:14.0999/-ext-10200/Year/Month/Day
TimeStamp_2010-12-43 05:09:14.0999/-ext-10200/Year/Month/Day
TimeStamp_2011-12-43 04:09:14.0999/-ext-1090/Year/Month/Day
TimeStamp_2018-12-43 03:09:14.0999/-ext-920/Year/Month/Day
TimeStamp_2013-12-43 02:09:14.0999/-ext-1200/Year/Month/Day
TimeStamp_2016-12-43 01:09:14.0999/-ext-02/Year/Month/Day
Here i need to replace or remove below format in each line
TimeStamp_*/-ext-*
**Input line in file(Sampel TimeStamp value and -ext- value is changing every time)
TimeStamp_2017-12-43 09:09:14.0999/-ext-10100/Year/Month/Day
Ouput Line after remove or replace
Year/Month/Day
Can any one help on this Question
Simply with **sed**:
sed 's#.*-ext-[^/]*/##' file
Use below sed command, it will work for you. How will it work? First it will find the pattern TimeStamp_.*-ext-.* (here you need to add dot . with * to inform sed command that you are using * as wild card character) and replace with a blank line and second expression /^\s*$/d will search for blank line and remove it and finally you will get your required output. Every expression is separated with ; in sed command.
sed -e 's/TimeStamp_.*-ext-.*//;/^\s*$/d' Test.txt > tmp.txt
mv tmp.txt Test.txt
Hope this will help you.
When you wat to keep everything after the second slash, use
cut -d"/" -f3- Test.txt

Shell Removing part of a string with sed

Good day.
I actually have 2 questions that are related to the sed command in shell and they are very similar.
The first question is how to use sed to get a file name and remover part of it's name like the example below:
Original file:
BAT_MAN_T_spades_proc_whatever_t6_12345_14785963214785_12345.txt
What i want the file name to look like:
BAT_T_spades_proc_whatever_t6_12345_14785963214785_12345.txt
I just want the "MAN" part after the first underline to be removed out of the original file name.
The second question is about the following sed command that I've found on a file a while ago:
random_string_var_name=$(echo $file_name | sed -r 's/^[^_]*_[^_]*_(.*_t[0-9]{1}).*(_[0-9]*)\.txt/_\1\2/')
this pretty much get parts of a file name a saves it on a variable, like the example bellow:
Name of the file:
BAT_MAN_T_spades_proc_whatever_t6_12345_14785963214785_12345.txt
What that sed command gets:
T_spades_proc_whatever_t6_12345
I got what it does but i don't understand how that command works, so i would like to understand that.
I just want the "MAN" part after the first underline to be removed out of the original file name.
echo "BAT_MAN_T_spades_proc_whatever_t6_12345_14785963214785_12345.txt" | sed "s/MAN_//"
What if i want to always remove the first word after the first underline and keep everything else?
echo "BAT_MAN_T_spades_proc_whatever_t6_12345_14785963214785_12345.txt" | sed -r 's/^([^_]*)_[^_]*(_.*)/\1\2/'
what does this do: echo $file_name | sed -r 's/^[^_]*_[^_]*_(.*_t[0-9]{1}).*(_[0-9]*)\.txt/_\1\2/')
-r: runs sed in "extended regex" mode
^: matches beginning of word
[^_]* matches everything except underline 0 or more times
_ matches underline
(.*_t[0-9]{1}) matches zero or more of anything followed by _t and only one number. This match is stored in variable 1
(_[0-9]*) same thing, only that there is no prefix
/_\1\2: replaces the whole filename with _ at the beginning and the match in the first brackets and the match in the second bracket
I recommend reading up on regular expressions. They are important and not really hard to get into
I think that you may have something else than "MAN", you may have "WOMAN". So you can use:
file_name=BAT_WOMAN_T_spades_proc_whatever_t6_12345_14785963214785_12345.txt
echo $file_name | sed 's/_[^_]*_/_/'

How to get a string out of a plain text file [duplicate]

This question already has answers here:
How do I split a string on a delimiter in Bash?
(37 answers)
Closed 6 years ago.
I have a .txt file that has a list containing a hash and a password so it looks likes this:
00608cbd5037e18593f96995a080a15c:9e:hoboken
00b108d5d2b5baceeb9853b1ad9aa9e5:1c:wVPZ
Out of this txt file I need to extract only the passwords and add them in a new text file so that I have a list that would look like this:
hoboken
wVPZ
etc
etc
etc
etc
How to do this in bash, a scripting language or simply with a text editor?
Given your examples, the following use of cut would achieve what you want:
cut -f3 -d':' /folder/file >> /folder/result
The code above would delete anything before (and including) the second colon : on each line, which would work on your case, given your examples. The result is stored on /folder/result.
Edit: I edited this answer to make it simpler.
I suggest to use awk to get always last column from your file:
awk -F ':' '{print $NF}' file
Output:
hoboken
wVPZ
With sed, to remove the string up to ::
sed 's/.*://' file
You could also use grep:
$ grep -o [^:]*$ file
hoboken
wVPZ
-o print only matching part
[^:] anything but :
* all matching characters
$ end of record

Finding a particular string from a file

I have a file that contains one particular string many times. How can I print all occurrences of the string on the screen along with letters following that word till the next space is encountered.
Suppose a line contained:
example="123asdfadf" foo=bar
I want to print example="123asdfadf".
I had tried using less filename | grep -i "example=*" but it was printing the complete lines in which example appeared.
$ grep -o "example[^ ]*" foo
example="abc"
example="123asdfadf"
Since -o is only supported by GNU grep, a portable solution would be to use sed:
sed -n 's/.*\(example=[^[:space:]]*\).*/\1/p' file

How to parse a config file using sed

I've never used sed apart from the few hours trying to solve this. I have a config file with parameters like:
test.us.param=value
test.eu.param=value
prod.us.param=value
prod.eu.param=value
I need to parse these and output this if REGIONID is US:
test.param=value
prod.param=value
Any help on how to do this (with sed or otherwise) would be great.
This works for me:
sed -n 's/\.us\././p'
i.e. if the ".us." can be replaced by a dot, print the result.
If there are hundreds and hundreds of lines it might be more efficient to first search for lines containing .us. and then do the string replacement... AWK is another good choice or pipe grep into sed
cat INPUT_FILE | grep "\.us\." | sed 's/\.us\./\./g'
Of course if '.us.' can be in the value this isn't sufficient.
You could also do with with the address syntax (technically you can embed the second sed into the first statement as well just can't remember syntax)
sed -n '/\(prod\|test\).us.[^=]*=/p' FILE | sed 's/\.us\./\./g'
We should probably do something cleaner. If the format is always environment.region.param we could look at forcing this only to occur on the text PRIOR to the equal sign.
sed -n 's/^\([^,]*\)\.us\.\([^=]\)=/\1.\2=/g'
This will only work on lines starting with any number of chars followed by '.' then 'us', then '.' and then anynumber prior to '=' sign. This way we won't potentially modify '.us.' if found within a "value"

Resources