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

The name of the pictureThe name of the pictureThe name of the pictureClash 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 if stat can separate the output pathnames by /null as find does and if awk can accept input field separator null.


  • why does my setup of OFS to | not work?


Thanks.







share|improve this question

















  • 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










  • 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















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 if stat can separate the output pathnames by /null as find does and if awk can accept input field separator null.


  • why does my setup of OFS to | not work?


Thanks.







share|improve this question

















  • 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










  • 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













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 if stat can separate the output pathnames by /null as find does and if awk can accept input field separator null.


  • why does my setup of OFS to | not work?


Thanks.







share|improve this question













$ 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 if stat can separate the output pathnames by /null as find does and if awk can accept input field separator null.


  • why does my setup of OFS to | not work?


Thanks.









share|improve this question












share|improve this question




share|improve this question








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 $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










  • It seems the use of null is problematic: see this insightful answer.
    – simlev
    Jun 14 at 15:47













  • 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










  • 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








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











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.



  1. Use string concatenation: print $2"|"$3.

  2. Define OFS="|" and then print $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'





share|improve this answer























  • 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










  • 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










Your Answer







StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "106"
;
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',
convertImagesToLinks: false,
noModals: false,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);








 

draft saved


draft discarded


















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






























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.



  1. Use string concatenation: print $2"|"$3.

  2. Define OFS="|" and then print $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'





share|improve this answer























  • 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










  • 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














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.



  1. Use string concatenation: print $2"|"$3.

  2. Define OFS="|" and then print $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'





share|improve this answer























  • 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










  • 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












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.



  1. Use string concatenation: print $2"|"$3.

  2. Define OFS="|" and then print $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'





share|improve this answer















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.



  1. Use string concatenation: print $2"|"$3.

  2. Define OFS="|" and then print $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'






share|improve this answer















share|improve this answer



share|improve this answer








edited Jun 14 at 16:34


























answered Jun 14 at 15:19









simlev

47019




47019











  • 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










  • 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
















  • 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










  • 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















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












 

draft saved


draft discarded


























 


draft saved


draft discarded














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













































































Popular posts from this blog

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

Displaying single band from multi-band raster using QGIS

How many registers does an x86_64 CPU actually have?