use any unix/linux commands(awk) ,input is like : 78/WESDA.99/C/694883/O///BFPC0T/C/BFPC0T X/C - awk

use any unix/linux commands(awk) ,input is like :
78/WESDA.99/C/694883/O///BFPC0T/C/BFPC0T X/C
out put should be:
78,WESDA.99/C,694883,O,,,BFPC0T,C,BFPC0T X,C

you can use one of these;
sed 's|/|,|g' yourFile
tr '/' ',' yourFile
awk '{gsub(/\//,",",$0);print $0}' yourFile
Test;
$ echo "78/WESDA.99/C/694883/O///BFPC0T/C/BFPC0T X/C " | sed 's|/|,|g'
78,WESDA.99,C,694883,O,,,BFPC0T,C,BFPC0T X,C
$ echo "78/WESDA.99/C/694883/O///BFPC0T/C/BFPC0T X/C " | tr '/' ','
78,WESDA.99,C,694883,O,,,BFPC0T,C,BFPC0T X,C

Related

How do I simultaneously replace ":" and ";" with a newline?

Background
The tr command translates, delete, or squeezes characters from standard input. I am trying to create a cshell alias that outputs the setenv command such that each file or directory in the path is displayed on a new line.
In other words, I want this example output of setenv:
PATH=/some/dir:/some/second/dir:/some/third/dir
PROFILES= file:///some/file/here.txt;file:///some/second/file.xml;file:///some/third/file/there.pl
To become this:
PATH= /some/dir
/some/second/dir
/some/third/dir
PROFILES= file:///some/file/here.txt
file:///some/second/file.xml
file:///some/third/file/there.pl
Related Aliases that work
I have created aliases that output the individual environment variables like above:
For PATH:
alias readablePath = 'echo "$PATH" | tr : '\'\\\n\'' '
For PROFILES:
alias readableProfiles= 'echo "$PROFILES" | tr \; 'echo "$PATH" | tr : '\'\\\n\'' '
Attempted aliases that do not work
These are the aliases that I have tried that return tr: no match.
alias readEnv 'echo "setenv" | tr [:\;] '\'\\\n\'' '
alias readEnv 'echo setenv | tr [:\;] '\'\\\n\'' '
alias readEnv 'echo setenv | tr [:\\;] '\'\\\n\'' '
alias readEnv 'echo setenv | tr [:;] '\'\\\n\'' '
These are the aliases that I have tried that return the string "setenv".
alias readEnv 'echo setenv | tr "[:;]" '\'\\\n\'' '
alias readEnv 'echo "setenv" | tr "[:;]" '\'\\\n\'' '
Question
How do I create an alias that successfully takes the output of setenv and inserts a newline where there is a ":" or a ";"?
To replace multiple characters in your (unix/linux) command line alias, you should rather use sed and regular expressions, as in Search and replace with sed when dots and underscores are present .
So something like:
sed 's/\.\|,/\\n/g'
where \. finds a dot (needs to be escaped, else it finds any character), \| is the or operation, also escaped, and the new line also must have its \\ escaped.

ANTLR4 input grammar

