TEXT processing with awk [closed]

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty margin-bottom:0;







up vote
-2
down vote

favorite












I have text like below:



select 112,department,112,client,112,place from table where id=1 and 112,cgid and 113,evid


I need to write into a file as



112,department
112,client
112,place
112,cgid
113,evid


with each item on its own line.



I tried awk -F '[]' 'print $2' but it prints only the first occurrence and only the data without . Adding suffix and the prefix gives a syntax error?



Additional requirement



  • to replace va1,val2 in the query with a single value which I'll get after querying a table using val1 and val2.






share|improve this question













closed as unclear what you're asking by steve, Kusalananda, Jesse_b, G-Man, Isaac Jul 27 at 21:34


Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.














  • Is this a typo? 112,cgid and  












up vote
-2
down vote

favorite












I have text like below:



select 112,department,112,client,112,place from table where id=1 and 112,cgid and 113,evid


I need to write into a file as



112,department
112,client
112,place
112,cgid
113,evid


with each item on its own line.



I tried awk -F '[]' 'print $2' but it prints only the first occurrence and only the data without . Adding suffix and the prefix gives a syntax error?



Additional requirement



  • to replace va1,val2 in the query with a single value which I'll get after querying a table using val1 and val2.






share /g' file






share|improve this answer















share|improve this answer



share|improve this answer








edited Jul 27 at 8:25


























answered Jul 27 at 8:20









steve

11.9k22047




11.9k22047











  • thank u.. this worked but this pattern may occur anywhere in the query.. not only i the select..
    – komz
    Jul 27 at 9:00
















  • thank u.. this worked but this pattern may occur anywhere in the query.. not only i the select..
    – komz
    Jul 27 at 9:00















thank u.. this worked but this pattern may occur anywhere in the query.. not only i the select..
– komz
Jul 27 at 9:00




thank u.. this worked but this pattern may occur anywhere in the query.. not only i the select..
– komz
Jul 27 at 9:00










up vote
0
down vote













With perl:



perl -lne 'print for /.*?(?=.*from S+ where)/g'


With GNU grep or compatible:



grep -Po '.*?(?=.*from S+ where)'


With GNU awk:



gawk -v FPAT='[][][^]*[][}]' '
sub(/from [[:space:]]+ where.*/, "")
for (i = 1; i <= NF; i++) print $i'


For all, you can feed the input on stdin or as a filename argument.






share|improve this answer























  • i am sorry. to ask this... how can i specify the input file in the above commands.. i m totally new to this
    – komz
    Jul 27 at 9:03










  • Add it as file arg. to them like grep .... <file>.
    – slm♦
    Jul 27 at 9:04











  • perl -lne 'print for /.*?/g' input.hql. am i right ? this didnt work
    – komz
    Jul 27 at 9:08










  • For the perl one do this cat <file> | perl ...
    – slm♦
    Jul 27 at 9:12










  • cat input.hql | perl -lne 'print for /.*?/g' this didnt give any o/p.. shd v capture the o/p for display?
    – komz
    Jul 27 at 9:16














up vote
0
down vote













With perl:



perl -lne 'print for /.*?(?=.*from S+ where)/g'


With GNU grep or compatible:



grep -Po '.*?(?=.*from S+ where)'


With GNU awk:



gawk -v FPAT='[][][^]*[][}]' '
sub(/from [[:space:]]+ where.*/, "")
for (i = 1; i <= NF; i++) print $i'


For all, you can feed the input on stdin or as a filename argument.






share|improve this answer























  • i am sorry. to ask this... how can i specify the input file in the above commands.. i m totally new to this
    – komz
    Jul 27 at 9:03










  • Add it as file arg. to them like grep .... <file>.
    – slm♦
    Jul 27 at 9:04











  • perl -lne 'print for /.*?/g' input.hql. am i right ? this didnt work
    – komz
    Jul 27 at 9:08










  • For the perl one do this cat <file> | perl ...
    – slm♦
    Jul 27 at 9:12










  • cat input.hql | perl -lne 'print for /.*?/g' this didnt give any o/p.. shd v capture the o/p for display?
    – komz
    Jul 27 at 9:16












