print lines with specific number of specific char

Clash Royale CLAN TAG#URR8PPP
up vote
0
down vote
favorite
I have a big text saving lots of file paths and their information
# just for demostration
/a/b/c/d/e
/a/b/c/d
/a/b/c
/a/b
/a
/b/c
/b
/c/d
/c
....
I want to show only those with specific number of specific char, for instance,
- with only one
/ - with no more than two
/
so that I extract only root folders or only to first-level subfolders.
shell text-processing
add a comment |Â
up vote
0
down vote
favorite
I have a big text saving lots of file paths and their information
# just for demostration
/a/b/c/d/e
/a/b/c/d
/a/b/c
/a/b
/a
/b/c
/b
/c/d
/c
....
I want to show only those with specific number of specific char, for instance,
- with only one
/ - with no more than two
/
so that I extract only root folders or only to first-level subfolders.
shell text-processing
Just because that looks like it could have been produced byfind, gnuâÂÂs find has a maxdepth option that you may find useful.
â Jeff Schaller
Apr 11 at 18:24
@JeffSchaller I knowfind. but I'm working on text data, not real file structure. Yourgrepsolution is perfect.
â Lee
Apr 11 at 19:00
add a comment |Â
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I have a big text saving lots of file paths and their information
# just for demostration
/a/b/c/d/e
/a/b/c/d
/a/b/c
/a/b
/a
/b/c
/b
/c/d
/c
....
I want to show only those with specific number of specific char, for instance,
- with only one
/ - with no more than two
/
so that I extract only root folders or only to first-level subfolders.
shell text-processing
I have a big text saving lots of file paths and their information
# just for demostration
/a/b/c/d/e
/a/b/c/d
/a/b/c
/a/b
/a
/b/c
/b
/c/d
/c
....
I want to show only those with specific number of specific char, for instance,
- with only one
/ - with no more than two
/
so that I extract only root folders or only to first-level subfolders.
shell text-processing
asked Apr 11 at 17:59
Lee
7692718
7692718
Just because that looks like it could have been produced byfind, gnuâÂÂs find has a maxdepth option that you may find useful.
â Jeff Schaller
Apr 11 at 18:24
@JeffSchaller I knowfind. but I'm working on text data, not real file structure. Yourgrepsolution is perfect.
â Lee
Apr 11 at 19:00
add a comment |Â
Just because that looks like it could have been produced byfind, gnuâÂÂs find has a maxdepth option that you may find useful.
â Jeff Schaller
Apr 11 at 18:24
@JeffSchaller I knowfind. but I'm working on text data, not real file structure. Yourgrepsolution is perfect.
â Lee
Apr 11 at 19:00
Just because that looks like it could have been produced by
find, gnuâÂÂs find has a maxdepth option that you may find useful.â Jeff Schaller
Apr 11 at 18:24
Just because that looks like it could have been produced by
find, gnuâÂÂs find has a maxdepth option that you may find useful.â Jeff Schaller
Apr 11 at 18:24
@JeffSchaller I know
find. but I'm working on text data, not real file structure. Your grep solution is perfect.â Lee
Apr 11 at 19:00
@JeffSchaller I know
find. but I'm working on text data, not real file structure. Your grep solution is perfect.â Lee
Apr 11 at 19:00
add a comment |Â
2 Answers
2
active
oldest
votes
up vote
2
down vote
accepted
With an extended regular expression:
grep -E '^([^/]*/[^/]*)1,2$' input
which results in these matches:
/a/b
/a
/b/c
/b
/c/d
/c
The regex syntax says:
^- anchored to the beginning of the line( ... )- group the following bits together[^/]*- any non-forward-slash character, zero or more of them/- a forward-slash[^/]*- any non-forward-slash character, zero or more of them1,2- one or two of those groups$- anchored to the end of the line
add a comment |Â
up vote
5
down vote
Awk solution:
-- with only one /:
awk -v sep=1 'BEGIN FS=OFS="/"; sep += 1 NF == sep' file
The output:
/a
/b
/c
-- with no more than two /:
awk -v sep=2 'BEGIN FS=OFS="/"; sep += 1 NF <= sep' file
The output:
/a/b
/a
/b/c
/b
/c/d
/c
sep- variable; stands for directory separator's countFSandOFS- are field separator and output field separator respectivelyNF- stands for total number of fields
If you need to combine the above approaches into a single awk command:
awk -v sep=1 'BEGIN FS=OFS="/"; sep += 1 (sep==2 && NF==sep) || (sep>2 && NF<=sep)' file
Thx. Good solutions! I chose thegreppost as answer only becausegrepis easier to understand.
â Lee
Apr 11 at 18:32
Is the setting ofOFSeven necessary in this context?
â steeldriver
Apr 11 at 20:36
@steeldriver, in this narrow context - no. But it might be so in case if some field value would be changed for some purpose.
â RomanPerekhrest
Apr 11 at 20:41
add a comment |Â
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
2
down vote
accepted
With an extended regular expression:
grep -E '^([^/]*/[^/]*)1,2$' input
which results in these matches:
/a/b
/a
/b/c
/b
/c/d
/c
The regex syntax says:
^- anchored to the beginning of the line( ... )- group the following bits together[^/]*- any non-forward-slash character, zero or more of them/- a forward-slash[^/]*- any non-forward-slash character, zero or more of them1,2- one or two of those groups$- anchored to the end of the line
add a comment |Â
up vote
2
down vote
accepted
With an extended regular expression:
grep -E '^([^/]*/[^/]*)1,2$' input
which results in these matches:
/a/b
/a
/b/c
/b
/c/d
/c
The regex syntax says:
^- anchored to the beginning of the line( ... )- group the following bits together[^/]*- any non-forward-slash character, zero or more of them/- a forward-slash[^/]*- any non-forward-slash character, zero or more of them1,2- one or two of those groups$- anchored to the end of the line
add a comment |Â
up vote
2
down vote
accepted
up vote
2
down vote
accepted
With an extended regular expression:
grep -E '^([^/]*/[^/]*)1,2$' input
which results in these matches:
/a/b
/a
/b/c
/b
/c/d
/c
The regex syntax says:
^- anchored to the beginning of the line( ... )- group the following bits together[^/]*- any non-forward-slash character, zero or more of them/- a forward-slash[^/]*- any non-forward-slash character, zero or more of them1,2- one or two of those groups$- anchored to the end of the line
With an extended regular expression:
grep -E '^([^/]*/[^/]*)1,2$' input
which results in these matches:
/a/b
/a
/b/c
/b
/c/d
/c
The regex syntax says:
^- anchored to the beginning of the line( ... )- group the following bits together[^/]*- any non-forward-slash character, zero or more of them/- a forward-slash[^/]*- any non-forward-slash character, zero or more of them1,2- one or two of those groups$- anchored to the end of the line
answered Apr 11 at 18:13
Jeff Schaller
31.1k846105
31.1k846105
add a comment |Â
add a comment |Â
up vote
5
down vote
Awk solution:
-- with only one /:
awk -v sep=1 'BEGIN FS=OFS="/"; sep += 1 NF == sep' file
The output:
/a
/b
/c
-- with no more than two /:
awk -v sep=2 'BEGIN FS=OFS="/"; sep += 1 NF <= sep' file
The output:
/a/b
/a
/b/c
/b
/c/d
/c
sep- variable; stands for directory separator's countFSandOFS- are field separator and output field separator respectivelyNF- stands for total number of fields
If you need to combine the above approaches into a single awk command:
awk -v sep=1 'BEGIN FS=OFS="/"; sep += 1 (sep==2 && NF==sep) || (sep>2 && NF<=sep)' file
Thx. Good solutions! I chose thegreppost as answer only becausegrepis easier to understand.
â Lee
Apr 11 at 18:32
Is the setting ofOFSeven necessary in this context?
â steeldriver
Apr 11 at 20:36
@steeldriver, in this narrow context - no. But it might be so in case if some field value would be changed for some purpose.
â RomanPerekhrest
Apr 11 at 20:41
add a comment |Â
up vote
5
down vote
Awk solution:
-- with only one /:
awk -v sep=1 'BEGIN FS=OFS="/"; sep += 1 NF == sep' file
The output:
/a
/b
/c
-- with no more than two /:
awk -v sep=2 'BEGIN FS=OFS="/"; sep += 1 NF <= sep' file
The output:
/a/b
/a
/b/c
/b
/c/d
/c
sep- variable; stands for directory separator's countFSandOFS- are field separator and output field separator respectivelyNF- stands for total number of fields
If you need to combine the above approaches into a single awk command:
awk -v sep=1 'BEGIN FS=OFS="/"; sep += 1 (sep==2 && NF==sep) || (sep>2 && NF<=sep)' file
Thx. Good solutions! I chose thegreppost as answer only becausegrepis easier to understand.
â Lee
Apr 11 at 18:32
Is the setting ofOFSeven necessary in this context?
â steeldriver
Apr 11 at 20:36
@steeldriver, in this narrow context - no. But it might be so in case if some field value would be changed for some purpose.
â RomanPerekhrest
Apr 11 at 20:41
add a comment |Â
up vote
5
down vote
up vote
5
down vote
Awk solution:
-- with only one /:
awk -v sep=1 'BEGIN FS=OFS="/"; sep += 1 NF == sep' file
The output:
/a
/b
/c
-- with no more than two /:
awk -v sep=2 'BEGIN FS=OFS="/"; sep += 1 NF <= sep' file
The output:
/a/b
/a
/b/c
/b
/c/d
/c
sep- variable; stands for directory separator's countFSandOFS- are field separator and output field separator respectivelyNF- stands for total number of fields
If you need to combine the above approaches into a single awk command:
awk -v sep=1 'BEGIN FS=OFS="/"; sep += 1 (sep==2 && NF==sep) || (sep>2 && NF<=sep)' file
Awk solution:
-- with only one /:
awk -v sep=1 'BEGIN FS=OFS="/"; sep += 1 NF == sep' file
The output:
/a
/b
/c
-- with no more than two /:
awk -v sep=2 'BEGIN FS=OFS="/"; sep += 1 NF <= sep' file
The output:
/a/b
/a
/b/c
/b
/c/d
/c
sep- variable; stands for directory separator's countFSandOFS- are field separator and output field separator respectivelyNF- stands for total number of fields
If you need to combine the above approaches into a single awk command:
awk -v sep=1 'BEGIN FS=OFS="/"; sep += 1 (sep==2 && NF==sep) || (sep>2 && NF<=sep)' file
edited Apr 12 at 6:41
answered Apr 11 at 18:13
RomanPerekhrest
22.4k12144
22.4k12144
Thx. Good solutions! I chose thegreppost as answer only becausegrepis easier to understand.
â Lee
Apr 11 at 18:32
Is the setting ofOFSeven necessary in this context?
â steeldriver
Apr 11 at 20:36
@steeldriver, in this narrow context - no. But it might be so in case if some field value would be changed for some purpose.
â RomanPerekhrest
Apr 11 at 20:41
add a comment |Â
Thx. Good solutions! I chose thegreppost as answer only becausegrepis easier to understand.
â Lee
Apr 11 at 18:32
Is the setting ofOFSeven necessary in this context?
â steeldriver
Apr 11 at 20:36
@steeldriver, in this narrow context - no. But it might be so in case if some field value would be changed for some purpose.
â RomanPerekhrest
Apr 11 at 20:41
Thx. Good solutions! I chose the
grep post as answer only because grep is easier to understand.â Lee
Apr 11 at 18:32
Thx. Good solutions! I chose the
grep post as answer only because grep is easier to understand.â Lee
Apr 11 at 18:32
Is the setting of
OFS even necessary in this context?â steeldriver
Apr 11 at 20:36
Is the setting of
OFS even necessary in this context?â steeldriver
Apr 11 at 20:36
@steeldriver, in this narrow context - no. But it might be so in case if some field value would be changed for some purpose.
â RomanPerekhrest
Apr 11 at 20:41
@steeldriver, in this narrow context - no. But it might be so in case if some field value would be changed for some purpose.
â RomanPerekhrest
Apr 11 at 20:41
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%2f437103%2fprint-lines-with-specific-number-of-specific-char%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
Just because that looks like it could have been produced by
find, gnuâÂÂs find has a maxdepth option that you may find useful.â Jeff Schaller
Apr 11 at 18:24
@JeffSchaller I know
find. but I'm working on text data, not real file structure. Yourgrepsolution is perfect.â Lee
Apr 11 at 19:00