How can I write this grammar expression for ANTLR4 input?
Originally expression:
<int_literal> = 0|(1 -9){0 -9}
<char_literal> = ’( ESC |~( ’|\| LF | CR )) ’
<string_literal> = "{ ESC |~("|\| LF | CR )}"
I tried the following expression:
int_literal : '0' | ('1'..'9')('0'..'9')*;
char_literal : '('ESC' | '~'('\'|'''|'LF'|'CR'))';
But it returned:
syntax error: '\' came as a complete surprise to me
syntax error: mismatched input ')' expecting SEMI while matching a rule
unterminated string literal
Your quotes don't match:
'('ESC' | '~'('\'|'''|'LF'|'CR'))'
^ ^ ^ ^ ^ ^ ^
| | | | | | |
o c o c o c error
o is open, c is close
I'd read "{ ESC |~("|\| LF | CR )}" as this:
// A string literal is zero or more chars other than ", \, \r and \n
// enclosed in double quotes
StringLiteral
: '"' ( Escape | ~( '"' | '\\' | '\r' | '\n' ) )* '"'
;
Escape
: '\\' ???
;
Also note that ANTLR4 has short hand char classes ([0-9] equals '0'..'9'), so you can do this:
IntLiteral
: '0'
| [1-9] [0-9]*
;
StringLiteral
: '"' ( Escape | ~["\\\r\n] )* '"'
;
Also not that lexer rules start with an uppercase letter! Otherwise they become parser rules (see: Practical difference between parser rules and lexer rules in ANTLR?).

Sed inside an awk statement

I'd like to perform a series of sed commands on lines of a file roster.txt only beginning with a keyword. For example:
Employee : Kiara 20 hours#8.25
Employee : Connor 25 hours#8.00
Employee : Dylan 30 hours#9.00
Becomes:
Employee : Kiara_20_hoursat8dot25
Employee : Connor_25_hoursat8dot00
Employee : Dylan_30_hoursat9dot00
I know the sed commands to make the changes I just wanted a way to peform them on lines starting with "employee". Maybe
awk '$1 == "Employee" {sed -i -e 's/\./dot/g' roster.txt}' roster.txt
$ cat roster.txt
foo : bar#baz.123
Employee : Kiara 20 hours#8.25
Employee : Connor 25 hours#8.00
Employee : Dylan 30 hours#9.00
$ awk 'BEGIN{FS=OFS=" : "} $1=="Employee"{gsub(/ /,"_",$2); gsub(/#/,"at",$2); gsub(/\./,"dot",$2)} 1' roster.txt
foo : bar#baz.123
Employee : Kiara_20_hoursat8dot25
Employee : Connor_25_hoursat8dot00
Employee : Dylan_30_hoursat9dot00
awk supports substitution commands as well - sub to replace first occurrence and gsub to replace all occurrences. Also allows to change only specific field
BEGIN{FS=OFS=" : "} use : as input/output field separator
gsub(/ /,"_",$2) replace all spaces with _ only for second field
Similarly other substitutions as required
1 at end of command is idiomatic way to print the line, includes any changes made
See also awk save modifications in place
I'd write:
sed '/^Employee :/ {s/#/at/; s/\./dot/; s/ /_/3g}' <<END
Employee : Kiara 20 hours#8.25
Employee : Connor 25 hours#8.00
Employee : Dylan 30 hours#9.00
Foo : bar baz#qux.blah
END
Employee : Kiara_20_hoursat8dot25
Employee : Connor_25_hoursat8dot00
Employee : Dylan_30_hoursat9dot00
Foo : bar baz#qux.blah
Requires GNU sed for the 3g modifier of the s command
You really do not need to use both tools, either will do everything you need.
sed solution:
sed -i -e 's/^Employee : \([^ ]*\) \([0-9]*\) hours#\([0-9]\)\.\([0-9]*\)/Employee : \1_\2_hoursat\3dot\4/' roster.txt
Edit after comment:
If you want a very generic replacement using only sed that works on your sample:
sed -i -e 's/\([^:]\) \([^:]\)/\1_\2/g' -e 's/#/at/g' -e 's/\./dot/g' roster.txt

Parsing multiline strings with antlr

I have a grammar that looks like this:
a: b c d ;
b: x STRING y ;
where
STRING: '"' (~('"' | '\\' | '\r' | '\n') | '\\' ('"' | '\\'))* '"';
And my file contains one 'a' production in each line so I'm currently dropping all newlines. I would however want to parse multiline strings, how can I do that? It doesn't work if I just allow '\r' and '\n' inside the string.
IIUC, you are just looking for a multi-line string lexer rule. The fact that you are dropping newlines really does not affect the construction of the string rule. The newlines that match within the string rule will be consumed there before the lexer ever considers the whitespace rule.
STRING : DQUOTE ( STR_TEXT | EOL )* DQUOTE ;
WS : [ \t\r\n] -> skip;
fragment STR_TEXT: ( ~["\r\n\\] | ESC_SEQ )+ ;
fragment ESC_SEQ : '\\' ( [btf"\\] | EOF )
fragment DQUOTE : '"' ;
fragment EOL : '\r'? '\n' ;

using variables in awk print

The contents of myfile.txt file are as follows:
| dbname |
| dbname1 |
| dbname2 |
The following command is expected to generate the output as shown below:
cat myfile.txt | awk '{print "mysql -uroot -Bse \"call mysql.p_check_fk_constraint_violations('\'$2\'','\''%'\'')\""}'
Expected Output:
mysql -uroot -Bse "call mysql.p_check_fk_constraint_violations('dbname','%')"
mysql -uroot -Bse "call mysql.p_check_fk_constraint_violations('dbname1','%')"
mysql -uroot -Bse "call mysql.p_check_fk_constraint_violations('dbname2','%')"
But the actual output is:
mysql -uroot -Bse "call mysql.p_check_fk_constraint_violations('','%')"
How do I add the database names in awk statement?
This should do it -
[jaypal~/Temp]$ cat db.file
| dbname |
| dbname1 |
| dbname2 |
Here we are substituting the second field with your text and using "&" matches the field getting substituted.
[jaypal~/Temp]$ awk -F\| '{sub($2,"mysql \-uroot \-Bse \"call mysql.p_check_fk_constraint_violations\(&,\%\)\""); print $2}' db.file
mysql -uroot -Bse "call mysql.p_check_fk_constraint_violations( dbname ,%)"
mysql -uroot -Bse "call mysql.p_check_fk_constraint_violations( dbname1 ,%)"
mysql -uroot -Bse "call mysql.p_check_fk_constraint_violations( dbname2 ,%)"
Alternatively as Teudimundo suggested, you can do -
[jaypal~/Temp]$ cat db.file | awk '{print "mysql -uroot -Bse \"call mysql.p_check_fk_constraint_violations("$2",'%')\""}'
mysql -uroot -Bse "call mysql.p_check_fk_constraint_violations(dbname,%)"
mysql -uroot -Bse "call mysql.p_check_fk_constraint_violations(dbname1,%)"
mysql -uroot -Bse "call mysql.p_check_fk_constraint_violations(dbname2,%)"
UPDATE
[jaypal~/Temp]$ cat db.file | awk '{print "mysql -uroot -Bse \"call mysql.p_check_fk_constraint_violations('"'"'"$2"'"'"', '"'"'%'"'"')"}'
mysql -uroot -Bse "call mysql.p_check_fk_constraint_violations('dbname', '%')
mysql -uroot -Bse "call mysql.p_check_fk_constraint_violations('dbname1', '%')
mysql -uroot -Bse "call mysql.p_check_fk_constraint_violations('dbname2', '%')
[jaypal~/Temp]$ awk '{ print $2 }' db.file | awk '{sub($1,"mysql \-uroot \-Bse \"call mysql.p_check_fk_constraint_violations\('"'"'&'"'"','"'"'%'"'"'\)\""); print $0}'
mysql -uroot -Bse "call mysql.p_check_fk_constraint_violations('dbname','%')"
mysql -uroot -Bse "call mysql.p_check_fk_constraint_violations('dbname1','%')"
mysql -uroot -Bse "call mysql.p_check_fk_constraint_violations('dbname2','%')"
here : '" $2 "' you are closing the first awk ' char, and so " $2 " is interpreted by the shell.
You're running into problems because of the single quotes, as Teudimundo said. To fix it, you need to replace each single quote ' that you'd like to embed with this '"'"', giving this awk command:
awk '{print "mysql -uroot -Bse \"call mysql.p_check_fk_constraint_violations('"'"'$2'"'"', '"'"'%'"'"')"}'
This works because the '"'"' first ends the single-quoted string for the awk command, begins a new double-quoted string containing a single quote, then starts a new single-quoted string with the rest of the awk command. Since adjacent strings are concatenated in the shell, this strange-seeming approach produces the string you need.
It suppose that it will be far better to use mysql procedure, instead of jumping in and out of mysql using shell scripting ...
I'm not familiar with mysql's procedural language, but I'm sure that if you search Internet
you can quickly come up with a simple procedure, something like this:
delimiter //
drop procedure run_proc //
create procedure run_proc()
begin
declare done boolean default 0;
declare l_db_name varchar(100);
declare cur_db_names cursor
for
select
schema_name
from
information_schema.schemata;
declare continue handler for
sqlstate '02000' set done=1;
open cur_db_names;
repeat
fetch cur_db_names into l_db_name;
call mysql.p_check_fk_constraint_violations(l_db_name,'%');
until done end repeat;
close cur_db_names;
end;
//
delimiter ;
call run_proc;