substitution operator :s in VIM in unix [duplicate] - unix

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 \/.....

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

There is a file some where in your system which contains word "UnixCommandInterviewQuestions” How will find that file in Unix? [duplicate]

This question already has answers here:
How to find all files containing specific text (string) on Linux?
(54 answers)
Closed 5 years ago.
This is a common $unix question thats asked in interviews. I know that you have to use find command but what is the exact answer? i searched in google but couldnt find a proper answer. below command i tried but its wrong.
$find / -name "UnixCommandInterviewQuestions”
Make sure you read the question very carefully.
It says that the file contains "UnixCommandInterviewQuestions", not that the file name is "UnixCommandInterviewQuestions".
One correct answer would be a recursive grep command, like:
grep -r UnixCommandInterviewQuestions /
There are other solutions that may be faster.

Insert a pipe separated header to a large txt file [duplicate]

This question already has answers here:
Add a header to a tab delimited file
(8 answers)
Closed 6 years ago.
I have pipe separated large file(s) that has more 10,000,000 lines. All of these files are missing header and only the first split has the header. I want insert the header line for the rest of the files.
I tried following:
sed -i '1 i \user_id|name|age|transactions' file.txt
but this didn't work. What is the most efficient way to insert a header line for large file.
Similar question has been asked, unfortunately solution didn't work. It could be due to sed structure of understanding pipe separation might be different.
Try this:
sed '1s/.*/user_id|name|age|transactions\n&/' file.txt

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

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