LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   showing the number of strings in the output filename (https://www.linuxquestions.org/questions/programming-9/showing-the-number-of-strings-in-the-output-filename-912497/)

udiubu 11-08-2011 11:27 AM

showing the number of strings in the output filename
 
Dear all,

I just a script which cut a long txt file into different new txt files.
Since I need to be sure - as a double-checking procedure - that exactly 48 strings (i.e. lines) are included per file. Is there any simple command that would echo the number of strings per file in the output filename?

For example, here is my simple awk command that looks for strings that have the number 2 in the first column:

awk '$1 == "2"{print $0}' filename_alpha > filename_beta

I would like to have an output filename which would look like this one:

filename_beta_48

(48 would be the number of strings that have "2" in the first column)

I thank you very much for your advice.

All the best,

Udiubu

rizhun 11-08-2011 11:38 AM

Have a look at the man page for 'split':
Code:

$ man split

colucix 11-08-2011 11:39 AM

You can do that with a slightly more complex awk program:
Code:

awk '$1==2{count++; if (text) text=sprintf("%s\n%s",text,$0); else text=$0}END{print text > ("filename_beta_" count)}' filename_alpha

Juako 11-08-2011 12:28 PM

Code:

sed -rn '/^[^[:space:]]*2[^[:space:]]*[[:space:]]/p' filename_alpha | wc -l > filename_beta
Oops. you need to change the output filename, that would require Bash:

Code:

#!/bin/bash
lines=$(sed -rn '/^[^[:space:]]*2[^[:space:]]*[[:space:]]/p' $1 | tee tempfile | wc -l)
mv tempfile ${1%%alpha}beta_$lines



All times are GMT -5. The time now is 10:55 AM.