Sponsored Content
Top Forums Shell Programming and Scripting How to read a file starting at certain line number? Post 302950973 by wisecracker on Thursday 30th of July 2015 05:30:56 PM
Old 07-30-2015
Longhand using builtins only and has a start and finish to select any or all lines.
If you have no idea how many lines there are then just give $2 a rediculously large value, say 1000000...
OSX 10.7.5, default bash terminal.
Code:
#!/bin/bash
# line_no
# Usage: line_no start finish /full/path/to/filename /full/path/to/outfile
# Note:- There is no user error correction code here so don't make any typos.
ifs_str="$IFS"
IFS=$'\n'
# Working part.
n=1
while read -r line
do
	if [ $n -ge $1 ] && [ $n -le $2 ]
	then
		echo -E $line
	fi
	n=$(($n+1))
done < $3 > $4
# End of working part.
cat $4
IFS="$ifs_str"
exit

Results...
Code:
Last login: Thu Jul 30 22:11:35 on ttys000
AMIGA:barrywalker~> cd Desktop/Code/Shell
AMIGA:barrywalker~/Desktop/Code/Shell> ./line_no 1 1 ~/AudioScope.sh /tmp/text
#!/bin/bash
AMIGA:barrywalker~/Desktop/Code/Shell> ./line_no 3470 3489 ~/AudioScope.sh /tmp/text
#               |        /    --+--     /         | +     /      ------------
#               |     R1 \     / \ D1   \ R2     === C2   \ R3
#               |        /    '---'     /        -+-      /
#               |        \      |       \         |       \ 
#               |        /      |       /         o       /
#               |        |      |       |         |       |       Y
#               +--------o------o-------o---------o---o---o-------O -VE.
#                              Pseudo Ground. ----> __|__
#                                             _GND_ /////
# Parts List:-
# C1 ......... 1 uF, 50V.
# C2 ......... 10 uF, electrolytic, 10V, IMPORTANT! SEE TEXT.
# R1 ......... 33R, 1/8W, 5% tolerance resistor.
# R2 ......... 1M, 1/8W, 5% tolerance resistor.
# R3 ......... 100K, 1/8W, 5% tolerance resistor.
# D1, D2 ..... OA90 or any similar germanium diode.
# 3.2mm standard STEREO jack plug for headset socket.
# Coaxial connecting cable.
# Sundries as required, stripboard, etc, (similar to above).
# #########################################################
AMIGA:barrywalker~/Desktop/Code/Shell> _

EDIT:
Hmm, just thought my Android phone does not have commands like 'tail', etc, so this could be useful...
Decided to remove variable names and use $1, $2, $3 and $4 directly instead.

Last edited by wisecracker; 07-31-2015 at 04:15 AM.. Reason: See above.
 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

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... (2 Replies)
Discussion started by: skymirror
2 Replies

2. 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

3. 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

4. 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

5. Shell Programming and Scripting

Read .txt file and dropping lines starting with #

Hi All, I have a .txt file with some contents as below: Hi How are you? # Fine and you? I want a script file which reads the .txt file and output the lines which does not start with #. Hi How are you? Help is highly appreciated. Please use code tags when posting data and... (5 Replies)
Discussion started by: bghosh
5 Replies

6. Shell Programming and Scripting

replace line starting with not a number

Dear users, I have a file like this: geometry,geometry_vertex_count,Id,strnum,platecode,datatype,dtnum,refnum,appearance,disappeara,color,geogdesc,datatype_ft_style,import_notes "<LineString><coordinates>-130.6539,51.5103,0 -130.7708,51.6287,0 -130.8356,51.6832,0 -130.9211,51.7772,0... (5 Replies)
Discussion started by: Gery
5 Replies

7. 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

8. UNIX for Advanced & Expert Users

Read file and skip the line starting with #

Hi all, I'm new in unix. Need some help here. I have a file called server.cfg which contains the servers name, if I don't want to run on that server, I'll put a "#" infront it. username1@hostname.com username2@hostname.com #username3@hostname.com #username4@hostname.com... (17 Replies)
Discussion started by: beezy
17 Replies

9. Shell Programming and Scripting

With script bash, read file line per line starting at the end

Hello, I'm works on Ubuntu server My goal : I would like to read file line per line, but i want to started at the end of file. Currently, I use instructions : while read line; do COMMAND done < /var/log/apache2/access.log But, the first line, i don't want this. The file is long... (5 Replies)
Discussion started by: Fuziion
5 Replies

10. UNIX for Dummies Questions & Answers

How to grep a line not starting with # from a file (there are two lines starting with # and normal)?

e.g. File name: File.txt cat File.txt Result: #INBOUND_QUEUE=FAQ1 INBOUND_QUEUE=FAQ2 I want to get the value for one which is not commented out. Thanks, (3 Replies)
Discussion started by: Tanu
3 Replies
gets(n) 						       Tcl Built-In Commands							   gets(n)

__________________________________________________________________________________________________________________________________________________

NAME
gets - Read a line from a channel SYNOPSIS
gets channelId ?varName? _________________________________________________________________ DESCRIPTION
This command reads the next line from channelId, returns everything in the line up to (but not including) the end-of-line character(s), and discards the end-of-line character(s). ChannelId must be an identifier for an open channel such as the Tcl standard input channel (stdin), the return value from an invocation of open or socket, or the result of a channel creation command provided by a Tcl extension. The channel must have been opened for input. If varName is omitted the line is returned as the result of the command. If varName is specified then the line is placed in the variable by that name and the return value is a count of the number of characters returned. If end of file occurs while scanning for an end of line, the command returns whatever input is available up to the end of file. If chan- nelId is in nonblocking mode and there is not a full line of input available, the command returns an empty string and does not consume any input. If varName is specified and an empty string is returned in varName because of end-of-file or because of insufficient data in non- blocking mode, then the return count is -1. Note that if varName is not specified then the end-of-file and no-full-line-available cases can produce the same results as if there were an input line consisting only of the end-of-line character(s). The eof and fblocked commands can be used to distinguish these three cases. EXAMPLE
This example reads a file one line at a time and prints it out with the current line number attached to the start of each line. set chan [open "some.file.txt"] set lineNumber 0 while {[gets $chan line] >= 0} { puts "[incr lineNumber]: $line" } close $chan SEE ALSO
file(n), eof(n), fblocked(n), Tcl_StandardChannels(3) KEYWORDS
blocking, channel, end of file, end of line, line, nonblocking, read Tcl 7.5 gets(n)
All times are GMT -4. The time now is 03:12 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy