Unix command for replacing strings which have "/" [duplicate] - unix

This question already has answers here:
Using different delimiters in sed commands and range addresses
(3 answers)
Closed 4 years ago.
I know that for replacing a string (in a file which has matchstring string), we can use following command
grep -rl matchstring somedir/ | xargs sed -i 's/string1/string2/g'
How can I use/change the command if my string has special characters like "/"?
For example:
string1: "/home/folder1"
string2: "/home/folder1/folder2"

As #jamieguinan mentioned in his command, almost any delimiter character can be used. So, I changed the command as following: grep -rl matchstring somedir/ | xargs sed -i 's,string1,string2,g' Where string1 and string2 are: /home/folder1 and /home/folder1/folder2, respectively.

Related

Unix substitute multiple strings using a reference file [duplicate]

This question already has answers here:
Match exact word with awk on Mac OS X
(4 answers)
Closed 11 months ago.
I have a reference file and using that I want to replace multiple files in a directory. I am using AWK GSUB for that, however it is not replacing exact word, but replacing all occurrences. How can I stop that behaviour? How can I replace just the word? in this case the word is "IT"
My reference file
$ cat dev_to_prod.config
nonprod_DATA_PATH PROD_DATA_PATH
nonprod_ENCRYPTKEY PROD_ENCRYPTKEY
IT Business
My current data file
$ cat filefile.txt
IT
WITH
/IT/DFGh/erfe
/WITH/IT/sjfgh/hjIT/dfdsf/ITvjkl
Output with current code
awk 'FNR==NR{A[$1]=$2;next}{for(i in A)gsub(i,A[i])}1' dev_to_prod.config file.txt
Business
WBusinessH
/Business/DFGh/erfe
/WBusinessH/Business/sjfgh/hjBusiness/dfdsf/Businessvjkl
man awk says:
\< matches the empty string at the beginning of a word.
\> matches the empty string at the end of a word.
Then would you please try:
awk 'FNR==NR{A[$1]=$2;next}{for(i in A)gsub("\\<"i"\\>",A[i])}1' dev_to_prod.config file.txt
Output:
Business
WITH
/Business/DFGh/erfe
/WITH/Business/sjfgh/hjIT/dfdsf/ITvjkl

Passing Variable to sed [duplicate]

This question already has answers here:
sed substitution with Bash variables
(6 answers)
Closed 2 years ago.
I'm having trouble passing the variable $APN to sed, tried lots of combinations of ' " \ \
First column of .csv files is a number, this code works:
cat *.csv > "$APN".csv #combine
sort -u "$APN".csv -o "$APN".csv #sort, remove duplicate APNs
sed -i '/^715/!d' combined2.csv #remove not 715
This code third line does nothing:
cat *.csv > "$APN".csv #combine
sort -u "$APN".csv -o "$APN".csv #sort, remove duplicate APNs
sed -i '/^$APN/!d' #remove not $APN
The variables inside single quotes are not expanded, you need double quotes.
sed -i "/^$APN/!d" "${APN}.csv"
Learn how to quote properly in shell, it's very important :
"Double quote" every literal that contains spaces/metacharacters and every expansion: "$var", "$(command "$var")", "${array[#]}", "a & b". Use 'single quotes' for code or literal $'s: 'Costs $5 US', ssh host 'echo "$HOSTNAME"'. See
http://mywiki.wooledge.org/Quotes
http://mywiki.wooledge.org/Arguments
http://wiki.bash-hackers.org/syntax/words

AWK to check a string pattern and extract it from a file [duplicate]

This question already has answers here:
How to print matched regex pattern using awk?
(9 answers)
How to print regexp matches using awk? [duplicate]
(3 answers)
Closed 7 years ago.
Below are the file contents:
{30001002|XXparameter|XSD_LOC|$\{FILES_DIR\}/xsd/EDXFB_mbr_demo.xsd|3|2|$|#{0|}}
{30001002|XXparameter|source_files|$XSD/EDXFB_mbr_demo.xsd|3|1|l|#{0|}}
I trying to accomplish below using awk:
Firstly I want to search for string Pattern "EDXFB*.xsd".
If exists, then extract the strings that starts with "EDXFB" and ends with ".xsd"
Output:
EDXFB_mbr_demo.xsd
EDXFB_mbr_demo.xsd
The basic awk pattern to extract the expression and print out matched data is following:
gawk 'match($0, /EDXFB.+\.xsd/, a) { print a[0] }'
Though, you should really spend some time reading awk manual.
And the regular expression could be changed to /EDXFB[a-z_]+\.xsd/ if it contains only lower-cased characters and _.
[EDIT]: Updated with cleaner code from #JID. Thanks :)
Here is one way to do it:
awk -F/ '/EDXFB.*\.xsd/ {split($NF,a,"|");print a[1]}' file
EDXFB_mbr_demo.xsd
EDXFB_mbr_demo.xsd
It separate the line by / then print last field until |
In your example, probably grep would do what you want:
grep -o 'EDXFB.*\.xsd'

passing variable to this particular sed command [duplicate]

This question already has answers here:
bash sed fail in while loop
(2 answers)
Closed 8 years ago.
I am first searching for a key word and once that key word is found in a file from that particular line i am supposed delete till end of file.
#! /bin/csh -f
set sa = `grep -n -m 1 "^Pattern" file`
set s = `echo "$sa" | cut -d':' -f1`
set m = `sed '$s,$d' file | tee see > /dev/null`
so first line gives me the matching line with line number, second line i am getting the line number and third line i am trying to delete from line $s say 20 till last but it is not working. I have tried all combinations but it does not take the variable $s. Please help.
But you can do it much more easier with a single line of sed:
sed -n '/SEARCHPATTERN/q;p
-n tells to not print the lines
/SEARCHPATTERN/q exits on search pattern
;p otherwise print the lines
You need to take $s out of the quotes so it will be expanded.
set m = `sed $s',$d' file | tee see > /dev/null`

find and replace using sed in unix [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Use slashes in sed replace
I need to find the following string /a/b/c and replace it with /r/s/t
It is a string and not a folder
/sam/pam/nancy --> /tim/cook/iphone
I am in the directory and just need to update multiple files having this line.
Use sed to change the files in-place. For example:
sed -i 's|/a/b/c|/r/s/t|g' *.txt
perl -pi -e 's/\/a\/b\/c/\/r\/s\/t/g' file_name

Resources