Match pattern in a file and print the output
Clash Royale CLAN TAG#URR8PPP
up vote
3
down vote
favorite
I am trying to match the pattern "SHM" in a file containing below information and print the word matching the pattern.
LOCALZONE01 ASHM001002003VOL01
BSHM001002003VOL02
CSHM001002003VOL03
DSHM001002003VOL03_DUP
ESHM001002003VOL04
FSHM001002003VOL05
GSHM001002003VOL06_
HSHM001002003VOL07
I have tried to use awk to print 2nd column
grep "SHM" <filename.txt> | awk -F" " 'print $2'
ASHM001002003VOL01
If i try to print column 1 its giving me the below output
LOCALZONE01
BSHM001002003VOL02
CSHM001002003VOL03
DSHM001002003VOL03_DUP
ESHM001002003VOL04
FSHM001002003VOL05
GSHM001002003VOL06_
HSHM001002003VOL07
Below is my desired output. How should i achieve it.
ASHM001002003VOL01
BSHM001002003VOL02
CSHM001002003VOL03
DSHM001002003VOL03_DUP
ESHM001002003VOL04
FSHM001002003VOL05
GSHM001002003VOL06_
HSHM001002003VOL07
shell-script text-processing awk sed
add a comment |Â
up vote
3
down vote
favorite
I am trying to match the pattern "SHM" in a file containing below information and print the word matching the pattern.
LOCALZONE01 ASHM001002003VOL01
BSHM001002003VOL02
CSHM001002003VOL03
DSHM001002003VOL03_DUP
ESHM001002003VOL04
FSHM001002003VOL05
GSHM001002003VOL06_
HSHM001002003VOL07
I have tried to use awk to print 2nd column
grep "SHM" <filename.txt> | awk -F" " 'print $2'
ASHM001002003VOL01
If i try to print column 1 its giving me the below output
LOCALZONE01
BSHM001002003VOL02
CSHM001002003VOL03
DSHM001002003VOL03_DUP
ESHM001002003VOL04
FSHM001002003VOL05
GSHM001002003VOL06_
HSHM001002003VOL07
Below is my desired output. How should i achieve it.
ASHM001002003VOL01
BSHM001002003VOL02
CSHM001002003VOL03
DSHM001002003VOL03_DUP
ESHM001002003VOL04
FSHM001002003VOL05
GSHM001002003VOL06_
HSHM001002003VOL07
shell-script text-processing awk sed
add a comment |Â
up vote
3
down vote
favorite
up vote
3
down vote
favorite
I am trying to match the pattern "SHM" in a file containing below information and print the word matching the pattern.
LOCALZONE01 ASHM001002003VOL01
BSHM001002003VOL02
CSHM001002003VOL03
DSHM001002003VOL03_DUP
ESHM001002003VOL04
FSHM001002003VOL05
GSHM001002003VOL06_
HSHM001002003VOL07
I have tried to use awk to print 2nd column
grep "SHM" <filename.txt> | awk -F" " 'print $2'
ASHM001002003VOL01
If i try to print column 1 its giving me the below output
LOCALZONE01
BSHM001002003VOL02
CSHM001002003VOL03
DSHM001002003VOL03_DUP
ESHM001002003VOL04
FSHM001002003VOL05
GSHM001002003VOL06_
HSHM001002003VOL07
Below is my desired output. How should i achieve it.
ASHM001002003VOL01
BSHM001002003VOL02
CSHM001002003VOL03
DSHM001002003VOL03_DUP
ESHM001002003VOL04
FSHM001002003VOL05
GSHM001002003VOL06_
HSHM001002003VOL07
shell-script text-processing awk sed
I am trying to match the pattern "SHM" in a file containing below information and print the word matching the pattern.
LOCALZONE01 ASHM001002003VOL01
BSHM001002003VOL02
CSHM001002003VOL03
DSHM001002003VOL03_DUP
ESHM001002003VOL04
FSHM001002003VOL05
GSHM001002003VOL06_
HSHM001002003VOL07
I have tried to use awk to print 2nd column
grep "SHM" <filename.txt> | awk -F" " 'print $2'
ASHM001002003VOL01
If i try to print column 1 its giving me the below output
LOCALZONE01
BSHM001002003VOL02
CSHM001002003VOL03
DSHM001002003VOL03_DUP
ESHM001002003VOL04
FSHM001002003VOL05
GSHM001002003VOL06_
HSHM001002003VOL07
Below is my desired output. How should i achieve it.
ASHM001002003VOL01
BSHM001002003VOL02
CSHM001002003VOL03
DSHM001002003VOL03_DUP
ESHM001002003VOL04
FSHM001002003VOL05
GSHM001002003VOL06_
HSHM001002003VOL07
shell-script text-processing awk sed
shell-script text-processing awk sed
edited 21 hours ago


