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
Related
I have a very simple grammar that looks like this:
grammar Testing;
a : d | b;
b : {_input.LT(1).equals("b")}? C;
d : {!_input.LT(1).equals("b")}? C;
C : .;
It parses one character from the input and checks whether the it's equal to the character b. If so, rule b is used, and if not, rule d is used.
However, the parse tree fails the expectation and parses everything using the first rule (rule d).
$ antlr Testing.g4
$ javac *.java
$ grun Testing a -trace (base)
c
enter a, LT(1)=c
enter d, LT(1)=c
consume [#0,0:0='c',<1>,1:0] rule d
exit d, LT(1)=
exit a, LT(1)=
$ grun Testing a -trace (base)
b
enter a, LT(1)=b
enter d, LT(1)=b
consume [#0,0:0='b',<1>,1:0] rule d
exit d, LT(1)=
exit a, LT(1)=
In both cases, rule d is used. However, since there is a guard on rule d, I expect rule d to fail when the first character is exactly 'b'.
Am I doing something wrong when using the semantic predicates?
(I need to use semantic predicates because I need to parse a language where keywords could be used as identifiers).
Reference: https://github.com/antlr/antlr4/blob/master/doc/predicates.md
_input.LT(int) returns a Token, and Token.equals(String) will always return false. What you want to do is call getText() on the Token:
b : {_input.LT(1).getText().equals("b")}? C;
d : {!_input.LT(1).getText().equals("b")}? C;
However, often it is easier to handle keywords-as-identifiers in such a way:
rule
: KEYWORD_1 identifier
;
identifier
: IDENTIFIER
| KEYWORD_1
| KEYWORD_2
| KEYWORD_3
;
KEYWORD_1 : 'k1';
KEYWORD_2 : 'k2';
KEYWORD_3 : 'k3';
IDENTIFIER : [a-zA-Z_] [a-zA-Z_0-9]*;
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.
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
I have written a grammar in antlr as follows:
grammar names;
init : stat+;
stat : name NEWLINE
| name SPACE NEWLINE
|NEWLINE
| name SPACE name SPACE
;
name : ID ;
ID : [a-zA-Z]+ ;
NEWLINE:'\r'? '\n' ;
SPACE : ' ';
this grammar should accept input of form :
name1
name1<space>name2<space>
name1<space>
I am not getting the required output. As of now the generated tree shows only the first value. I am a novice to antlr and any help would be appreciated.
You stat rule should be more like this:
stat: name (SPACE name)* SPACE? NEWLINE;
assuming from you small example that each line represents a complete entry matched by the stat rule.
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;