Finding row number along with length of row


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Finding row number along with length of row
# 1  
Old 03-21-2013
Finding row number along with length of row

I have a fixed length file and I want to find out row number along with row length.

I have a program that give me the line length if it satisfy the condition; but i would like to add row number as well?

How do I do that?

HTML Code:
while IFS= read -r line; do
if [ ${#line} == 4499 ]; then
 echo ${line}
 echo ${#line}
 echo "You have access!thereis a line length of 4499"
fi
done < input.dat
Thanks ,
Prince
# 2  
Old 03-21-2013
Code:
awk 'length($0)==4499 {print FNR, " has length 4499"}' input.dat

# 3  
Old 03-21-2013
Certain shells - you regrettably don't mention your's - allow to insert an easily incremented counter like ((cnt++)) after each read...
# 4  
Old 03-21-2013
i am getting error, i tried with bash and without as well.

Code:
 
bash-3.00$ awk 'length($0==4499) {print FNR, " has length 4499"}' input.dat
awk: syntax error near line 1
awk: bailing out near line 1


HTML Code:
$  awk 'length($0==4499) {print FNR, " has length 4499"}' input.dat
awk: syntax error near line 1
awk: bailing out near line 1
---------- Post updated at 06:31 PM ---------- Previous update was at 06:19 PM ----------

is not possible to add row number in the following code?

Code:
while IFS= read -r line; do
if [ ${#line} == 4499 ]; then
 echo ${line}
 echo ${#line}
 echo "You have access!thereis a line length of 4499"
fi
done < input.dat

# 5  
Old 03-21-2013
If you are on SunOS or Solaris, use nawk instead.

By the way in your script you can keep a counter to count the number of records:
Code:
c=$(( c + 1 ))

# 6  
Old 03-22-2013
Quote:
Originally Posted by princetd001
. . .
is not possible to add row number in the following code?
. . .
Well, princetd001, you don't seem to read your thread. See post#3, which is equivalent to what yoda proposes in post#5.
# 7  
Old 03-22-2013
I added
Code:
c=$(( c + 1 ))

It throws the following error

Code:
ksh[2]:  c + 1 : bad number

Code:
while IFS= read -r line; do
c=$(( c + 1 ))
if [ ${#line} == 4499 ]; then
 echo ${line}
 echo ${#line}
echo "You have a french character at  "${#c}
 echo "You have access!thereis a line length of 4499"
fi
done < input.dat

 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Keep only the closet match of timestamped row (include headers) from file1 to precede file2 row/s

This is a question that is related to one I had last August when I was trying to sort/merge two files by millsecond time column (in this case column 6). The script (below) that helped me last august by RudiC solved the puzzle of sorting/merging two files by time, except it gets lost when the... (0 Replies)
Discussion started by: aachave1
0 Replies

2. Shell Programming and Scripting

Splitting single row into multiple rows based on for every 10 digits of last field of the row

Hi ALL, We have requirement in a file, i have multiple rows. Example below: Input file rows 01,1,102319,0,0,70,26,U,1,331,000000113200000011920000001212 01,1,102319,0,1,80,20,U,1,241,00000059420000006021 I need my output file should be as mentioned below. Last field should split for... (4 Replies)
Discussion started by: kotra
4 Replies

3. UNIX for Beginners Questions & Answers

Keep only the closet match of timestamped row (include headers) from file1 to precede file2 row/s

My original files are like this below and I distinguish them from the AP_ID (file1 has 572 and file2 has 544). Also, the header on file1 has “G_” pre-pended. NOTE: these are only snippets of very large files and much of the data is not present here. Original File 1: ... (36 Replies)
Discussion started by: aachave1
36 Replies

4. Shell Programming and Scripting

Bash - delete from csv all the row if the first column is length >

Hi guys, i have a csv file like: USERID;COG;DESCR;FIL;OFF user001;user;test1;001;A01 user002;user;test2;002;A02 user0003;user;test3;003;A03 user004;user;test4;004;A04 user0005;user;test5;005;A05 etc.. I need to read line for line and, if value of first column is > 7 char (in this example... (4 Replies)
Discussion started by: kamose
4 Replies

5. Shell Programming and Scripting

Add Row from First Row (Split Row)

HI Guys, I have Below Input :- RepigA_hteis522 ReptCfiEtrBsCll_aofe MSL04_MSL2_A25_1A 0 9 MSL04_MSL2_A25_1B 0 9 MSL04_MSL2_A25_1C 0 9 RepigA ReptCfiEtrBsCll hteis522 aofe MSL04_MSL2_A25_1A 0 9 MSL04_MSL2_A25_1B 0 9 MSL04_MSL2_A25_1C 0 9 Split Data in two first row... (2 Replies)
Discussion started by: pareshkp
2 Replies

6. Shell Programming and Scripting

Read row number from 1 file and print that row of second file

Hi. How can I read row number from one file and print that corresponding record present at that row in another file. eg file1 1 3 5 7 9 file2 11111 22222 33333 44444 55555 66666 77777 88888 99999 (3 Replies)
Discussion started by: Abhiraj Singh
3 Replies

7. Shell Programming and Scripting

Get row number from file1 and print that row of file2

Hi. How can we print those rows of file2 which are mentioned in file1. first character of file1 is a row number.. for eg file1 1:abc 3:ghi 6:pqr file2 a abc b def c ghi d jkl e mno f pqr ... (6 Replies)
Discussion started by: Abhiraj Singh
6 Replies

8. Shell Programming and Scripting

Extend the Output length of a row

hi all, I use shell script to fetch some data's from my sybase database and i redirect my output to a file , i have some disorder in my file.my current o/p file looks like, firstrecord|secondrecord|thirdrecord|fourthrecord|fivthrecord|s ixhrecord|seventhrecord| ... (10 Replies)
Discussion started by: ulin
10 Replies

9. UNIX for Advanced & Expert Users

Finding last row

Using Linux and Bash, I have a script that outputs filenames with complete path, like this:I would like the following output:And I would like to get the filenames only. Tricky part is that I cannot predict how many levels deep the filename is located, so I cannot use standard Bash-kungfu to solve... (7 Replies)
Discussion started by: indo1144
7 Replies

10. Shell Programming and Scripting

Changing the column for a row in a text file and adding another row

Hi, I want to write a shell script which increments a particular column in a row from a text file and then adds another row below the current row with the incremented value . For Eg . if the input file has a row : abc xyz lmn 89 lm nk o p I would like the script to create something like... (9 Replies)
Discussion started by: aYankeeFan
9 Replies
Login or Register to Ask a Question