Jeff Schaller
32.3k849109
32.3k849109
asked 21 hours ago
xrkr
7515
7515
add a comment |Â
add a comment |Â
5 Answers
5
active
oldest
votes
up vote
3
down vote
accepted
If the output you want is always the last field in the file, try this
awk 'if ($NF ~ /SHM/) print $NF' _input_file_
Hope this helps.
New contributor
Lewis M is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
more idiomatic awk is "condition action":awk '$NF ~ /SHM/ print $NF'
– glenn jackman
17 hours ago
add a comment |Â
up vote
6
down vote
If you have GNU grep available,
grep -Eo '[[:alnum:]_]*SHM[[:alnum:]_]*' < filename.txt
If not, you could ask awk to loop through the fields of each line, looking for SHM:
awk ' for(i=1;i<=NF;i++) if ($i ~ /SHM/) print $i ' < filename.txt
I don't think thatgrep
expression requires GNUgrep
, nor-E
. (sorry, that was before I spotted the-o
, but-E
would not be needed, right?)
– Kusalananda
20 hours ago
Correct, @Kusalananda, as-is. I suppose it could/should be tightened up to require 1-or-more alnum's after the "SHM" text, in which case it would need-E
for clarity, if not functionality, asIn GNU grep, there is no difference in available functionality between basic and extended syntaxes.
– Jeff Schaller
20 hours ago
@jeff I used the awk version. Thanks. I am redirecting the output to another file where i am using these hostnames to capture df output. Surprisingly the redirected output seems to have an extra character "^M" in the redirected output. I filtered it out using sed i.esed -e "s/^M//" filename
but it seems like it is removing the letters starting with M as well. Any thoughts pls
– xrkr
11 hours ago
Got it . i usedsed 's/r//g'
to filter it out
– xrkr
11 hours ago
jeff, I want to print only the 2nd column in this case.
– xrkr
10 hours ago
add a comment |Â
up vote
5
down vote
Because the first column of your example data has no entries beginning with row #2 and going forward, you'll have to parse it as fixed-width
columns. You can do this:
$awk 'BEGIN FIELDWIDTHS = "16 40" /SHM/ print $2'
ASHM001002003VOL01
BSHM001002003VOL02
CSHM001002003VOL03
DSHM001002003VOL03_DUP
ESHM001002003VOL04
FSHM001002003VOL05
GSHM001002003VOL06_
HSHM001002003VOL07
2
I'd suggest a simplification:awk 'BEGIN FIELDWIDTHS = "16 40" /SHM/ print $2'
– Jeff Schaller
19 hours ago
1
@JeffSchaller. Your solutions are always elegant! I will update the answer ;-)
– Goro
19 hours ago
2
I used to chain grep awk and sed together, until I learned awk a bit better!
– Jeff Schaller
19 hours ago
1
@Jeff Schaller I can use this it is not always certain the field widths are in the same way mentioned above meaning the first word can have more than 20 characters in some files. If i use the above one, sometimes i am seeing the output as04 SHM001002016VOL01
– xrkr
11 hours ago
@xrkr. The command above can handle files contain fixed-width columns. If the width is not equal between the columns, then you would need to convert the file to fixed-width columns. After then, pipe the output to the previous command, something like:awk -F, 'printf("%20s %20s %15sn", $1, $2, $3)' <FILE.txt> | awk 'BEGIN FIELDWIDTHS = "16 40" /SHM/ print $2'
– Goro
3 hours ago
 |Â
show 2 more comments
up vote
3
down vote
awk 'NR==1 print $2' filename && awk 'NR>1' filename | sed 's/[[:space:]]*//g'
Output:
ASHM001002003VOL01
BSHM001002003VOL02
CSHM001002003VOL03
DSHM001002003VOL03_DUP
ESHM001002003VOL04
FSHM001002003VOL05
GSHM001002003VOL06_
HSHM001002003VOL07
That prints the second column on the first line, the following lines, and then removes whitespace to fix the formatting and return the output you want.
you don't need to call awk multiple times:awk 'NR == 1 print $2 NR > 1 print $1'
. This can even be collapsed, but at the expense of readability:awk 'print $(NR == 1 ? 2 : 1)'
– glenn jackman
17 hours ago
@glennjackman True, but it works just the same and it's roughly the same length with the removal of the whitespace.
– Nasir Riley
17 hours ago
I guess it's the principle of calling one program versus calling 3 to achieve the same result.
– glenn jackman
17 hours ago
@glennjackman I understand, but in his case, it really doesn't affect anything. If it were a script that was doing more work then I, too, would prefer your solution because it wouldn't call as many programs and it would be less tedious.
– Nasir Riley
17 hours ago
add a comment |Â
up vote
2
down vote
You could do this as follows using Perl
:
perl -lne 'print for /w*SHMw*/g' input-file.txt
perl -lane 'print for grep /SHM/, @F' input-file.txt # assuming SHM fields r alphanumeric
Or, with the sed
editor in a POSIX-compatible manner, assuming all lines atleast one SHM
sed -ne '
s/[[:alnum:]_]*SHM[[:alnum:]_]*/
&
/;s/.*n(.*n)/1/;P;/n$/!D
' input.txt
Output:
ASHM001002003VOL01
BSHM001002003VOL02
CSHM001002003VOL03
DSHM001002003VOL03_DUP
ESHM001002003VOL04
FSHM001002003VOL05
GSHM001002003VOL06_
add a comment |Â
5 Answers
5
active
oldest
votes
5 Answers
5
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
3
down vote
accepted
If the output you want is always the last field in the file, try this
awk 'if ($NF ~ /SHM/) print $NF' _input_file_
Hope this helps.
New contributor
Lewis M is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
more idiomatic awk is "condition action":awk '$NF ~ /SHM/ print $NF'
– glenn jackman
17 hours ago
add a comment |Â
up vote
3
down vote
accepted
If the output you want is always the last field in the file, try this
awk 'if ($NF ~ /SHM/) print $NF' _input_file_
Hope this helps.
New contributor
Lewis M is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
more idiomatic awk is "condition action":awk '$NF ~ /SHM/ print $NF'
– glenn jackman
17 hours ago
add a comment |Â
up vote
3
down vote
accepted
up vote
3
down vote
accepted
If the output you want is always the last field in the file, try this
awk 'if ($NF ~ /SHM/) print $NF' _input_file_
Hope this helps.
New contributor
Lewis M is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
If the output you want is always the last field in the file, try this
awk 'if ($NF ~ /SHM/) print $NF' _input_file_
Hope this helps.
New contributor
Lewis M is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
New contributor
Lewis M is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
answered 19 hours ago
Lewis M
1903
1903
New contributor
Lewis M is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
New contributor
Lewis M is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
Lewis M is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
more idiomatic awk is "condition action":awk '$NF ~ /SHM/ print $NF'
– glenn jackman
17 hours ago
add a comment |Â
more idiomatic awk is "condition action":awk '$NF ~ /SHM/ print $NF'
– glenn jackman
17 hours ago
more idiomatic awk is "condition action":
awk '$NF ~ /SHM/ print $NF'
– glenn jackman
17 hours ago
more idiomatic awk is "condition action":
awk '$NF ~ /SHM/ print $NF'
– glenn jackman
17 hours ago
add a comment |Â
up vote
6
down vote
If you have GNU grep available,
grep -Eo '[[:alnum:]_]*SHM[[:alnum:]_]*' < filename.txt
If not, you could ask awk to loop through the fields of each line, looking for SHM:
awk ' for(i=1;i<=NF;i++) if ($i ~ /SHM/) print $i ' < filename.txt
I don't think thatgrep
expression requires GNUgrep
, nor-E
. (sorry, that was before I spotted the-o
, but-E
would not be needed, right?)
– Kusalananda
20 hours ago
Correct, @Kusalananda, as-is. I suppose it could/should be tightened up to require 1-or-more alnum's after the "SHM" text, in which case it would need-E
for clarity, if not functionality, asIn GNU grep, there is no difference in available functionality between basic and extended syntaxes.
– Jeff Schaller
20 hours ago
@jeff I used the awk version. Thanks. I am redirecting the output to another file where i am using these hostnames to capture df output. Surprisingly the redirected output seems to have an extra character "^M" in the redirected output. I filtered it out using sed i.esed -e "s/^M//" filename
but it seems like it is removing the letters starting with M as well. Any thoughts pls
– xrkr
11 hours ago
Got it . i usedsed 's/r//g'
to filter it out
– xrkr
11 hours ago
jeff, I want to print only the 2nd column in this case.
– xrkr
10 hours ago
add a comment |Â
up vote
6
down vote
If you have GNU grep available,
grep -Eo '[[:alnum:]_]*SHM[[:alnum:]_]*' < filename.txt
If not, you could ask awk to loop through the fields of each line, looking for SHM:
awk ' for(i=1;i<=NF;i++) if ($i ~ /SHM/) print $i ' < filename.txt
I don't think thatgrep
expression requires GNUgrep
, nor-E
. (sorry, that was before I spotted the-o
, but-E
would not be needed, right?)
– Kusalananda
20 hours ago
Correct, @Kusalananda, as-is. I suppose it could/should be tightened up to require 1-or-more alnum's after the "SHM" text, in which case it would need-E
for clarity, if not functionality, asIn GNU grep, there is no difference in available functionality between basic and extended syntaxes.
– Jeff Schaller
20 hours ago
@jeff I used the awk version. Thanks. I am redirecting the output to another file where i am using these hostnames to capture df output. Surprisingly the redirected output seems to have an extra character "^M" in the redirected output. I filtered it out using sed i.esed -e "s/^M//" filename
but it seems like it is removing the letters starting with M as well. Any thoughts pls
– xrkr
11 hours ago
Got it . i usedsed 's/r//g'
to filter it out
– xrkr
11 hours ago
jeff, I want to print only the 2nd column in this case.
– xrkr
10 hours ago
add a comment |Â
up vote
6
down vote
up vote
6
down vote
If you have GNU grep available,
grep -Eo '[[:alnum:]_]*SHM[[:alnum:]_]*' < filename.txt
If not, you could ask awk to loop through the fields of each line, looking for SHM:
awk ' for(i=1;i<=NF;i++) if ($i ~ /SHM/) print $i ' < filename.txt
If you have GNU grep available,
grep -Eo '[[:alnum:]_]*SHM[[:alnum:]_]*' < filename.txt
If not, you could ask awk to loop through the fields of each line, looking for SHM:
awk ' for(i=1;i<=NF;i++) if ($i ~ /SHM/) print $i ' < filename.txt
answered 21 hours ago


Jeff Schaller
32.3k849109
32.3k849109
I don't think thatgrep
expression requires GNUgrep
, nor-E
. (sorry, that was before I spotted the-o
, but-E
would not be needed, right?)
– Kusalananda
20 hours ago
Correct, @Kusalananda, as-is. I suppose it could/should be tightened up to require 1-or-more alnum's after the "SHM" text, in which case it would need-E
for clarity, if not functionality, asIn GNU grep, there is no difference in available functionality between basic and extended syntaxes.
– Jeff Schaller
20 hours ago
@jeff I used the awk version. Thanks. I am redirecting the output to another file where i am using these hostnames to capture df output. Surprisingly the redirected output seems to have an extra character "^M" in the redirected output. I filtered it out using sed i.esed -e "s/^M//" filename
but it seems like it is removing the letters starting with M as well. Any thoughts pls
– xrkr
11 hours ago
Got it . i usedsed 's/r//g'
to filter it out
– xrkr
11 hours ago
jeff, I want to print only the 2nd column in this case.
– xrkr
10 hours ago
add a comment |Â
I don't think thatgrep
expression requires GNUgrep
, nor-E
. (sorry, that was before I spotted the-o
, but-E
would not be needed, right?)
– Kusalananda
20 hours ago
Correct, @Kusalananda, as-is. I suppose it could/should be tightened up to require 1-or-more alnum's after the "SHM" text, in which case it would need-E
for clarity, if not functionality, asIn GNU grep, there is no difference in available functionality between basic and extended syntaxes.
– Jeff Schaller
20 hours ago
@jeff I used the awk version. Thanks. I am redirecting the output to another file where i am using these hostnames to capture df output. Surprisingly the redirected output seems to have an extra character "^M" in the redirected output. I filtered it out using sed i.esed -e "s/^M//" filename
but it seems like it is removing the letters starting with M as well. Any thoughts pls
– xrkr
11 hours ago
Got it . i usedsed 's/r//g'
to filter it out
– xrkr
11 hours ago
jeff, I want to print only the 2nd column in this case.
– xrkr
10 hours ago
I don't think that
grep
expression requires GNU grep
, nor -E
. (sorry, that was before I spotted the -o
, but -E
would not be needed, right?)– Kusalananda
20 hours ago
I don't think that
grep
expression requires GNU grep
, nor -E
. (sorry, that was before I spotted the -o
, but -E
would not be needed, right?)– Kusalananda
20 hours ago
Correct, @Kusalananda, as-is. I suppose it could/should be tightened up to require 1-or-more alnum's after the "SHM" text, in which case it would need
-E
for clarity, if not functionality, as In GNU grep, there is no difference in available functionality between basic and extended syntaxes.
– Jeff Schaller
20 hours ago
Correct, @Kusalananda, as-is. I suppose it could/should be tightened up to require 1-or-more alnum's after the "SHM" text, in which case it would need
-E
for clarity, if not functionality, as In GNU grep, there is no difference in available functionality between basic and extended syntaxes.
– Jeff Schaller
20 hours ago
@jeff I used the awk version. Thanks. I am redirecting the output to another file where i am using these hostnames to capture df output. Surprisingly the redirected output seems to have an extra character "^M" in the redirected output. I filtered it out using sed i.e
sed -e "s/^M//" filename
but it seems like it is removing the letters starting with M as well. Any thoughts pls– xrkr
11 hours ago
@jeff I used the awk version. Thanks. I am redirecting the output to another file where i am using these hostnames to capture df output. Surprisingly the redirected output seems to have an extra character "^M" in the redirected output. I filtered it out using sed i.e
sed -e "s/^M//" filename
but it seems like it is removing the letters starting with M as well. Any thoughts pls– xrkr
11 hours ago
Got it . i used
sed 's/r//g'
to filter it out– xrkr
11 hours ago
Got it . i used
sed 's/r//g'
to filter it out– xrkr
11 hours ago
jeff, I want to print only the 2nd column in this case.
– xrkr
10 hours ago
jeff, I want to print only the 2nd column in this case.
– xrkr
10 hours ago
add a comment |Â
up vote
5
down vote
Because the first column of your example data has no entries beginning with row #2 and going forward, you'll have to parse it as fixed-width
columns. You can do this:
$awk 'BEGIN FIELDWIDTHS = "16 40" /SHM/ print $2'
ASHM001002003VOL01
BSHM001002003VOL02
CSHM001002003VOL03
DSHM001002003VOL03_DUP
ESHM001002003VOL04
FSHM001002003VOL05
GSHM001002003VOL06_
HSHM001002003VOL07
2
I'd suggest a simplification:awk 'BEGIN FIELDWIDTHS = "16 40" /SHM/ print $2'
– Jeff Schaller
19 hours ago
1
@JeffSchaller. Your solutions are always elegant! I will update the answer ;-)
– Goro
19 hours ago
2
I used to chain grep awk and sed together, until I learned awk a bit better!
– Jeff Schaller
19 hours ago
1
@Jeff Schaller I can use this it is not always certain the field widths are in the same way mentioned above meaning the first word can have more than 20 characters in some files. If i use the above one, sometimes i am seeing the output as04 SHM001002016VOL01
– xrkr
11 hours ago
@xrkr. The command above can handle files contain fixed-width columns. If the width is not equal between the columns, then you would need to convert the file to fixed-width columns. After then, pipe the output to the previous command, something like:awk -F, 'printf("%20s %20s %15sn", $1, $2, $3)' <FILE.txt> | awk 'BEGIN FIELDWIDTHS = "16 40" /SHM/ print $2'
– Goro
3 hours ago
 |Â
