Sponsored Content
Top Forums Shell Programming and Scripting count lines in file to variable Post 302566765 by ahamed101 on Friday 21st of October 2011 05:19:05 AM
Old 10-21-2011
You mean something like this?

Code:
line_no=$(awk '{x++} END {print x}' filename)
echo $line_no

--ahamed

PS : Please use code tags. Thank you for your co-operation.

Last edited by ahamed101; 10-21-2011 at 06:25 AM..
This User Gave Thanks to ahamed101 For This Post:
 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

setting file count to a variable

Hey guys. My goal here is to count the number of .dat files in in a directory(28 files). If 28 files exist I am ok. Having trouble doing this. Any help would b e greatly appreciated. #!/usr/bin/ksh #============================================================================= ### Define local... (3 Replies)
Discussion started by: ecupirate1998
3 Replies

2. Shell Programming and Scripting

Count number of lines in Binary file

Hi Friends Please help me out to count number of lines in binary file. It gives some wrong(less) using wc -l. Is there any other way to count lines of binary file. Thanks. (3 Replies)
Discussion started by: vanand420
3 Replies

3. Shell Programming and Scripting

count lines of file

dear all, i want to count the lines of a flat(text) file using awk.i have tried with {print NR} but its taking lot of time for a big file like 2GB file size. so i want better efficiency...so can any body please help me with some other and better awk code? Regards, Pankaj (15 Replies)
Discussion started by: panknil
15 Replies

4. Programming

to count the number of commented lines in a file

can someone guide me how to have a C pgm to count the number of commented lines? (3 Replies)
Discussion started by: naan
3 Replies

5. Shell Programming and Scripting

Count lines between two patterns inside a file

Hi, Im doing a script to find the number of lines included inside a file newly. These lines are in between #ifdef FLAG1 and #else or #endif or #else and #endif. I tried like this, awk '/#ifdef Flag1/,/#e/{print}' aa.c | wc -l awk '/#ifndef Flag1/,/#endif/{print}' aa.c | awk... (6 Replies)
Discussion started by: priyadarshini
6 Replies

6. UNIX for Dummies Questions & Answers

Count of Number of Lines in a File

Dear Members, I want to count the number of lines in a file; for that i am using the following command : FILE_LINE_COUNT=`wc -l $INT_IN/$RAW_FILE_NAME` if i do an echo on FILE_LINE_COUNT then i get 241 /home/data/testfile.txt I don't want the directory path to be displayed. Variable... (1 Reply)
Discussion started by: sandeep_1105
1 Replies

7. Shell Programming and Scripting

Trying to do a count on multiple lines in a file

Guys I am having a problem with being able to do a count of entries in a file. What I am trying to get a count of the total number of members that are listed in the files. So I need to pull the number of the lines after members. I tried using sed but it only seems to count the first... (7 Replies)
Discussion started by: scottzx7rr
7 Replies

8. Solaris

WC -l does not count all the lines in a file? HELP

I have a file that I need to merge with another like file. Normally I remove the trailer reocrd and merge the file and update the trailer record of the second file. I did a WC -l on the first file before I removed the trailer record, and again afterwards. The count came back the same. I opened the... (6 Replies)
Discussion started by: Harleyrci
6 Replies

9. Shell Programming and Scripting

If file exists count lines

Hello, Currently I have: FILE=/home/file.txt if ; then echo "File $FILE exists" else echo "File $FILE does not exist" fi exit I would like to make it such that if the file *does* exist, it performs a wc -l count of the file and then if the count is greater than 3 performs... (3 Replies)
Discussion started by: holyearth
3 Replies

10. Shell Programming and Scripting

Getting file count in a variable

Hi All I am checking for the presence of certain no of files in a directory. Only if the required no of files are present should I continue with my processing. For e.g. in the temp directory below, there are 4 files of the format sample_aa(bb)(cc)(dd)_test. I need to check the count of these... (17 Replies)
Discussion started by: swasid
17 Replies
csplit(1)							   User Commands							 csplit(1)

