Unix substitute multiple strings using a reference file [duplicate] - unix

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

Related

substitution operator :s in VIM in unix [duplicate]

This question already has an answer here:
How to include forward slash in vi search & replace
(1 answer)
Closed 12 months ago.
In unix, while using VIM and the substitue command I am unable to replace the value to a string containing special character /
:%s/var1/"ab1/ab2/ab3"/g
I need to replace the entire file containing var1 with ab1/ab2/ab3
Output in vim I see: Trailing characters
Escape both / like so \/.....

Show unique filename only on command grep result [duplicate]

This question already has answers here:
How can I use grep to show just filenames on Linux? [closed]
(3 answers)
Closed 4 years ago.
I'm trying to show only unique filenames when I grep a certain string. Currently I'm getting multiple results with the same filename if a certain string appear several times inside a file.
for example:
If I have a string "string12345" and it appears 3 times in several lines inside filename1.txt and appear 3 times in several lines inside filename2.txt as well when I use *grep 'string12345' .txt it shows 3 occurrences of filename1.txt and filename2.txt
Now what I'm trying to achieve is to show only 1 occurrence of filename1.txt and filename2.txt. Thank you in advance.
use the -l flag.
test.txt:
Hello, World!
Hello, World!
Grep search:
$ grep ./ -re "Hello"
./test.txt:Hello, World!
./test.txt:Hello, World!
$ grep ./ -re "Hello" -l
./test.txt
From the manual:
-l, --files-with-matches
Suppress normal output; instead print the name of each input file from which output would normally have been printed. The scanning will stop on the first match.

How to check for special characters in UNIX? [duplicate]

This question already has answers here:
Regex, every non-alphanumeric character except white space or colon
(11 answers)
Closed 3 years ago.
I have a fixed width file with 10-15 columns. The file contains alphanumeric values. How do I check for any special characters (like !,#,#,$,% etc.) in the entire file in UNIX ?
try this;
grep -vn "^[a-zA-Z0-9]*$" yourFile
or
grep -vn "^[[:alnum:]]*$" yourFile
man grep :
-v, --invert-match
Invert the sense of matching, to select non-matching lines.
-n, --line-number
Prefix each line of output with the 1-based line number within its input file. (-n is specified by POSIX.)
[[:alnum:]] means the character class of numbers and letters in the
current locale

Unix - sed get value from a line after a first colon [duplicate]

This question already has answers here:
Extract one word after a specific word on the same line [duplicate]
(4 answers)
Closed 6 years ago.
I have a file (newline.txt) that contains the following line
Footer - Count: 00034300, Facility: TRACE, File Created: 20160506155539
I am trying to get the value after Count: up to the comma (in the example 00034300) from this line.
I tried this but I get is all the numbers concatenated into one large string with that command:
grep -i "Count:" newfile.txt | sed 's/[^0-9]//g'
output:0003430020160506155539
how do I get just the digits after Count: up to to the first non-digit character?
I just need 00034300.
Using sed
$ sed '/[Cc]ount/ s/[^:]*: *//; s/,.*//' newline.txt
00034300
How it works:
/[Cc]ount/ selects lines containing Count or count. This eliminates the need for grep.
s/[^:]*: *// removes everything up to the first colon including any spaces after the colon.
In what remains, s/,.*// removes everything after the first comma.
Using awk
$ awk -F'[[:blank:],]' '/[Cc]ount/ {print $4}' newline.txt
00034300
How it works:
-F'[[:blank:],]' tells awk to treat spaces, tabs, and commas as field separators.
/[Cc]ount/ selects lines that contain Count or count.
print $4 prints the fourth field on the selected lines.
Using grep
$ grep -oiP '(?<=Count: )[[:digit:]]+' newline.txt
00034300
This looks for any numbers following Count: and prints them.

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'

Resources