show 2 more comments
up vote
5
down vote
Because the first column of your example data has no entries beginning with row #2 and going forward, you'll have to parse it as fixed-width
columns. You can do this:
$awk 'BEGIN FIELDWIDTHS = "16 40" /SHM/ print $2'
ASHM001002003VOL01
BSHM001002003VOL02
CSHM001002003VOL03
DSHM001002003VOL03_DUP
ESHM001002003VOL04
FSHM001002003VOL05
GSHM001002003VOL06_
HSHM001002003VOL07
2
I'd suggest a simplification:awk 'BEGIN FIELDWIDTHS = "16 40" /SHM/ print $2'
– Jeff Schaller
19 hours ago
1
@JeffSchaller. Your solutions are always elegant! I will update the answer ;-)
– Goro
19 hours ago
2
I used to chain grep awk and sed together, until I learned awk a bit better!
– Jeff Schaller
19 hours ago
1
@Jeff Schaller I can use this it is not always certain the field widths are in the same way mentioned above meaning the first word can have more than 20 characters in some files. If i use the above one, sometimes i am seeing the output as04 SHM001002016VOL01
– xrkr
11 hours ago
@xrkr. The command above can handle files contain fixed-width columns. If the width is not equal between the columns, then you would need to convert the file to fixed-width columns. After then, pipe the output to the previous command, something like:awk -F, 'printf("%20s %20s %15sn", $1, $2, $3)' <FILE.txt> | awk 'BEGIN FIELDWIDTHS = "16 40" /SHM/ print $2'
– Goro
3 hours ago
 |Â
