LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 11-08-2011, 11:27 AM   #1
udiubu
Member
 
Registered: Oct 2011
Posts: 73

Rep: Reputation: Disabled
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
 
Old 11-08-2011, 11:38 AM   #2
rizhun
Member
 
Registered: Jun 2005
Location: England
Distribution: Ubuntu, SLES, AIX
Posts: 268

Rep: Reputation: 47
Have a look at the man page for 'split':
Code:
$ man split
 
Old 11-08-2011, 11:39 AM   #3
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
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
 
Old 11-08-2011, 12:28 PM   #4
Juako
Member
 
Registered: Mar 2010
Posts: 202

Rep: Reputation: 84
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

Last edited by Juako; 11-08-2011 at 12:37 PM.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
extract last number from filename csegau Linux - Newbie 8 05-28-2010 07:24 AM
showing emacs filename/buffer in kde panel? lublum1 Linux - General 6 09-21-2008 04:26 AM
LXer: Number Pools And Guaranteed Combinations Within Fixed Strings LXer Syndicated Linux News 0 09-02-2008 12:20 AM
number strings in fortran 95 Lordandmaker Programming 1 03-31-2006 03:28 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 12:27 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration