How can I separate information into fields in both input and output?

Clash Royale CLAN TAG#URR8PPP
up vote
0
down vote
favorite
$ stat -c "%Y#%n#%y" * | awk -F'#' "BEGIN OFS=" NR==1,NR==3 print $2 $3"
directory1/Blum2017-12-22 22:33:38.644178442 -0500
dir2/Ciolli2017-12-22 21:53:51.769368496 -0500
Dar2017-12-06 13:29:37.698296879 -0500
I try to set up input and output field separators.
I use
#as input separator, but a filename can contains#. Is there a better choice? I don't know ifstatcan separate the output pathnames by/nullasfinddoes and ifawkcan accept input field separatornull.why does my setup of
OFSto|not work?
Thanks.
bash awk stat
add a comment |Â
up vote
0
down vote
favorite
$ stat -c "%Y#%n#%y" * | awk -F'#' "BEGIN OFS=" NR==1,NR==3 print $2 $3"
directory1/Blum2017-12-22 22:33:38.644178442 -0500
dir2/Ciolli2017-12-22 21:53:51.769368496 -0500
Dar2017-12-06 13:29:37.698296879 -0500
I try to set up input and output field separators.
I use
#as input separator, but a filename can contains#. Is there a better choice? I don't know ifstatcan separate the output pathnames by/nullasfinddoes and ifawkcan accept input field separatornull.why does my setup of
OFSto|not work?
Thanks.
bash awk stat
1
(1) it depends - what are you trying to do, exactly? (2) because$2 $3is a string concatenation: try$2,$3
â steeldriver
Jun 13 at 22:56
Thanks. (1) I want to make sure the three pieces of information provided bystatare recognzed by awk as separate fields.
â Tim
Jun 13 at 23:27
It seems the use of null is problematic: see this insightful answer.
â simlev
Jun 14 at 15:47
add a comment |Â
up vote
0
down vote
favorite
up vote
0
down vote
favorite
$ stat -c "%Y#%n#%y" * | awk -F'#' "BEGIN OFS=" NR==1,NR==3 print $2 $3"
directory1/Blum2017-12-22 22:33:38.644178442 -0500
dir2/Ciolli2017-12-22 21:53:51.769368496 -0500
Dar2017-12-06 13:29:37.698296879 -0500
I try to set up input and output field separators.
I use
#as input separator, but a filename can contains#. Is there a better choice? I don't know ifstatcan separate the output pathnames by/nullasfinddoes and ifawkcan accept input field separatornull.why does my setup of
OFSto|not work?
Thanks.
bash awk stat
$ stat -c "%Y#%n#%y" * | awk -F'#' "BEGIN OFS=" NR==1,NR==3 print $2 $3"
directory1/Blum2017-12-22 22:33:38.644178442 -0500
dir2/Ciolli2017-12-22 21:53:51.769368496 -0500
Dar2017-12-06 13:29:37.698296879 -0500
I try to set up input and output field separators.
I use
#as input separator, but a filename can contains#. Is there a better choice? I don't know ifstatcan separate the output pathnames by/nullasfinddoes and ifawkcan accept input field separatornull.why does my setup of
OFSto|not work?
Thanks.
bash awk stat
edited Jun 14 at 15:26
asked Jun 13 at 22:13
Tim
22.5k61222401
22.5k61222401
1
(1) it depends - what are you trying to do, exactly? (2) because$2 $3is a string concatenation: try$2,$3
â steeldriver
Jun 13 at 22:56
Thanks. (1) I want to make sure the three pieces of information provided bystatare recognzed by awk as separate fields.
â Tim
Jun 13 at 23:27
It seems the use of null is problematic: see this insightful answer.
â simlev
Jun 14 at 15:47
add a comment |Â
1
(1) it depends - what are you trying to do, exactly? (2) because$2 $3is a string concatenation: try$2,$3
â steeldriver
Jun 13 at 22:56
Thanks. (1) I want to make sure the three pieces of information provided bystatare recognzed by awk as separate fields.
â Tim
Jun 13 at 23:27
It seems the use of null is problematic: see this insightful answer.
â simlev
Jun 14 at 15:47
1
1
(1) it depends - what are you trying to do, exactly? (2) because
$2 $3 is a string concatenation: try $2,$3â steeldriver
Jun 13 at 22:56
(1) it depends - what are you trying to do, exactly? (2) because
$2 $3 is a string concatenation: try $2,$3â steeldriver
Jun 13 at 22:56
Thanks. (1) I want to make sure the three pieces of information provided by
stat are recognzed by awk as separate fields.â Tim
Jun 13 at 23:27
Thanks. (1) I want to make sure the three pieces of information provided by
stat are recognzed by awk as separate fields.â Tim
Jun 13 at 23:27
It seems the use of null is problematic: see this insightful answer.
â simlev
Jun 14 at 15:47
It seems the use of null is problematic: see this insightful answer.
â simlev
Jun 14 at 15:47
add a comment |Â
1 Answer
1
active
oldest
votes
up vote
1
down vote
stat -c "%Y/%n/%y" * | awk -F'/' 'NR==1,NR==3 "$3'
Or:
stat -c "%Y/%n/%y" * | awk -F'/' 'BEGIN OFS=" NR==1,NR==3 print $2,$3'
Explanation:
Instead of #, you might want to use / (the directory separator), since it can not be part of a filename.
The input awk field separator needs to be set accordingly: -F'/'.
As already pointed out in a comment by @steeldriver, you have two ways of choosing an output field separator.
- Use string concatenation:
print $2"|"$3. - Define
OFS="|"and thenprint $2,$3.
I chose single quotes ' over double quotes " which reduces the need of escaping in this case.
Update:
Since the question now specifies that the stat output may contain the directory separator, / would not be a wise choice for the record separator. The only other character that I know of which is not allowed in filenames is NUL, but its use is problematic at least in this case. My suggestion would be to make up an awkward string that is very unlikely (although allowed) to be found as part of a filename. Accidentally, x0 (which is a representation of NUL) could be a good choice:
stat -c "%Yx0%nx0%y" * | awk -Fx0 'BEGIN OFS=" NR==1,NR==3 "$3'
Thanks. The output ofstatcan be pathnames containing/, because there can be subdirectories in the expansion of*.
â Tim
Jun 14 at 15:24
@Tim Your best bet is to use the NUL character then!
â simlev
Jun 14 at 15:27
How shall I specify NUL tostatandawk?
â Tim
Jun 14 at 15:28
I don't think it will work. You can specify a very awkward string that is unlikely to be found in filenames, such as#°°#.
â simlev
Jun 14 at 15:40
add a comment |Â
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
stat -c "%Y/%n/%y" * | awk -F'/' 'NR==1,NR==3 "$3'
Or:
stat -c "%Y/%n/%y" * | awk -F'/' 'BEGIN OFS=" NR==1,NR==3 print $2,$3'
Explanation:
Instead of #, you might want to use / (the directory separator), since it can not be part of a filename.
The input awk field separator needs to be set accordingly: -F'/'.
As already pointed out in a comment by @steeldriver, you have two ways of choosing an output field separator.
- Use string concatenation:
print $2"|"$3. - Define
OFS="|"and thenprint $2,$3.
I chose single quotes ' over double quotes " which reduces the need of escaping in this case.
Update:
Since the question now specifies that the stat output may contain the directory separator, / would not be a wise choice for the record separator. The only other character that I know of which is not allowed in filenames is NUL, but its use is problematic at least in this case. My suggestion would be to make up an awkward string that is very unlikely (although allowed) to be found as part of a filename. Accidentally, x0 (which is a representation of NUL) could be a good choice:
stat -c "%Yx0%nx0%y" * | awk -Fx0 'BEGIN OFS=" NR==1,NR==3 "$3'
Thanks. The output ofstatcan be pathnames containing/, because there can be subdirectories in the expansion of*.
â Tim
Jun 14 at 15:24
@Tim Your best bet is to use the NUL character then!
â simlev
Jun 14 at 15:27
How shall I specify NUL tostatandawk?
â Tim
Jun 14 at 15:28
I don't think it will work. You can specify a very awkward string that is unlikely to be found in filenames, such as#°°#.
â simlev
Jun 14 at 15:40
add a comment |Â
up vote
1
down vote
stat -c "%Y/%n/%y" * | awk -F'/' 'NR==1,NR==3 "$3'
Or:
stat -c "%Y/%n/%y" * | awk -F'/' 'BEGIN OFS=" NR==1,NR==3 print $2,$3'
Explanation:
Instead of #, you might want to use / (the directory separator), since it can not be part of a filename.
The input awk field separator needs to be set accordingly: -F'/'.
As already pointed out in a comment by @steeldriver, you have two ways of choosing an output field separator.
- Use string concatenation:
print $2"|"$3. - Define
OFS="|"and thenprint $2,$3.
I chose single quotes ' over double quotes " which reduces the need of escaping in this case.
Update:
Since the question now specifies that the stat output may contain the directory separator, / would not be a wise choice for the record separator. The only other character that I know of which is not allowed in filenames is NUL, but its use is problematic at least in this case. My suggestion would be to make up an awkward string that is very unlikely (although allowed) to be found as part of a filename. Accidentally, x0 (which is a representation of NUL) could be a good choice:
stat -c "%Yx0%nx0%y" * | awk -Fx0 'BEGIN OFS=" NR==1,NR==3 "$3'
Thanks. The output ofstatcan be pathnames containing/, because there can be subdirectories in the expansion of*.
â Tim
Jun 14 at 15:24
@Tim Your best bet is to use the NUL character then!
â simlev
Jun 14 at 15:27
How shall I specify NUL tostatandawk?
â Tim
Jun 14 at 15:28
I don't think it will work. You can specify a very awkward string that is unlikely to be found in filenames, such as#°°#.
â simlev
Jun 14 at 15:40
add a comment |Â
up vote
1
down vote
up vote
1
down vote
stat -c "%Y/%n/%y" * | awk -F'/' 'NR==1,NR==3 "$3'
Or:
stat -c "%Y/%n/%y" * | awk -F'/' 'BEGIN OFS=" NR==1,NR==3 print $2,$3'
Explanation:
Instead of #, you might want to use / (the directory separator), since it can not be part of a filename.
The input awk field separator needs to be set accordingly: -F'/'.
As already pointed out in a comment by @steeldriver, you have two ways of choosing an output field separator.
- Use string concatenation:
print $2"|"$3. - Define
OFS="|"and thenprint $2,$3.
I chose single quotes ' over double quotes " which reduces the need of escaping in this case.
Update:
Since the question now specifies that the stat output may contain the directory separator, / would not be a wise choice for the record separator. The only other character that I know of which is not allowed in filenames is NUL, but its use is problematic at least in this case. My suggestion would be to make up an awkward string that is very unlikely (although allowed) to be found as part of a filename. Accidentally, x0 (which is a representation of NUL) could be a good choice:
stat -c "%Yx0%nx0%y" * | awk -Fx0 'BEGIN OFS=" NR==1,NR==3 "$3'
stat -c "%Y/%n/%y" * | awk -F'/' 'NR==1,NR==3 "$3'
Or:
stat -c "%Y/%n/%y" * | awk -F'/' 'BEGIN OFS=" NR==1,NR==3 print $2,$3'
Explanation:
Instead of #, you might want to use / (the directory separator), since it can not be part of a filename.
The input awk field separator needs to be set accordingly: -F'/'.
As already pointed out in a comment by @steeldriver, you have two ways of choosing an output field separator.
- Use string concatenation:
print $2"|"$3. - Define
OFS="|"and thenprint $2,$3.
I chose single quotes ' over double quotes " which reduces the need of escaping in this case.
Update:
Since the question now specifies that the stat output may contain the directory separator, / would not be a wise choice for the record separator. The only other character that I know of which is not allowed in filenames is NUL, but its use is problematic at least in this case. My suggestion would be to make up an awkward string that is very unlikely (although allowed) to be found as part of a filename. Accidentally, x0 (which is a representation of NUL) could be a good choice:
stat -c "%Yx0%nx0%y" * | awk -Fx0 'BEGIN OFS=" NR==1,NR==3 "$3'
edited Jun 14 at 16:34
answered Jun 14 at 15:19
simlev
47019
47019
Thanks. The output ofstatcan be pathnames containing/, because there can be subdirectories in the expansion of*.
â Tim
Jun 14 at 15:24
@Tim Your best bet is to use the NUL character then!
â simlev
Jun 14 at 15:27
How shall I specify NUL tostatandawk?
â Tim
Jun 14 at 15:28
I don't think it will work. You can specify a very awkward string that is unlikely to be found in filenames, such as#°°#.
â simlev
Jun 14 at 15:40
add a comment |Â
Thanks. The output ofstatcan be pathnames containing/, because there can be subdirectories in the expansion of*.
â Tim
Jun 14 at 15:24
@Tim Your best bet is to use the NUL character then!
â simlev
Jun 14 at 15:27
How shall I specify NUL tostatandawk?
â Tim
Jun 14 at 15:28
I don't think it will work. You can specify a very awkward string that is unlikely to be found in filenames, such as#°°#.
â simlev
Jun 14 at 15:40
Thanks. The output of
stat can be pathnames containing /, because there can be subdirectories in the expansion of *.â Tim
Jun 14 at 15:24
Thanks. The output of
stat can be pathnames containing /, because there can be subdirectories in the expansion of *.â Tim
Jun 14 at 15:24
@Tim Your best bet is to use the NUL character then!
â simlev
Jun 14 at 15:27
@Tim Your best bet is to use the NUL character then!
â simlev
Jun 14 at 15:27
How shall I specify NUL to
stat and awk?â Tim
Jun 14 at 15:28
How shall I specify NUL to
stat and awk?â Tim
Jun 14 at 15:28
I don't think it will work. You can specify a very awkward string that is unlikely to be found in filenames, such as
#°°#.â simlev
Jun 14 at 15:40
I don't think it will work. You can specify a very awkward string that is unlikely to be found in filenames, such as
#°°#.â simlev
Jun 14 at 15:40
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%2f449680%2fhow-can-i-separate-information-into-fields-in-both-input-and-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
1
(1) it depends - what are you trying to do, exactly? (2) because
$2 $3is a string concatenation: try$2,$3â steeldriver
Jun 13 at 22:56
Thanks. (1) I want to make sure the three pieces of information provided by
statare recognzed by awk as separate fields.â Tim
Jun 13 at 23:27
It seems the use of null is problematic: see this insightful answer.
â simlev
Jun 14 at 15:47