show 2 more comments
up vote
5
down vote
up vote
5
down vote
Because the first column of your example data has no entries beginning with row #2 and going forward, you'll have to parse it as fixed-width
columns. You can do this:
$awk 'BEGIN FIELDWIDTHS = "16 40" /SHM/ print $2'
ASHM001002003VOL01
BSHM001002003VOL02
CSHM001002003VOL03
DSHM001002003VOL03_DUP
ESHM001002003VOL04
FSHM001002003VOL05
GSHM001002003VOL06_
HSHM001002003VOL07
Because the first column of your example data has no entries beginning with row #2 and going forward, you'll have to parse it as fixed-width
columns. You can do this:
$awk 'BEGIN FIELDWIDTHS = "16 40" /SHM/ print $2'
ASHM001002003VOL01
BSHM001002003VOL02
CSHM001002003VOL03
DSHM001002003VOL03_DUP
ESHM001002003VOL04
FSHM001002003VOL05
GSHM001002003VOL06_
HSHM001002003VOL07
edited 3 hours ago
answered 21 hours ago
Goro
1,62941644
1,62941644
2
I'd suggest a simplification:awk 'BEGIN FIELDWIDTHS = "16 40" /SHM/ print $2'
– Jeff Schaller
19 hours ago
1
@JeffSchaller. Your solutions are always elegant! I will update the answer ;-)
– Goro
19 hours ago
2
I used to chain grep awk and sed together, until I learned awk a bit better!
– Jeff Schaller
19 hours ago
1
@Jeff Schaller I can use this it is not always certain the field widths are in the same way mentioned above meaning the first word can have more than 20 characters in some files. If i use the above one, sometimes i am seeing the output as04 SHM001002016VOL01
– xrkr
11 hours ago
@xrkr. The command above can handle files contain fixed-width columns. If the width is not equal between the columns, then you would need to convert the file to fixed-width columns. After then, pipe the output to the previous command, something like:awk -F, 'printf("%20s %20s %15sn", $1, $2, $3)' <FILE.txt> | awk 'BEGIN FIELDWIDTHS = "16 40" /SHM/ print $2'
– Goro
3 hours ago
 |Â
show 2 more comments
2
I'd suggest a simplification:awk 'BEGIN FIELDWIDTHS = "16 40" /SHM/ print $2'
– Jeff Schaller
19 hours ago
1
@JeffSchaller. Your solutions are always elegant! I will update the answer ;-)
– Goro
19 hours ago
2
I used to chain grep awk and sed together, until I learned awk a bit better!
– Jeff Schaller
19 hours ago
1
@Jeff Schaller I can use this it is not always certain the field widths are in the same way mentioned above meaning the first word can have more than 20 characters in some files. If i use the above one, sometimes i am seeing the output as04 SHM001002016VOL01
– xrkr
11 hours ago
@xrkr. The command above can handle files contain fixed-width columns. If the width is not equal between the columns, then you would need to convert the file to fixed-width columns. After then, pipe the output to the previous command, something like:awk -F, 'printf("%20s %20s %15sn", $1, $2, $3)' <FILE.txt> | awk 'BEGIN FIELDWIDTHS = "16 40" /SHM/ print $2'
– Goro
3 hours ago
2
2
I'd suggest a simplification:
awk 'BEGIN FIELDWIDTHS = "16 40" /SHM/ print $2'
– Jeff Schaller
19 hours ago
I'd suggest a simplification:
awk 'BEGIN FIELDWIDTHS = "16 40" /SHM/ print $2'
– Jeff Schaller
19 hours ago
1
1
@JeffSchaller. Your solutions are always elegant! I will update the answer ;-)
– Goro
19 hours ago
@JeffSchaller. Your solutions are always elegant! I will update the answer ;-)
– Goro
19 hours ago
2
2
I used to chain grep awk and sed together, until I learned awk a bit better!
– Jeff Schaller
19 hours ago
I used to chain grep awk and sed together, until I learned awk a bit better!
– Jeff Schaller
19 hours ago
1
1
@Jeff Schaller I can use this it is not always certain the field widths are in the same way mentioned above meaning the first word can have more than 20 characters in some files. If i use the above one, sometimes i am seeing the output as
04 SHM001002016VOL01
– xrkr
11 hours ago
@Jeff Schaller I can use this it is not always certain the field widths are in the same way mentioned above meaning the first word can have more than 20 characters in some files. If i use the above one, sometimes i am seeing the output as
04 SHM001002016VOL01
– xrkr
11 hours ago
@xrkr. The command above can handle files contain fixed-width columns. If the width is not equal between the columns, then you would need to convert the file to fixed-width columns. After then, pipe the output to the previous command, something like:
awk -F, 'printf("%20s %20s %15sn", $1, $2, $3)' <FILE.txt> | awk 'BEGIN FIELDWIDTHS = "16 40" /SHM/ print $2'
– Goro
3 hours ago
@xrkr. The command above can handle files contain fixed-width columns. If the width is not equal between the columns, then you would need to convert the file to fixed-width columns. After then, pipe the output to the previous command, something like:
awk -F, 'printf("%20s %20s %15sn", $1, $2, $3)' <FILE.txt> | awk 'BEGIN FIELDWIDTHS = "16 40" /SHM/ print $2'
– Goro
3 hours ago
 |Â
