Unix Sed Command to replace file name entries in a *.txt file - regex

I have a class.txt file which contains multiple .class file entries along with their respective paths, I want to rename .class file names as mentioned
Requirement:
from
modules/abc_1.1.3/abc.domain.ear!/APP-INF/lib/adj.jar!/ba/sr/ApplicationModule.class
to:
modules/abc_1.1.3/abc.domain.ear!/APP-INF/lib/adj.jar!/ba/sr/[ApplicationModule\$.*\.class]
I tried using sed command, but didnt get desired output as shown below
cat class.txt | sed "s/.class/\\\\$.*\\\.class]/g"
modules/abc_1.1.3/abc.domain.ear!/APP-INF/lib/adj.jar!/ba/sr/ApplicationModule\$.*\.class]
Kindly help, Thanks!

You have to capture file name:
sed 's/\([^/]*\).class/[\1\\$.*\\.class]/g'

You need to use capture groups in order to capture the filename and its extension in two separate groups.
$ sed 's~\([^./]*\)\.\([^/.]*\)$~[\1\\\$.*\\.\2]~' file
modules/abc_1.1.3/abc.domain.ear!/APP-INF/lib/adj.jar!/ba/sr/[ApplicationModule\$.*\.class]

Related

Shell script to extract text between two strings and modify and replace it in the same file

I have a markdown file which have src to respective images.
For example:
![Login Screen](0005_eppm_login_page.png)
I want to replace it as:
![Login Screen](../src/0005_eppm_login_page.png)
I guess you got the problem of the slashes.. this one-liner may give you a hand:
sed '/\[Login Screen\]/{s#(#(../src/#}'
In the sed one-liner, we can pick a separator other than / for s(substitution), particularly, when the text/replacements containing slashes.
Use the following:
sed -i 's,\(!\[[^][]*](\)\([^()]*\.png)\),\1../src/\2,g' file
It will replace ![...](...\.png) patterns with ![...](../src/...\.png).

Extract Source IP from log files

i want to extract "srcip=x.x.x.x" from log file in bash. my log file is like this:
2019:06:23-17:50:03 myhost ulogd[5692]: id="2021" severity="info" sys="SecureNet" sub="packetfilter" name="Packet dropped (GEOIP)" action="drop" fwrule="60019" initf="eth0" srcmac="3c:1e:04:92:6f:fb" dstmac="00:50:56:97:7c:af" srcip="185.53.91.50" dstip="192.168.50.10" proto="6" length="44" tos="0x00" prec="0x00" ttl="235" srcport="54522" dstport="5038" tcpflags="SYN"
I've wrote awk '{print $15}' to extract srcip but the problem is srcip position not same in each line. how can i extract srcip=x.x.x.x without position of that?
With any sed in any shell on every UNIX box:
$ sed -n 's/.*\(srcip="[^"]*"\).*/\1/p' file
srcip="185.53.91.50"
The following command provides the result you expect
grep -o -P 'srcip="(\d{1,3}[.]){3}\d{1,3}"' log
The option o is to print only the matched parts. The option P is to use perl-compatible regular expressions. The regex is matching srcip=<ipv4> and log is the name of the file you want to extract content from.
Here is a link to regex101 for an explanation for the regex: https://regex101.com/r/hjuZlM/2
An awk version
awk -F"srcip=" '{split($2,a," ");print FS a[1]}' file
srcip="185.53.91.50"
Split the line using the key word, then get the next field after split.

Comment out file paths in a file matching lines in another file with sed and bash

I have a file (names.txt) with the following content:
/bin/pgawk
/bin/zsh
/dev/cua0
/dev/initctl
/root/.Xresources
/root/.esd_auth
... and so on. I want to read this file line by line, and use sed to comment out matches in another file. I have the code below, but it does nothing:
#/bin/bash
while read line
do
name=$line
sed -e '/\<$name\>/s/^/#/' config.conf
done < names.txt
Lines in the input file needs to be commented out in config.conf file. Like follows:
config {
#/bin/pgawk
#/bin/zsh
#/dev/cua0
#/dev/initctl
#/root/.Xresources
#/root/.esd_auth
}
I don't want to do this by hand, because the file contains more then 300 file paths. Can someone help me to figure this out?
You need to use double quotes around your sed command, otherwise shell variables will not be expanded. Try this:
sed "/\<$name\>/s/^/#/" config.conf
However, I would recommend that you skip the bash for-loop entirely and do the whole thing in one go, using awk:
awk 'NR==FNR{a[$0];next}{for(i=1;i<=NF;++i)if($i in a)$i="#"$i}1' names.txt config.conf
The awk command stores all of the file names as keys in the array a and then loops through every word in each line of the config file, adding a "#" before the word if it is in the array. The 1 at the end means that every line is printed.
It is better not to use regular expression matching here, as some of the characters in your file names (such as .) will be interpreted by the regular expression engine. This approach does a simple string match, which avoids the problem.

awk: how to include file names when concatenating files?

Am running GNUwin32 under windows 7.
Have many files in a single directory with file names that look like this:
chem.001.txt
chem.002.b4.txt
chem.003.md6.txt
(more files.txt) ...
In their current form, none of the files includes the file name.
Need to clean these files for further use.
Want to concatenate all files into a single file.
But also need to include the file name at the beginning of concatenated content to later associate the original file with clean data.
For example, the single, concatenated file (new_file.txt) would look like this:
chem.001.txt delimiter (could be a tab or pipe) followed by text from chem.001.txt...
chem.002.b4.txt delimiter followed by text from chem.002.b4.txt ...
chem.003.md6.txt delimiter followed by text from chem.003.md6.txt ...
etc. ...
Will then clean the concatenated file and parse content as needed.
awk - gawk may have a means to associate the file name with ($1), associate the text in the file with ($2) and then, in sequence, print ($1, $2) for each file into 'new_file.txt' but I've not been able to make it work.
How to do this?
Put this in foo.awk:
BEGIN{ RS="^$"; ORS=""; OFS="|" }
{ gsub(/\n[\r]?/," "); print FILENAME, $0 > "new_file.txt" }
and then execute it as
awk -f foo.awk <files>
where <files> is however you provide a list of file names in Windows. It uses GNU awk for multi-char RS to let you read a whole file as a single record.

in a text file that is a list of paths, insert directory immediately before files with certain extension

I have a list of files in files.txt, a hugely simplified example
$FOO%foo\bar\biz.asmx
%FOO%foo\bar\biz.cs
%FOO%baz\bar\foo\biz.asmx
It is my desire to insert App_Code in the path of .asmx files like:
$FOO%foo\bar\App_code\biz.asmx
%FOO%foo\bar\biz.cs
%FOO%baz\bar\foo\App_Code\biz.asmx
Though I'm on a windows box I have gnuwin32, which gives me sed/awk/grep and other fancy stuff.
I'm not wedded to a particular solution, but am interested in the sed/awk route for my on enlightenment
I have tried:
sed "s/\\([:alnum:]*)\.asmx/App_Code\/{1}/"
which I had thought would capture any alphanumeric characters after a path separator (filename) that are followed by .asmx, and then replace it with `App_Code{contents of group}.
Something is off as it never finds what I want. I'm strugging with the docs and examples, advice and guidance would be appreciated.
Quoting on Windows is a pain so put the following script into a file called appcode.awk:
BEGIN {
FS=OFS="\\"
}
$NF~/[.]asmx/{
$NF = "App_code" OFS $NF
}
{
print
}
And run like:
$ awk -f appcode.awk file
$FOO%foo\bar\App_code\biz.asmx
%FOO%foo\bar\biz.cs
%FOO%baz\bar\foo\App_code\biz.asmx
Using awk
awk -F\\ '/\.asmx/ {$NF="App_Code\\"$NF}1' OFS=\\ file
$FOO%foo\bar\App_Code\biz.asmx
%FOO%foo\bar\biz.cs
%FOO%baz\bar\foo\App_Code\biz.asmx
Using sed:
sed -r 's/(\\\w+\.asmx)/\\App_Code\1/' files.txt
Output:
$FOO%foo\bar\App_Code\biz.asmx
%FOO%foo\bar\biz.cs
%FOO%baz\bar\foo\App_Code\biz.asmx
EDIT
As suggested in by sudo_O, capture group can be dropped and & can be used in the same command.
sed -r 's/\\\w+\.asmx/\\App_Code&/' files.txt