NAME
csplit - split files based on context SYNOPSIS
csplit [-ks] [-f prefix] [-n number] file arg1... argn DESCRIPTION
The csplit utility reads the file named by the file operand, writes all or part of that file into other files as directed by the arg oper- ands, and writes the sizes of the files. OPTIONS
The following options are supported: -f prefix Names the created files prefix00, prefix01, ..., prefixn. The default is xx00 ... xxn. If the prefix argument would create a file name exceeding 14 bytes, an error results. In that case, csplit exits with a diagnostic message and no files are created. -k Leaves previously created files intact. By default, csplit removes created files if an error occurs. -n number Uses number decimal digits to form filenames for the file pieces. The default is 2. -s Suppresses the output of file size messages. OPERANDS
The following operands are supported: file The path name of a text file to be split. If file is -, the standard input will be used. The operands arg1 ... argn can be a combination of the following: /rexp/[offset] Create a file using the content of the lines from the current line up to, but not including, the line that results from the evaluation of the regular expression with offset, if any, applied. The regular expression rexp must follow the rules for basic regular expressions. Regular expressions can include the use of '/' and '\%'. These forms must be properly quoted with single quotes, since "" is special to the shell. The optional offset must be a positive or negative integer value representing a number of lines. The integer value must be preceded by + or -. If the selection of lines from an offset expression of this type would create a file with zero lines, or one with greater than the number of lines left in the input file, the results are unspecified. After the section is created, the current line will be set to the line that results from the evaluation of the regular expression with any offset applied. The pattern match of rexp always is applied from the cur- rent line to the end of the file. %rexp%[offset] This operand is the same as /rexp/[offset], except that no file will be created for the selected section of the input file. line_no Create a file from the current line up to (but not including) the line number line_no. Lines in the file will be numbered starting at one. The current line becomes line_no. {num} Repeat operand. This operand can follow any of the operands described previously. If it follows a rexp type operand, that operand will be applied num more times. If it follows a line_no operand, the file will be split every line_no lines, num times, from that point. An error will be reported if an operand does not reference a line between the current position and the end of the file. USAGE
See largefile(5) for the description of the behavior of csplit when encountering files greater than or equal to 2 Gbyte (2**31 bytes). EXAMPLES
Example 1: Splitting and combining files This example creates four files, cobol00...cobol03. example% csplit -f cobol filename '/procedure division/' /par5./ /par16./ After editing the ``split'' files, they can be recombined as follows: example% cat cobol0[0-3] > filename Note: This example overwrites the original file. Example 2: Splitting a file into equal parts This example splits the file at every 100 lines, up to 10,000 lines. The -k option causes the created files to be retained if there are less than 10,000 lines; however, an error message would still be printed. example% csplit -k filename 100 {99} Example 3: Creating a file for separate C routines If prog.c follows the normal C coding convention (the last line of a routine consists only of a } in the first character position), this example creates a file for each separate C routine (up to 21) in prog.c. example% csplit -k prog.c '%main(%' '/^}/+1' {20} ENVIRONMENT VARIABLES
See environ(5) for descriptions of the following environment variables that affect the execution of csplit: LANG, LC_ALL, LC_COLLATE, LC_CTYPE, LC_MESSAGES, and NLSPATH. EXIT STATUS
The following exit values are returned: 0 Successful completion. >0 An error occurred. ATTRIBUTES
See attributes(5) for descriptions of the following attributes: +-----------------------------+-----------------------------+ | ATTRIBUTE TYPE | ATTRIBUTE VALUE | +-----------------------------+-----------------------------+ |Availability |SUNWesu | +-----------------------------+-----------------------------+ |CSI |Enabled | +-----------------------------+-----------------------------+ |Interface Stability |Standard | +-----------------------------+-----------------------------+ SEE ALSO
sed(1), split(1), attributes(5), environ(5), largefile(5), standards(5) DIAGNOSTICS
The diagnostic messages are self-explanatory, except for the following: arg - out of range The given argument did not reference a line between the current position and the end of the file. SunOS 5.10 4 Dec 2003 csplit(1)
All times are GMT -4. The time now is 10:52 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy