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

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

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

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

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'

How do I specify a glob that matches file.txt but not file.x.txt in zsh?

Let's say I have a bunch of files and one of them has .txt as the extension and others have .x.txt where x could be whatever. How do I pull out the file that only has the .txt extension ?
Here's a reproducible example:
touch file.txt file.x.txt
echo *.txt
# file.txt file.x.txt
% touch file.txt file.x.txt
% echo [^.]#.txt
file.txt
From the FILENAME GENERATION section of man zshexpn:
[...] Matches any of the enclosed characters. Ranges of characters can be specified by separating two characters by a `-'. A `-' or `]' may be matched by including it as the first character in the
list. There are also several named classes of characters, in the form `[:name:]' with the following meanings. The first set use the macros provided by the operating system to test for the given
character combinations, including any modifications due to local language settings, see ctype(3):
...
[^...]
[!...] Like [...], except that it matches any character which is not in the given set.
...
x# (Requires EXTENDED_GLOB to be set.) Matches zero or more occurrences of the pattern x. This operator has high precedence; `12#' is equivalent to `1(2#)', rather than `(12)#'. It is an error for
an unquoted `#' to follow something which cannot be repeated; this includes an empty string, a pattern already followed by `##', or parentheses when part of a KSH_GLOB pattern (for example,
`!(foo)#' is invalid and must be replaced by `*(!(foo))').
So this matches any string ending with .txt that contains no other . characters.

Resources