up vote
0
down vote










up vote
0
down vote









With perl:



perl -lne 'print for /.*?(?=.*from S+ where)/g'


With GNU grep or compatible:



grep -Po '.*?(?=.*from S+ where)'


With GNU awk:



gawk -v FPAT='[][][^]*[][}]' '
sub(/from [[:space:]]+ where.*/, "")
for (i = 1; i <= NF; i++) print $i'


For all, you can feed the input on stdin or as a filename argument.






share|improve this answer















With perl:



perl -lne 'print for /.*?(?=.*from S+ where)/g'


With GNU grep or compatible:



grep -Po '.*?(?=.*from S+ where)'


With GNU awk:



gawk -v FPAT='[][][^]*[][}]' '
sub(/from [[:space:]]+ where.*/, "")
for (i = 1; i <= NF; i++) print $i'


For all, you can feed the input on stdin or as a filename argument.







share|improve this answer















share|improve this answer



share|improve this answer








edited Jul 27 at 9:20


























answered Jul 27 at 8:58









Stéphane Chazelas

278k52512842




278k52512842











  • i am sorry. to ask this... how can i specify the input file in the above commands.. i m totally new to this
    – komz
    Jul 27 at 9:03










  • Add it as file arg. to them like grep .... <file>.
    – slm♦
    Jul 27 at 9:04











  • perl -lne 'print for /.*?/g' input.hql. am i right ? this didnt work
    – komz
    Jul 27 at 9:08










  • For the perl one do this cat <file> | perl ...
    – slm♦
    Jul 27 at 9:12










  • cat input.hql | perl -lne 'print for /.*?/g' this didnt give any o/p.. shd v capture the o/p for display?
    – komz
    Jul 27 at 9:16
















  • i am sorry. to ask this... how can i specify the input file in the above commands.. i m totally new to this
    – komz
    Jul 27 at 9:03










  • Add it as file arg. to them like grep .... <file>.
    – slm♦
    Jul 27 at 9:04











  • perl -lne 'print for /.*?/g' input.hql. am i right ? this didnt work
    – komz
    Jul 27 at 9:08










  • For the perl one do this cat <file> | perl ...
    – slm♦
    Jul 27 at 9:12










  • cat input.hql | perl -lne 'print for /.*?/g' this didnt give any o/p.. shd v capture the o/p for display?
    – komz
    Jul 27 at 9:16















i am sorry. to ask this... how can i specify the input file in the above commands.. i m totally new to this
– komz
Jul 27 at 9:03




i am sorry. to ask this... how can i specify the input file in the above commands.. i m totally new to this
– komz
Jul 27 at 9:03












Add it as file arg. to them like grep .... <file>.
– slm♦
Jul 27 at 9:04





Add it as file arg. to them like grep .... <file>.
– slm♦
Jul 27 at 9:04













perl -lne 'print for /.*?/g' input.hql. am i right ? this didnt work
– komz
Jul 27 at 9:08




perl -lne 'print for /.*?/g' input.hql. am i right ? this didnt work
– komz
Jul 27 at 9:08












For the perl one do this cat <file> | perl ...
– slm♦
Jul 27 at 9:12




For the perl one do this cat <file> | perl ...
– slm♦
Jul 27 at 9:12












cat input.hql | perl -lne 'print for /.*?/g' this didnt give any o/p.. shd v capture the o/p for display?
– komz
Jul 27 at 9:16




cat input.hql | perl -lne 'print for /.*?/g' this didnt give any o/p.. shd v capture the o/p for display?
– komz
Jul 27 at 9:16










up vote
0
down vote













I'd use grep rather than awk. Here the -P flag asks grep to use PCRE regular expressions rather than its default (so that .*? matches as little as possible), and the -o flag gets grep to print only matching components:



grep -Po '.*?'


Example:



grep -Po '.*?' <<<'select 112,department,112,client,112,place from table where id=1 and 112,cgid and 113,evid'
112,department
112,client
112,place
112,cgid
113,evid


There is no va1,val2 to match in your query so the "additional requirement" is moot. Or are you generalising to the matched entities such as 112,department? If so, you need to explain what is to be done, because to replace [them] in the query with a single value which I'll get [later] is too broad a requirement to address with explicit code.






share|improve this answer





















  • thank u this worked... now how can i capture the first value from . like 112 from 112,department. i tried var1=echo $line | awk -F '[]' 'print $2' | awk -F '[,]' 'print $1' its not working. the same command works for single
    – komz
    Aug 1 at 4:21














up vote
0
down vote













I'd use grep rather than awk. Here the -P flag asks grep to use PCRE regular expressions rather than its default (so that .*? matches as little as possible), and the -o flag gets grep to print only matching components:



grep -Po '.*?'


Example:



grep -Po '.*?' <<<'select 112,department,112,client,112,place from table where id=1 and 112,cgid and 113,evid'
112,department
112,client
112,place
112,cgid
113,evid


There is no va1,val2 to match in your query so the "additional requirement" is moot. Or are you generalising to the matched entities such as 112,department? If so, you need to explain what is to be done, because to replace [them] in the query with a single value which I'll get [later] is too broad a requirement to address with explicit code.






share|improve this answer





















  • thank u this worked... now how can i capture the first value from . like 112 from 112,department. i tried var1=echo $line | awk -F '[]' 'print $2' | awk -F '[,]' 'print $1' its not working. the same command works for single
    – komz
    Aug 1 at 4:21












up vote
0
down vote










up vote
0
down vote









I'd use grep rather than awk. Here the -P flag asks grep to use PCRE regular expressions rather than its default (so that .*? matches as little as possible), and the -o flag gets grep to print only matching components:



grep -Po '.*?'


Example:



grep -Po '.*?' <<<'select 112,department,112,client,112,place from table where id=1 and 112,cgid and 113,evid'
112,department
112,client
112,place
112,cgid
113,evid


There is no va1,val2 to match in your query so the "additional requirement" is moot. Or are you generalising to the matched entities such as 112,department? If so, you need to explain what is to be done, because to replace [them] in the query with a single value which I'll get [later] is too broad a requirement to address with explicit code.






share|improve this answer













I'd use grep rather than awk. Here the -P flag asks grep to use PCRE regular expressions rather than its default (so that .*? matches as little as possible), and the -o flag gets grep to print only matching components:



grep -Po '.*?'


Example:



grep -Po '.*?' <<<'select 112,department,112,client,112,place from table where id=1 and 112,cgid and 113,evid'
112,department
112,client
112,place
112,cgid
113,evid


There is no va1,val2 to match in your query so the "additional requirement" is moot. Or are you generalising to the matched entities such as 112,department? If so, you need to explain what is to be done, because to replace [them] in the query with a single value which I'll get [later] is too broad a requirement to address with explicit code.







share|improve this answer













share|improve this answer



share|improve this answer











answered Jul 27 at 17:49









roaima

39.2k544105




39.2k544105











  • thank u this worked... now how can i capture the first value from . like 112 from 112,department. i tried var1=echo $line | awk -F '[]' 'print $2' | awk -F '[,]' 'print $1' its not working. the same command works for single
    – komz
    Aug 1 at 4:21
















  • thank u this worked... now how can i capture the first value from . like 112 from 112,department. i tried var1=echo $line | awk -F '[]' 'print $2' | awk -F '[,]' 'print $1' its not working. the same command works for single
    – komz
    Aug 1 at 4:21















thank u this worked... now how can i capture the first value from . like 112 from 112,department. i tried var1=echo $line | awk -F '[]' 'print $2' | awk -F '[,]' 'print $1' its not working. the same command works for single
– komz
Aug 1 at 4:21




thank u this worked... now how can i capture the first value from . like 112 from 112,department. i tried var1=echo $line | awk -F '[]' 'print $2' | awk -F '[,]' 'print $1' its not working. the same command works for single
– komz
Aug 1 at 4:21


Popular posts from this blog

How to check contact read email or not when send email to Individual?

Bahrain

Postfix configuration issue with fips on centos 7; mailgun relay