show 2 more comments
up vote
3
down vote
awk 'NR==1 print $2' filename && awk 'NR>1' filename | sed 's/[[:space:]]*//g'
Output:
ASHM001002003VOL01
BSHM001002003VOL02
CSHM001002003VOL03
DSHM001002003VOL03_DUP
ESHM001002003VOL04
FSHM001002003VOL05
GSHM001002003VOL06_
HSHM001002003VOL07
That prints the second column on the first line, the following lines, and then removes whitespace to fix the formatting and return the output you want.
you don't need to call awk multiple times:awk 'NR == 1 print $2 NR > 1 print $1'
. This can even be collapsed, but at the expense of readability:awk 'print $(NR == 1 ? 2 : 1)'
– glenn jackman
17 hours ago
@glennjackman True, but it works just the same and it's roughly the same length with the removal of the whitespace.
– Nasir Riley
17 hours ago
I guess it's the principle of calling one program versus calling 3 to achieve the same result.
– glenn jackman
17 hours ago
@glennjackman I understand, but in his case, it really doesn't affect anything. If it were a script that was doing more work then I, too, would prefer your solution because it wouldn't call as many programs and it would be less tedious.
– Nasir Riley
17 hours ago
add a comment |Â
up vote
3
down vote
awk 'NR==1 print $2' filename && awk 'NR>1' filename | sed 's/[[:space:]]*//g'
Output:
ASHM001002003VOL01
BSHM001002003VOL02
CSHM001002003VOL03
DSHM001002003VOL03_DUP
ESHM001002003VOL04
FSHM001002003VOL05
GSHM001002003VOL06_
HSHM001002003VOL07
That prints the second column on the first line, the following lines, and then removes whitespace to fix the formatting and return the output you want.
you don't need to call awk multiple times:awk 'NR == 1 print $2 NR > 1 print $1'
. This can even be collapsed, but at the expense of readability:awk 'print $(NR == 1 ? 2 : 1)'
– glenn jackman
17 hours ago
@glennjackman True, but it works just the same and it's roughly the same length with the removal of the whitespace.
– Nasir Riley
17 hours ago
I guess it's the principle of calling one program versus calling 3 to achieve the same result.
– glenn jackman
17 hours ago
@glennjackman I understand, but in his case, it really doesn't affect anything. If it were a script that was doing more work then I, too, would prefer your solution because it wouldn't call as many programs and it would be less tedious.
– Nasir Riley
17 hours ago
add a comment |Â
up vote
3
down vote
up vote
3
down vote
awk 'NR==1 print $2' filename && awk 'NR>1' filename | sed 's/[[:space:]]*//g'
Output:
ASHM001002003VOL01
BSHM001002003VOL02
CSHM001002003VOL03
DSHM001002003VOL03_DUP
ESHM001002003VOL04
FSHM001002003VOL05
GSHM001002003VOL06_
HSHM001002003VOL07
That prints the second column on the first line, the following lines, and then removes whitespace to fix the formatting and return the output you want.
awk 'NR==1 print $2' filename && awk 'NR>1' filename | sed 's/[[:space:]]*//g'
Output:
ASHM001002003VOL01
BSHM001002003VOL02
CSHM001002003VOL03
DSHM001002003VOL03_DUP
ESHM001002003VOL04
FSHM001002003VOL05
GSHM001002003VOL06_
HSHM001002003VOL07
That prints the second column on the first line, the following lines, and then removes whitespace to fix the formatting and return the output you want.
answered 20 hours ago
Nasir Riley
1,597139
1,597139
you don't need to call awk multiple times:awk 'NR == 1 print $2 NR > 1 print $1'
. This can even be collapsed, but at the expense of readability:awk 'print $(NR == 1 ? 2 : 1)'
– glenn jackman
17 hours ago
@glennjackman True, but it works just the same and it's roughly the same length with the removal of the whitespace.
– Nasir Riley
17 hours ago
I guess it's the principle of calling one program versus calling 3 to achieve the same result.
– glenn jackman
17 hours ago
@glennjackman I understand, but in his case, it really doesn't affect anything. If it were a script that was doing more work then I, too, would prefer your solution because it wouldn't call as many programs and it would be less tedious.
– Nasir Riley
17 hours ago
add a comment |Â
you don't need to call awk multiple times:awk 'NR == 1 print $2 NR > 1 print $1'
. This can even be collapsed, but at the expense of readability:awk 'print $(NR == 1 ? 2 : 1)'
– glenn jackman
17 hours ago
@glennjackman True, but it works just the same and it's roughly the same length with the removal of the whitespace.
– Nasir Riley
17 hours ago
I guess it's the principle of calling one program versus calling 3 to achieve the same result.
– glenn jackman
17 hours ago
@glennjackman I understand, but in his case, it really doesn't affect anything. If it were a script that was doing more work then I, too, would prefer your solution because it wouldn't call as many programs and it would be less tedious.
– Nasir Riley
17 hours ago
you don't need to call awk multiple times:
awk 'NR == 1 print $2 NR > 1 print $1'
. This can even be collapsed, but at the expense of readability: awk 'print $(NR == 1 ? 2 : 1)'
– glenn jackman
17 hours ago
you don't need to call awk multiple times:
awk 'NR == 1 print $2 NR > 1 print $1'
. This can even be collapsed, but at the expense of readability: awk 'print $(NR == 1 ? 2 : 1)'
– glenn jackman
17 hours ago
@glennjackman True, but it works just the same and it's roughly the same length with the removal of the whitespace.
– Nasir Riley
17 hours ago
@glennjackman True, but it works just the same and it's roughly the same length with the removal of the whitespace.
– Nasir Riley
17 hours ago
I guess it's the principle of calling one program versus calling 3 to achieve the same result.
– glenn jackman
17 hours ago
I guess it's the principle of calling one program versus calling 3 to achieve the same result.
– glenn jackman
17 hours ago
@glennjackman I understand, but in his case, it really doesn't affect anything. If it were a script that was doing more work then I, too, would prefer your solution because it wouldn't call as many programs and it would be less tedious.
– Nasir Riley
17 hours ago
@glennjackman I understand, but in his case, it really doesn't affect anything. If it were a script that was doing more work then I, too, would prefer your solution because it wouldn't call as many programs and it would be less tedious.
– Nasir Riley
17 hours ago
add a comment |Â
up vote
2
down vote
You could do this as follows using Perl
:
perl -lne 'print for /w*SHMw*/g' input-file.txt
perl -lane 'print for grep /SHM/, @F' input-file.txt # assuming SHM fields r alphanumeric
Or, with the sed
editor in a POSIX-compatible manner, assuming all lines atleast one SHM
sed -ne '
s/[[:alnum:]_]*SHM[[:alnum:]_]*/
&
/;s/.*n(.*n)/1/;P;/n$/!D
' input.txt
Output:
ASHM001002003VOL01
BSHM001002003VOL02
CSHM001002003VOL03
DSHM001002003VOL03_DUP
ESHM001002003VOL04
FSHM001002003VOL05
GSHM001002003VOL06_
add a comment |Â
up vote
2
down vote
You could do this as follows using Perl
:
perl -lne 'print for /w*SHMw*/g' input-file.txt
perl -lane 'print for grep /SHM/, @F' input-file.txt # assuming SHM fields r alphanumeric
Or, with the sed
editor in a POSIX-compatible manner, assuming all lines atleast one SHM
sed -ne '
s/[[:alnum:]_]*SHM[[:alnum:]_]*/
&
/;s/.*n(.*n)/1/;P;/n$/!D
' input.txt
Output:
ASHM001002003VOL01
BSHM001002003VOL02
CSHM001002003VOL03
DSHM001002003VOL03_DUP
ESHM001002003VOL04
FSHM001002003VOL05
GSHM001002003VOL06_
add a comment |Â
up vote
2
down vote
up vote
2
down vote
You could do this as follows using Perl
:
perl -lne 'print for /w*SHMw*/g' input-file.txt
perl -lane 'print for grep /SHM/, @F' input-file.txt # assuming SHM fields r alphanumeric
Or, with the sed
editor in a POSIX-compatible manner, assuming all lines atleast one SHM
sed -ne '
s/[[:alnum:]_]*SHM[[:alnum:]_]*/
&
/;s/.*n(.*n)/1/;P;/n$/!D
' input.txt
Output:
ASHM001002003VOL01
BSHM001002003VOL02
CSHM001002003VOL03
DSHM001002003VOL03_DUP
ESHM001002003VOL04
FSHM001002003VOL05
GSHM001002003VOL06_
You could do this as follows using Perl
:
perl -lne 'print for /w*SHMw*/g' input-file.txt
perl -lane 'print for grep /SHM/, @F' input-file.txt # assuming SHM fields r alphanumeric
Or, with the sed
editor in a POSIX-compatible manner, assuming all lines atleast one SHM
sed -ne '
s/[[:alnum:]_]*SHM[[:alnum:]_]*/
&
/;s/.*n(.*n)/1/;P;/n$/!D
' input.txt
Output:
ASHM001002003VOL01
BSHM001002003VOL02
CSHM001002003VOL03
DSHM001002003VOL03_DUP
ESHM001002003VOL04
FSHM001002003VOL05
GSHM001002003VOL06_
answered 19 hours ago
Rakesh Sharma
56513
56513
add a comment |Â
add a comment |Â
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
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f468585%2fmatch-pattern-in-a-file-and-print-the-output%23new-answer', 'question_page');
);
Post as a guest
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
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
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