What grep/awk/sed command to use to the output that i want2019 Community Moderator ElectionWhat are the differences between Perl, Python, AWK and sed?How to do a recursive find/replace of a string with awk or sed?What is the difference between sed and awk?How to output only captured groups with sed?sed command find and replace in file and overwrite file doesn't work, it empties the fileInsert a line at specific line number with sed or awkWhat are the differences among grep, awk & sed?BASH: grep/awk/sed to extract variable dataHow to remove unique values from an HTML select list with linux program like sed, awk, or grep?Saved format output in columns [grep, sed, awk or ?]
Is divide-by-zero a security vulnerability?
How does insurance birth control work?
Split a number into equal parts given the number of parts
I encountered my boss during an on-site interview at another company. Should I bring it up when seeing him next time?
An Undercover Army
How can I conditionally format my HTML table?
Rationale to prefer local variables over instance variables?
Is there a math equivalent to the conditional ternary operator?
Book about a time-travel war fought by computers
How does signal strength relate to bandwidth?
Why are special aircraft used for the carriers in the United States Navy?
Quitting employee has privileged access to critical information
How to kill a localhost:8080
I've given my players a lot of magic items. Is it reasonable for me to give them harder encounters?
Lock enemy's y-axis when using Vector3.MoveTowards to follow the player
Find maximum of the output from reduce
Did Amazon pay $0 in taxes last year?
Formatting a table to look nice
Practical reasons to have both a large police force and bounty hunting network?
Why do phishing e-mails use faked e-mail addresses instead of the real one?
I can't die. Who am I?
Are there other characters in the Star Wars universe who had damaged bodies and needed to wear an outfit like Darth Vader?
Why would the IRS ask for birth certificates or even audit a small tax return?
Reason why dimensional travelling would be restricted
What grep/awk/sed command to use to the output that i want
2019 Community Moderator ElectionWhat are the differences between Perl, Python, AWK and sed?How to do a recursive find/replace of a string with awk or sed?What is the difference between sed and awk?How to output only captured groups with sed?sed command find and replace in file and overwrite file doesn't work, it empties the fileInsert a line at specific line number with sed or awkWhat are the differences among grep, awk & sed?BASH: grep/awk/sed to extract variable dataHow to remove unique values from an HTML select list with linux program like sed, awk, or grep?Saved format output in columns [grep, sed, awk or ?]
I have a input file like this:
COL1: VALUE1 , XYZ: 2, OWNER: (DSF) , FLG: DIT /-/-/ OX if 0X, proc=0xyyy23, NAME=AUDIT
COL1: VALUE2 , XYZ: 2, OWNER: (DSF) , FLG: DIT /-/-/ OX if 0X, proc=0xyy23, NAME=generic
XYZ:2, COL1: 289 , TREK:MRP, OWNER: (DSF) , FLG: DIT /-/-/ OX if 0X, NAME=Oil, trial=TREE
I want to get the output like this:
COL1: VALUE1 , NAME=AUDIT
COL1: VALUE2 , NAME=generic
COL1: 289 , NAME=Oil
How can I achieve this using awk/grep/sed
on command line without using any advanced versions of awk
like gawk
, nawk
etc?
Basically I want to get the value of COL1
(i.e. text after :
and =
) & NAME
irrespective of where they are in the line.
See that location of NAME column got slightly altered.
This is what I could come up with:
awk -F"," 'print $1, $6' file.txt
COL1: VALUE1 NAME=AUDIT
COL1: VALUE2 NAME=generic
XYZ:2 NAME=Oil
perl awk sed
add a comment |
I have a input file like this:
COL1: VALUE1 , XYZ: 2, OWNER: (DSF) , FLG: DIT /-/-/ OX if 0X, proc=0xyyy23, NAME=AUDIT
COL1: VALUE2 , XYZ: 2, OWNER: (DSF) , FLG: DIT /-/-/ OX if 0X, proc=0xyy23, NAME=generic
XYZ:2, COL1: 289 , TREK:MRP, OWNER: (DSF) , FLG: DIT /-/-/ OX if 0X, NAME=Oil, trial=TREE
I want to get the output like this:
COL1: VALUE1 , NAME=AUDIT
COL1: VALUE2 , NAME=generic
COL1: 289 , NAME=Oil
How can I achieve this using awk/grep/sed
on command line without using any advanced versions of awk
like gawk
, nawk
etc?
Basically I want to get the value of COL1
(i.e. text after :
and =
) & NAME
irrespective of where they are in the line.
See that location of NAME column got slightly altered.
This is what I could come up with:
awk -F"," 'print $1, $6' file.txt
COL1: VALUE1 NAME=AUDIT
COL1: VALUE2 NAME=generic
XYZ:2 NAME=Oil
perl awk sed
add a comment |
I have a input file like this:
COL1: VALUE1 , XYZ: 2, OWNER: (DSF) , FLG: DIT /-/-/ OX if 0X, proc=0xyyy23, NAME=AUDIT
COL1: VALUE2 , XYZ: 2, OWNER: (DSF) , FLG: DIT /-/-/ OX if 0X, proc=0xyy23, NAME=generic
XYZ:2, COL1: 289 , TREK:MRP, OWNER: (DSF) , FLG: DIT /-/-/ OX if 0X, NAME=Oil, trial=TREE
I want to get the output like this:
COL1: VALUE1 , NAME=AUDIT
COL1: VALUE2 , NAME=generic
COL1: 289 , NAME=Oil
How can I achieve this using awk/grep/sed
on command line without using any advanced versions of awk
like gawk
, nawk
etc?
Basically I want to get the value of COL1
(i.e. text after :
and =
) & NAME
irrespective of where they are in the line.
See that location of NAME column got slightly altered.
This is what I could come up with:
awk -F"," 'print $1, $6' file.txt
COL1: VALUE1 NAME=AUDIT
COL1: VALUE2 NAME=generic
XYZ:2 NAME=Oil
perl awk sed
I have a input file like this:
COL1: VALUE1 , XYZ: 2, OWNER: (DSF) , FLG: DIT /-/-/ OX if 0X, proc=0xyyy23, NAME=AUDIT
COL1: VALUE2 , XYZ: 2, OWNER: (DSF) , FLG: DIT /-/-/ OX if 0X, proc=0xyy23, NAME=generic
XYZ:2, COL1: 289 , TREK:MRP, OWNER: (DSF) , FLG: DIT /-/-/ OX if 0X, NAME=Oil, trial=TREE
I want to get the output like this:
COL1: VALUE1 , NAME=AUDIT
COL1: VALUE2 , NAME=generic
COL1: 289 , NAME=Oil
How can I achieve this using awk/grep/sed
on command line without using any advanced versions of awk
like gawk
, nawk
etc?
Basically I want to get the value of COL1
(i.e. text after :
and =
) & NAME
irrespective of where they are in the line.
See that location of NAME column got slightly altered.
This is what I could come up with:
awk -F"," 'print $1, $6' file.txt
COL1: VALUE1 NAME=AUDIT
COL1: VALUE2 NAME=generic
XYZ:2 NAME=Oil
perl awk sed
perl awk sed
edited 16 hours ago
Tiw
3,57341229
3,57341229
asked 19 hours ago
SachSach
575
575
add a comment |
add a comment |
4 Answers
4
active
oldest
votes
Could you please try following(tested and written in GNU awk
).
awk '
BEGIN
OFS=" , "
match($0,/COL[0-9]+: [^,]*/)
val=substr($0,RSTART,RLENGTH)
match($0,/NAME[^,]*/)
print val OFS substr($0,RSTART,RLENGTH)
val=""
' Input_file
I have clubbed the match(es) of string COL
and NAME
in each line so in case any line do not have string COL
in it, it may not print anything in it.
In case string COL
is not found in a line and you still want to print NAME
string match then try following.
awk '
BEGIN
OFS=" , "
match($0,/COL[0-9]+: [^,]*/)
val=substr($0,RSTART,RLENGTH)
match($0,/NAME[^,]*/)
if(val)
printf "%s%s",val,OFS
print substr($0,RSTART,RLENGTH)
' Input_file
Explanation: Adding explanation for above code now.
awk ' ##Starting awk program heer.
BEGIN ##Starting BEGIN section for awk code here.
OFS=" , " ##Setting OFS output field separator as space comma space here.
##Closing BEGIN section here.
match($0,/COL[0-9]+: [^,]*/) ##Using match of awk OOTB function to match a REGEX string COL till comma here.
val=substr($0,RSTART,RLENGTH) ##If a match is foundthen creating variable val whose value is sub string of matched regex starting to till end value of it.
match($0,/NAME[^,]*/) ##Again using match to match string from NAME to till next comma comes.
print val OFS substr($0,RSTART,RLENGTH) ##Printing value of variable val OFS and substring of current line whose sarting point is RSTART and end point is RLENGTH.
val="" ##Nullifying variable val here.
' Input_file ##Mentioning Input_file name here.
Adding reference from man awk
page:
RSTART The index of the first character matched by match(); 0 if no match. (This implies that character indices start at one.)
RLENGTH The length of the string matched by match(); -1 if no match.
1
works perfect, can you please explain it to me, will appreciate if you do
– Sach
18 hours ago
1
@Sach fyi, thisawk
solution is usinggawk
, which stands forGNU awk
.awk
andgawk
can point to same executable, and in your case, I think you are already using GNU awk since you said it's working. Better to check your version (awk --version
) and to know the difference, and if you plan to migrate the code to other platform, bear in mind that it does not guarantee that it will work.
– Tiw
17 hours ago
2
awk --version GNU Awk 3.1.7
– Sach
17 hours ago
1
@Sach, IMHOman awk
is BEST reference, after that you could go through SO posts too there is lot to learn from this GREAT forums. It is simple, if a match is found thenRSTART
tells its starting indexing point and RLENGTH means that full length of matched index. Also[^,]*
means match till first comma occures in current line.
– RavinderSingh13
17 hours ago
1
on moba this is the version :awk --version awk: unknown option -- version BusyBox v1.22.1 (2015-11-10 11:07:12 ) multi-call binary.
– Sach
17 hours ago
|
show 11 more comments
You can try Perl one-liner
perl -lne ' /(COL1:s*S+).+(NAME=w+)/ and print "$1,t$2" ' input_file
with your inputs:
$ cat sach.txt
COL1: VALUE1 , XYZ: 2, OWNER: (DSF) , FLG: DIT /-/-/ OX if 0X, proc=0xyyy23, NAME=AUDIT
COL1: VALUE2 , XYZ: 2, OWNER: (DSF) , FLG: DIT /-/-/ OX if 0X, proc=0xyy23, NAME=generic
XYZ:2, COL1: 289 , TREK:MRP, OWNER: (DSF) , FLG: DIT /-/-/ OX if 0X, NAME=Oil, trial=TREE
$ perl -lne ' /(COL1:s*S+).+(NAME=w+)/ and print "$1,t$2" ' sach.txt
COL1: VALUE1, NAME=AUDIT
COL1: VALUE2, NAME=generic
COL1: 289, NAME=Oil
$
Explanation:
perl -lne # use -n for suppressing print default at the end of each line
' /(COL1:s*S+).+(NAME=w+)/ # Match pattern and capture them in capture groups first () will be $1 and second () will be in $2
# First () matches COL1:s*S+ => COL1: followed by zero or more spaces using s* and S+ for non-space characters
# .+ => match all strings between first () and second ()
# Seecond () matches NAME followed by a word w+
and # bind on the success of previous condition /..../
print "$1,t$2" # print the $1 and $2 captured variables
' input_file
Ahh...perl
, always a good choice, and so powerful as well as elegant PRE is :)
– Tiw
17 hours ago
1
@Tiw.. thank you for the appreciation..yep, Perl is the right tool for regex problems like this..
– stack0114106
17 hours ago
@stack0114106 can you please explain it a bit to me. this is very nice
– Sach
17 hours ago
yes.. will add explanation
– stack0114106
17 hours ago
Added explanation to the code.. pls check
– stack0114106
17 hours ago
add a comment |
With grep you can maybe try something like that :
while read line; do COL=$(echo $line | grep -o "COL1:.*,"); NAME=$(echo $line | grep -o "NAME=[a-zA-Z]*"); echo $COL $NAME >> new_file.txt; done < your_file.txt
The regexp in this example assume that the value after COL1 are always followed by a "," (then it take every characters between the : and ,) so you might have to adapt it to fit your file (same for the regexp used for NAME).
New contributor
add a comment |
Try this:
$ sed 'H;s/.*NAME=/NAME=/;s/ *,.*//;x;s/^.*COL1/COL1/;s/ *,.*//;G;s/n/t, /;' file
COL1: VALUE1 , NAME=AUDIT
COL1: VALUE2 , NAME=generic
COL1: 289 , NAME=Oil
Used hold space, and used t
for alignment.
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55021528%2fwhat-grep-awk-sed-command-to-use-to-the-output-that-i-want%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
4 Answers
4
active
oldest
votes
4 Answers
4
active
oldest
votes
active
oldest
votes
active
oldest
votes
Could you please try following(tested and written in GNU awk
).
awk '
BEGIN
OFS=" , "
match($0,/COL[0-9]+: [^,]*/)
val=substr($0,RSTART,RLENGTH)
match($0,/NAME[^,]*/)
print val OFS substr($0,RSTART,RLENGTH)
val=""
' Input_file
I have clubbed the match(es) of string COL
and NAME
in each line so in case any line do not have string COL
in it, it may not print anything in it.
In case string COL
is not found in a line and you still want to print NAME
string match then try following.
awk '
BEGIN
OFS=" , "
match($0,/COL[0-9]+: [^,]*/)
val=substr($0,RSTART,RLENGTH)
match($0,/NAME[^,]*/)
if(val)
printf "%s%s",val,OFS
print substr($0,RSTART,RLENGTH)
' Input_file
Explanation: Adding explanation for above code now.
awk ' ##Starting awk program heer.
BEGIN ##Starting BEGIN section for awk code here.
OFS=" , " ##Setting OFS output field separator as space comma space here.
##Closing BEGIN section here.
match($0,/COL[0-9]+: [^,]*/) ##Using match of awk OOTB function to match a REGEX string COL till comma here.
val=substr($0,RSTART,RLENGTH) ##If a match is foundthen creating variable val whose value is sub string of matched regex starting to till end value of it.
match($0,/NAME[^,]*/) ##Again using match to match string from NAME to till next comma comes.
print val OFS substr($0,RSTART,RLENGTH) ##Printing value of variable val OFS and substring of current line whose sarting point is RSTART and end point is RLENGTH.
val="" ##Nullifying variable val here.
' Input_file ##Mentioning Input_file name here.
Adding reference from man awk
page:
RSTART The index of the first character matched by match(); 0 if no match. (This implies that character indices start at one.)
RLENGTH The length of the string matched by match(); -1 if no match.
1
works perfect, can you please explain it to me, will appreciate if you do
– Sach
18 hours ago
1
@Sach fyi, thisawk
solution is usinggawk
, which stands forGNU awk
.awk
andgawk
can point to same executable, and in your case, I think you are already using GNU awk since you said it's working. Better to check your version (awk --version
) and to know the difference, and if you plan to migrate the code to other platform, bear in mind that it does not guarantee that it will work.
– Tiw
17 hours ago
2
awk --version GNU Awk 3.1.7
– Sach
17 hours ago
1
@Sach, IMHOman awk
is BEST reference, after that you could go through SO posts too there is lot to learn from this GREAT forums. It is simple, if a match is found thenRSTART
tells its starting indexing point and RLENGTH means that full length of matched index. Also[^,]*
means match till first comma occures in current line.
– RavinderSingh13
17 hours ago
1
on moba this is the version :awk --version awk: unknown option -- version BusyBox v1.22.1 (2015-11-10 11:07:12 ) multi-call binary.
– Sach
17 hours ago
|
show 11 more comments
Could you please try following(tested and written in GNU awk
).
awk '
BEGIN
OFS=" , "
match($0,/COL[0-9]+: [^,]*/)
val=substr($0,RSTART,RLENGTH)
match($0,/NAME[^,]*/)
print val OFS substr($0,RSTART,RLENGTH)
val=""
' Input_file
I have clubbed the match(es) of string COL
and NAME
in each line so in case any line do not have string COL
in it, it may not print anything in it.
In case string COL
is not found in a line and you still want to print NAME
string match then try following.
awk '
BEGIN
OFS=" , "
match($0,/COL[0-9]+: [^,]*/)
val=substr($0,RSTART,RLENGTH)
match($0,/NAME[^,]*/)
if(val)
printf "%s%s",val,OFS
print substr($0,RSTART,RLENGTH)
' Input_file
Explanation: Adding explanation for above code now.
awk ' ##Starting awk program heer.
BEGIN ##Starting BEGIN section for awk code here.
OFS=" , " ##Setting OFS output field separator as space comma space here.
##Closing BEGIN section here.
match($0,/COL[0-9]+: [^,]*/) ##Using match of awk OOTB function to match a REGEX string COL till comma here.
val=substr($0,RSTART,RLENGTH) ##If a match is foundthen creating variable val whose value is sub string of matched regex starting to till end value of it.
match($0,/NAME[^,]*/) ##Again using match to match string from NAME to till next comma comes.
print val OFS substr($0,RSTART,RLENGTH) ##Printing value of variable val OFS and substring of current line whose sarting point is RSTART and end point is RLENGTH.
val="" ##Nullifying variable val here.
' Input_file ##Mentioning Input_file name here.
Adding reference from man awk
page:
RSTART The index of the first character matched by match(); 0 if no match. (This implies that character indices start at one.)
RLENGTH The length of the string matched by match(); -1 if no match.
1
works perfect, can you please explain it to me, will appreciate if you do
– Sach
18 hours ago
1
@Sach fyi, thisawk
solution is usinggawk
, which stands forGNU awk
.awk
andgawk
can point to same executable, and in your case, I think you are already using GNU awk since you said it's working. Better to check your version (awk --version
) and to know the difference, and if you plan to migrate the code to other platform, bear in mind that it does not guarantee that it will work.
– Tiw
17 hours ago
2
awk --version GNU Awk 3.1.7
– Sach
17 hours ago
1
@Sach, IMHOman awk
is BEST reference, after that you could go through SO posts too there is lot to learn from this GREAT forums. It is simple, if a match is found thenRSTART
tells its starting indexing point and RLENGTH means that full length of matched index. Also[^,]*
means match till first comma occures in current line.
– RavinderSingh13
17 hours ago
1
on moba this is the version :awk --version awk: unknown option -- version BusyBox v1.22.1 (2015-11-10 11:07:12 ) multi-call binary.
– Sach
17 hours ago
|
show 11 more comments
Could you please try following(tested and written in GNU awk
).
awk '
BEGIN
OFS=" , "
match($0,/COL[0-9]+: [^,]*/)
val=substr($0,RSTART,RLENGTH)
match($0,/NAME[^,]*/)
print val OFS substr($0,RSTART,RLENGTH)
val=""
' Input_file
I have clubbed the match(es) of string COL
and NAME
in each line so in case any line do not have string COL
in it, it may not print anything in it.
In case string COL
is not found in a line and you still want to print NAME
string match then try following.
awk '
BEGIN
OFS=" , "
match($0,/COL[0-9]+: [^,]*/)
val=substr($0,RSTART,RLENGTH)
match($0,/NAME[^,]*/)
if(val)
printf "%s%s",val,OFS
print substr($0,RSTART,RLENGTH)
' Input_file
Explanation: Adding explanation for above code now.
awk ' ##Starting awk program heer.
BEGIN ##Starting BEGIN section for awk code here.
OFS=" , " ##Setting OFS output field separator as space comma space here.
##Closing BEGIN section here.
match($0,/COL[0-9]+: [^,]*/) ##Using match of awk OOTB function to match a REGEX string COL till comma here.
val=substr($0,RSTART,RLENGTH) ##If a match is foundthen creating variable val whose value is sub string of matched regex starting to till end value of it.
match($0,/NAME[^,]*/) ##Again using match to match string from NAME to till next comma comes.
print val OFS substr($0,RSTART,RLENGTH) ##Printing value of variable val OFS and substring of current line whose sarting point is RSTART and end point is RLENGTH.
val="" ##Nullifying variable val here.
' Input_file ##Mentioning Input_file name here.
Adding reference from man awk
page:
RSTART The index of the first character matched by match(); 0 if no match. (This implies that character indices start at one.)
RLENGTH The length of the string matched by match(); -1 if no match.
Could you please try following(tested and written in GNU awk
).
awk '
BEGIN
OFS=" , "
match($0,/COL[0-9]+: [^,]*/)
val=substr($0,RSTART,RLENGTH)
match($0,/NAME[^,]*/)
print val OFS substr($0,RSTART,RLENGTH)
val=""
' Input_file
I have clubbed the match(es) of string COL
and NAME
in each line so in case any line do not have string COL
in it, it may not print anything in it.
In case string COL
is not found in a line and you still want to print NAME
string match then try following.
awk '
BEGIN
OFS=" , "
match($0,/COL[0-9]+: [^,]*/)
val=substr($0,RSTART,RLENGTH)
match($0,/NAME[^,]*/)
if(val)
printf "%s%s",val,OFS
print substr($0,RSTART,RLENGTH)
' Input_file
Explanation: Adding explanation for above code now.
awk ' ##Starting awk program heer.
BEGIN ##Starting BEGIN section for awk code here.
OFS=" , " ##Setting OFS output field separator as space comma space here.
##Closing BEGIN section here.
match($0,/COL[0-9]+: [^,]*/) ##Using match of awk OOTB function to match a REGEX string COL till comma here.
val=substr($0,RSTART,RLENGTH) ##If a match is foundthen creating variable val whose value is sub string of matched regex starting to till end value of it.
match($0,/NAME[^,]*/) ##Again using match to match string from NAME to till next comma comes.
print val OFS substr($0,RSTART,RLENGTH) ##Printing value of variable val OFS and substring of current line whose sarting point is RSTART and end point is RLENGTH.
val="" ##Nullifying variable val here.
' Input_file ##Mentioning Input_file name here.
Adding reference from man awk
page:
RSTART The index of the first character matched by match(); 0 if no match. (This implies that character indices start at one.)
RLENGTH The length of the string matched by match(); -1 if no match.
edited 17 hours ago
answered 19 hours ago
RavinderSingh13RavinderSingh13
29.3k41639
29.3k41639
1
works perfect, can you please explain it to me, will appreciate if you do
– Sach
18 hours ago
1
@Sach fyi, thisawk
solution is usinggawk
, which stands forGNU awk
.awk
andgawk
can point to same executable, and in your case, I think you are already using GNU awk since you said it's working. Better to check your version (awk --version
) and to know the difference, and if you plan to migrate the code to other platform, bear in mind that it does not guarantee that it will work.
– Tiw
17 hours ago
2
awk --version GNU Awk 3.1.7
– Sach
17 hours ago
1
@Sach, IMHOman awk
is BEST reference, after that you could go through SO posts too there is lot to learn from this GREAT forums. It is simple, if a match is found thenRSTART
tells its starting indexing point and RLENGTH means that full length of matched index. Also[^,]*
means match till first comma occures in current line.
– RavinderSingh13
17 hours ago
1
on moba this is the version :awk --version awk: unknown option -- version BusyBox v1.22.1 (2015-11-10 11:07:12 ) multi-call binary.
– Sach
17 hours ago
|
show 11 more comments
1
works perfect, can you please explain it to me, will appreciate if you do
– Sach
18 hours ago
1
@Sach fyi, thisawk
solution is usinggawk
, which stands forGNU awk
.awk
andgawk
can point to same executable, and in your case, I think you are already using GNU awk since you said it's working. Better to check your version (awk --version
) and to know the difference, and if you plan to migrate the code to other platform, bear in mind that it does not guarantee that it will work.
– Tiw
17 hours ago
2
awk --version GNU Awk 3.1.7
– Sach
17 hours ago
1
@Sach, IMHOman awk
is BEST reference, after that you could go through SO posts too there is lot to learn from this GREAT forums. It is simple, if a match is found thenRSTART
tells its starting indexing point and RLENGTH means that full length of matched index. Also[^,]*
means match till first comma occures in current line.
– RavinderSingh13
17 hours ago
1
on moba this is the version :awk --version awk: unknown option -- version BusyBox v1.22.1 (2015-11-10 11:07:12 ) multi-call binary.
– Sach
17 hours ago
1
1
works perfect, can you please explain it to me, will appreciate if you do
– Sach
18 hours ago
works perfect, can you please explain it to me, will appreciate if you do
– Sach
18 hours ago
1
1
@Sach fyi, this
awk
solution is using gawk
, which stands for GNU awk
. awk
and gawk
can point to same executable, and in your case, I think you are already using GNU awk since you said it's working. Better to check your version (awk --version
) and to know the difference, and if you plan to migrate the code to other platform, bear in mind that it does not guarantee that it will work.– Tiw
17 hours ago
@Sach fyi, this
awk
solution is using gawk
, which stands for GNU awk
. awk
and gawk
can point to same executable, and in your case, I think you are already using GNU awk since you said it's working. Better to check your version (awk --version
) and to know the difference, and if you plan to migrate the code to other platform, bear in mind that it does not guarantee that it will work.– Tiw
17 hours ago
2
2
awk --version GNU Awk 3.1.7
– Sach
17 hours ago
awk --version GNU Awk 3.1.7
– Sach
17 hours ago
1
1
@Sach, IMHO
man awk
is BEST reference, after that you could go through SO posts too there is lot to learn from this GREAT forums. It is simple, if a match is found then RSTART
tells its starting indexing point and RLENGTH means that full length of matched index. Also [^,]*
means match till first comma occures in current line.– RavinderSingh13
17 hours ago
@Sach, IMHO
man awk
is BEST reference, after that you could go through SO posts too there is lot to learn from this GREAT forums. It is simple, if a match is found then RSTART
tells its starting indexing point and RLENGTH means that full length of matched index. Also [^,]*
means match till first comma occures in current line.– RavinderSingh13
17 hours ago
1
1
on moba this is the version :awk --version awk: unknown option -- version BusyBox v1.22.1 (2015-11-10 11:07:12 ) multi-call binary.
– Sach
17 hours ago
on moba this is the version :awk --version awk: unknown option -- version BusyBox v1.22.1 (2015-11-10 11:07:12 ) multi-call binary.
– Sach
17 hours ago
|
show 11 more comments
You can try Perl one-liner
perl -lne ' /(COL1:s*S+).+(NAME=w+)/ and print "$1,t$2" ' input_file
with your inputs:
$ cat sach.txt
COL1: VALUE1 , XYZ: 2, OWNER: (DSF) , FLG: DIT /-/-/ OX if 0X, proc=0xyyy23, NAME=AUDIT
COL1: VALUE2 , XYZ: 2, OWNER: (DSF) , FLG: DIT /-/-/ OX if 0X, proc=0xyy23, NAME=generic
XYZ:2, COL1: 289 , TREK:MRP, OWNER: (DSF) , FLG: DIT /-/-/ OX if 0X, NAME=Oil, trial=TREE
$ perl -lne ' /(COL1:s*S+).+(NAME=w+)/ and print "$1,t$2" ' sach.txt
COL1: VALUE1, NAME=AUDIT
COL1: VALUE2, NAME=generic
COL1: 289, NAME=Oil
$
Explanation:
perl -lne # use -n for suppressing print default at the end of each line
' /(COL1:s*S+).+(NAME=w+)/ # Match pattern and capture them in capture groups first () will be $1 and second () will be in $2
# First () matches COL1:s*S+ => COL1: followed by zero or more spaces using s* and S+ for non-space characters
# .+ => match all strings between first () and second ()
# Seecond () matches NAME followed by a word w+
and # bind on the success of previous condition /..../
print "$1,t$2" # print the $1 and $2 captured variables
' input_file
Ahh...perl
, always a good choice, and so powerful as well as elegant PRE is :)
– Tiw
17 hours ago
1
@Tiw.. thank you for the appreciation..yep, Perl is the right tool for regex problems like this..
– stack0114106
17 hours ago
@stack0114106 can you please explain it a bit to me. this is very nice
– Sach
17 hours ago
yes.. will add explanation
– stack0114106
17 hours ago
Added explanation to the code.. pls check
– stack0114106
17 hours ago
add a comment |
You can try Perl one-liner
perl -lne ' /(COL1:s*S+).+(NAME=w+)/ and print "$1,t$2" ' input_file
with your inputs:
$ cat sach.txt
COL1: VALUE1 , XYZ: 2, OWNER: (DSF) , FLG: DIT /-/-/ OX if 0X, proc=0xyyy23, NAME=AUDIT
COL1: VALUE2 , XYZ: 2, OWNER: (DSF) , FLG: DIT /-/-/ OX if 0X, proc=0xyy23, NAME=generic
XYZ:2, COL1: 289 , TREK:MRP, OWNER: (DSF) , FLG: DIT /-/-/ OX if 0X, NAME=Oil, trial=TREE
$ perl -lne ' /(COL1:s*S+).+(NAME=w+)/ and print "$1,t$2" ' sach.txt
COL1: VALUE1, NAME=AUDIT
COL1: VALUE2, NAME=generic
COL1: 289, NAME=Oil
$
Explanation:
perl -lne # use -n for suppressing print default at the end of each line
' /(COL1:s*S+).+(NAME=w+)/ # Match pattern and capture them in capture groups first () will be $1 and second () will be in $2
# First () matches COL1:s*S+ => COL1: followed by zero or more spaces using s* and S+ for non-space characters
# .+ => match all strings between first () and second ()
# Seecond () matches NAME followed by a word w+
and # bind on the success of previous condition /..../
print "$1,t$2" # print the $1 and $2 captured variables
' input_file
Ahh...perl
, always a good choice, and so powerful as well as elegant PRE is :)
– Tiw
17 hours ago
1
@Tiw.. thank you for the appreciation..yep, Perl is the right tool for regex problems like this..
– stack0114106
17 hours ago
@stack0114106 can you please explain it a bit to me. this is very nice
– Sach
17 hours ago
yes.. will add explanation
– stack0114106
17 hours ago
Added explanation to the code.. pls check
– stack0114106
17 hours ago
add a comment |
You can try Perl one-liner
perl -lne ' /(COL1:s*S+).+(NAME=w+)/ and print "$1,t$2" ' input_file
with your inputs:
$ cat sach.txt
COL1: VALUE1 , XYZ: 2, OWNER: (DSF) , FLG: DIT /-/-/ OX if 0X, proc=0xyyy23, NAME=AUDIT
COL1: VALUE2 , XYZ: 2, OWNER: (DSF) , FLG: DIT /-/-/ OX if 0X, proc=0xyy23, NAME=generic
XYZ:2, COL1: 289 , TREK:MRP, OWNER: (DSF) , FLG: DIT /-/-/ OX if 0X, NAME=Oil, trial=TREE
$ perl -lne ' /(COL1:s*S+).+(NAME=w+)/ and print "$1,t$2" ' sach.txt
COL1: VALUE1, NAME=AUDIT
COL1: VALUE2, NAME=generic
COL1: 289, NAME=Oil
$
Explanation:
perl -lne # use -n for suppressing print default at the end of each line
' /(COL1:s*S+).+(NAME=w+)/ # Match pattern and capture them in capture groups first () will be $1 and second () will be in $2
# First () matches COL1:s*S+ => COL1: followed by zero or more spaces using s* and S+ for non-space characters
# .+ => match all strings between first () and second ()
# Seecond () matches NAME followed by a word w+
and # bind on the success of previous condition /..../
print "$1,t$2" # print the $1 and $2 captured variables
' input_file
You can try Perl one-liner
perl -lne ' /(COL1:s*S+).+(NAME=w+)/ and print "$1,t$2" ' input_file
with your inputs:
$ cat sach.txt
COL1: VALUE1 , XYZ: 2, OWNER: (DSF) , FLG: DIT /-/-/ OX if 0X, proc=0xyyy23, NAME=AUDIT
COL1: VALUE2 , XYZ: 2, OWNER: (DSF) , FLG: DIT /-/-/ OX if 0X, proc=0xyy23, NAME=generic
XYZ:2, COL1: 289 , TREK:MRP, OWNER: (DSF) , FLG: DIT /-/-/ OX if 0X, NAME=Oil, trial=TREE
$ perl -lne ' /(COL1:s*S+).+(NAME=w+)/ and print "$1,t$2" ' sach.txt
COL1: VALUE1, NAME=AUDIT
COL1: VALUE2, NAME=generic
COL1: 289, NAME=Oil
$
Explanation:
perl -lne # use -n for suppressing print default at the end of each line
' /(COL1:s*S+).+(NAME=w+)/ # Match pattern and capture them in capture groups first () will be $1 and second () will be in $2
# First () matches COL1:s*S+ => COL1: followed by zero or more spaces using s* and S+ for non-space characters
# .+ => match all strings between first () and second ()
# Seecond () matches NAME followed by a word w+
and # bind on the success of previous condition /..../
print "$1,t$2" # print the $1 and $2 captured variables
' input_file
edited 17 hours ago
answered 17 hours ago
stack0114106stack0114106
4,1032421
4,1032421
Ahh...perl
, always a good choice, and so powerful as well as elegant PRE is :)
– Tiw
17 hours ago
1
@Tiw.. thank you for the appreciation..yep, Perl is the right tool for regex problems like this..
– stack0114106
17 hours ago
@stack0114106 can you please explain it a bit to me. this is very nice
– Sach
17 hours ago
yes.. will add explanation
– stack0114106
17 hours ago
Added explanation to the code.. pls check
– stack0114106
17 hours ago
add a comment |
Ahh...perl
, always a good choice, and so powerful as well as elegant PRE is :)
– Tiw
17 hours ago
1
@Tiw.. thank you for the appreciation..yep, Perl is the right tool for regex problems like this..
– stack0114106
17 hours ago
@stack0114106 can you please explain it a bit to me. this is very nice
– Sach
17 hours ago
yes.. will add explanation
– stack0114106
17 hours ago
Added explanation to the code.. pls check
– stack0114106
17 hours ago
Ahh...
perl
, always a good choice, and so powerful as well as elegant PRE is :)– Tiw
17 hours ago
Ahh...
perl
, always a good choice, and so powerful as well as elegant PRE is :)– Tiw
17 hours ago
1
1
@Tiw.. thank you for the appreciation..yep, Perl is the right tool for regex problems like this..
– stack0114106
17 hours ago
@Tiw.. thank you for the appreciation..yep, Perl is the right tool for regex problems like this..
– stack0114106
17 hours ago
@stack0114106 can you please explain it a bit to me. this is very nice
– Sach
17 hours ago
@stack0114106 can you please explain it a bit to me. this is very nice
– Sach
17 hours ago
yes.. will add explanation
– stack0114106
17 hours ago
yes.. will add explanation
– stack0114106
17 hours ago
Added explanation to the code.. pls check
– stack0114106
17 hours ago
Added explanation to the code.. pls check
– stack0114106
17 hours ago
add a comment |
With grep you can maybe try something like that :
while read line; do COL=$(echo $line | grep -o "COL1:.*,"); NAME=$(echo $line | grep -o "NAME=[a-zA-Z]*"); echo $COL $NAME >> new_file.txt; done < your_file.txt
The regexp in this example assume that the value after COL1 are always followed by a "," (then it take every characters between the : and ,) so you might have to adapt it to fit your file (same for the regexp used for NAME).
New contributor
add a comment |
With grep you can maybe try something like that :
while read line; do COL=$(echo $line | grep -o "COL1:.*,"); NAME=$(echo $line | grep -o "NAME=[a-zA-Z]*"); echo $COL $NAME >> new_file.txt; done < your_file.txt
The regexp in this example assume that the value after COL1 are always followed by a "," (then it take every characters between the : and ,) so you might have to adapt it to fit your file (same for the regexp used for NAME).
New contributor
add a comment |
With grep you can maybe try something like that :
while read line; do COL=$(echo $line | grep -o "COL1:.*,"); NAME=$(echo $line | grep -o "NAME=[a-zA-Z]*"); echo $COL $NAME >> new_file.txt; done < your_file.txt
The regexp in this example assume that the value after COL1 are always followed by a "," (then it take every characters between the : and ,) so you might have to adapt it to fit your file (same for the regexp used for NAME).
New contributor
With grep you can maybe try something like that :
while read line; do COL=$(echo $line | grep -o "COL1:.*,"); NAME=$(echo $line | grep -o "NAME=[a-zA-Z]*"); echo $COL $NAME >> new_file.txt; done < your_file.txt
The regexp in this example assume that the value after COL1 are always followed by a "," (then it take every characters between the : and ,) so you might have to adapt it to fit your file (same for the regexp used for NAME).
New contributor
New contributor
answered 18 hours ago
Loïs RancilhacLoïs Rancilhac
11
11
New contributor
New contributor
add a comment |
add a comment |
Try this:
$ sed 'H;s/.*NAME=/NAME=/;s/ *,.*//;x;s/^.*COL1/COL1/;s/ *,.*//;G;s/n/t, /;' file
COL1: VALUE1 , NAME=AUDIT
COL1: VALUE2 , NAME=generic
COL1: 289 , NAME=Oil
Used hold space, and used t
for alignment.
add a comment |
Try this:
$ sed 'H;s/.*NAME=/NAME=/;s/ *,.*//;x;s/^.*COL1/COL1/;s/ *,.*//;G;s/n/t, /;' file
COL1: VALUE1 , NAME=AUDIT
COL1: VALUE2 , NAME=generic
COL1: 289 , NAME=Oil
Used hold space, and used t
for alignment.
add a comment |
Try this:
$ sed 'H;s/.*NAME=/NAME=/;s/ *,.*//;x;s/^.*COL1/COL1/;s/ *,.*//;G;s/n/t, /;' file
COL1: VALUE1 , NAME=AUDIT
COL1: VALUE2 , NAME=generic
COL1: 289 , NAME=Oil
Used hold space, and used t
for alignment.
Try this:
$ sed 'H;s/.*NAME=/NAME=/;s/ *,.*//;x;s/^.*COL1/COL1/;s/ *,.*//;G;s/n/t, /;' file
COL1: VALUE1 , NAME=AUDIT
COL1: VALUE2 , NAME=generic
COL1: 289 , NAME=Oil
Used hold space, and used t
for alignment.
edited 16 hours ago
answered 17 hours ago
TiwTiw
3,57341229
3,57341229
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55021528%2fwhat-grep-awk-sed-command-to-use-to-the-output-that-i-want%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown