How to get the line number from while read


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to get the line number from while read
# 1  
Old 11-29-2007
How to get the line number from while read

I have a file TXTPROCESS.TXT.20071129 in which line 1 contains the file name and rest of the records are data.

The file data looks like this:

TXTPROCESS.TXT.20071129
DIVD20071129
INTR20071129
BALN20071129

From line 2 onwards the first 4 characters defines individual process. What I need to do is read through the line and check if it is not line 1 then exectute individual process. My problem is I don't know how to check the line number. This code gives me one line at time for all the lines.

cat TXTPROCESS.TXT.20071129 | while read LINE
do
echo $LINE
done

Any idea?

Thanks
# 2  
Old 11-29-2007
Hi

Wont this suffice?

count=0
cat TXTPROCESS.TXT.20071129 | while read file
do
if [ $count -eq 0 ];then
(count+=1))
continue
fi
echo $line
done
# 3  
Old 11-30-2007
its much more comfortable with awk

try this,

Code:
awk '{ print $0, NR }' b

this prints the content read from a line and the line number

to skip first line and print only the first 4 characters from the remaining
Code:
awk ' NR != 1 { print substr($0, 1, 4) }' b

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to read a file starting at certain line number?

I am new to ksh scripts. I would like to be able to read a file line by line from a certain line number. I have a specific line number saved in a variable, say $lineNumber. How can I start reading the file from the line number saved in $lineNumber? Thanks! (4 Replies)
Discussion started by: dcowboys13
4 Replies

2. Shell Programming and Scripting

Read line with particular number of lines

Hi all, I have a file sample.txt abc asd adf daf adw add adv wdf I want to control the number of lines to read Like if i give input as ./script_name 2 5 required output asd adf daf (2 Replies)
Discussion started by: krux_rap
2 Replies

3. Shell Programming and Scripting

Write $line number into textfile and read from line number

Hello everyone, I don't really know anything about scripting, but I have to manage to make this script, out of necessity. #!/bin/bash while read -r line; do #I'm reading from a big wordlist instructions using $line done Is there a way to automatically write the $line number the script... (4 Replies)
Discussion started by: bobylapointe
4 Replies

4. Shell Programming and Scripting

How to read from specific line number in shell script?

I have a text file which is having 30000 lines in it. I have to create a xml file for each 10000 lines until all the lines in the text files are written. Also please help how can i get number of lines in the text file in a shell variable? (19 Replies)
Discussion started by: vel4ever
19 Replies

5. UNIX for Dummies Questions & Answers

How to read contents of a file from a given line number upto line number again specified by user

Hello Everyone. I am trying to display contains of a file from a specific line to a specific line(let say, from line number 3 to line number 5). For this I got the shell script as shown below: if ; then if ; then tail +$1 $3 | head -n $2 else ... (5 Replies)
Discussion started by: grc
5 Replies

6. Shell Programming and Scripting

bash: read file line by line (lines have '\0') - not full line has read???

I am using the while-loop to read a file. The file has lines with null-terminated strings (words, actually.) What I have by that reading - just a first word up to '\0'! I need to have whole string up to 'new line' - (LF, 10#10, 16#A) What I am doing wrong? #make file 'grb' with... (6 Replies)
Discussion started by: alex_5161
6 Replies

7. Shell Programming and Scripting

Read the specified line number from file

Hi Guys, I am new to unix. Actually i want help in writing an single command where i can actually read specific line number in file where the line number will be passed to command as parameter. ex. 1 a 2 b 3 c 4 d And to my command i pass as 2. so i should get output as 2 b ... (15 Replies)
Discussion started by: kam786sim
15 Replies

8. Shell Programming and Scripting

how to get the data from line number 1 to line number 100 of a file

Hi Everybody, I am trying to write a script that will get some perticuler data from a file and redirect to a file. My Question is, I have a Very huge file,In that file I have my required data is started from 25th line and it will ends in 100th line. I know the line numbers, I need to get all... (9 Replies)
Discussion started by: Anji
9 Replies

9. Shell Programming and Scripting

Adding a columnfrom a specifit line number to a specific line number

Hi, I have a huge file & I want to add a specific text in column. But I want to add this text from a specific line number to a specific line number & another text in to another range of line numbers. To be more specific: lets say my file has 1000 lines & 4 Columns. I want to add text "Hello"... (2 Replies)
Discussion started by: Ezy
2 Replies

10. Shell Programming and Scripting

Appending line number to each line and getting total number of lines

Hello, I need help in appending the line number of each line to the file and also to get the total number of lines. Can somebody please help me. I have a file say: abc def ccc ddd ffff The output should be: Instance1=abc Instance2=def Instance3=ccc Instance4=ddd Instance5=ffff ... (2 Replies)
Discussion started by: chiru_h
2 Replies
Login or Register to Ask a Question