Bash script - printing range of lines from text file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Bash script - printing range of lines from text file
# 1  
Old 11-20-2014
Bash script - printing range of lines from text file

I'm working on a new exercise that calls for a script that will take in two arguments on the command line (representing the range of line numbers) and will subsequently print those lines from a a specified file. Command line would look like this: ./lines_script.bash 5 15 <file.txt. The script would then spit out lines 5-15 from the text file (including the line number). The only caveat - I'm not supposed to use sed, awk, grep, head or tail.

Here's what I have so far:
Code:
COUNT=($1-1)
while read -r line; do
COUNT=$(( $COUNT +1))
if [$COUNT >= $2]; then
echo $COUNT
fi
done

When I run this on the command line- ./lines_script.bash 5 15 <file.txt it spits this garbage back at me:

./lines_script.bash: line 11: [20: command not found
./lines_script.bash: line 11: [21: command not found
./lines_script.bash: line 11: [22: command not found
./lines_script.bash: line 11: [23: command not found
./lines_script.bash: line 11: [24: command not found
./lines_script.bash: line 11: [25: command not found
./lines_script.bash: line 11: [26: command not found
./lines_script.bash: line 11: [27: command not found
./lines_script.bash: line 11: [28: command not found
./lines_script.bash: line 11: [29: command not found
./lines_script.bash: line 11: [30: command not found
# 2  
Old 11-20-2014
[ and ] must have a spaces before and after them
This User Gave Thanks to Chubler_XL For This Post:
# 3  
Old 11-20-2014
Chubler! Thank you, I got that patched up, though I'm still getting garbage back.

Code:
COUNT=($1-1)
while read -r line; do
COUNT=$(( $COUNT +1))
if [ $COUNT >= $2 ]
then
echo "$COUNT"
fi
done

# 4  
Old 11-21-2014
Working fine for me - what garbage do you get? And what would you expect, respectively?
# 5  
Old 11-21-2014
Quote:
Originally Posted by ksmarine1980
Command line would look like this: ./lines_script.bash 5 15 <file.txt
Try this code (note it needs to be "hardened", e.g. checking if $1 and $2 were supplied, is $1 less than $2, etc.):
Code:
#!/bin/bash

ARGSTART=$1
ARGSTOP=$2

COUNT=0

while IFS= read -r line; do
 COUNT=$((COUNT+1))

  if [ $COUNT -ge $ARGSTART ] && [ $COUNT -le $ARGSTOP ]; then
   echo "$COUNT: $line"
  fi

done

Demo:
Code:
$ man bash | head -15 >bash.txt
$ ./script.sh 5 12 <bash.txt
5: NAME
6:        bash - GNU Bourne-Again SHell
7: 
8: SYNOPSIS
9:        bash [options] [file]
10: 
11: COPYRIGHT
12:        Bash is Copyright (C) 1989-2011 by the Free Software Foundation, Inc.
$

---------- Post updated at 06:24 PM ---------- Previous update was at 06:18 PM ----------

Quote:
Originally Posted by RudiC
what garbage do you get?
Don't know what OP gets, but I got some unary operator expected
This User Gave Thanks to junior-helper For This Post:
# 6  
Old 11-21-2014
Thank you, Junior. I'm trying that out now. And yes - I'm getting unary operator expected errors with my script.

---------- Post updated at 11:33 AM ---------- Previous update was at 11:18 AM ----------

That seems to have done the trick, junior-helper! Thank you.

The one thing I don't understand - why did you use "while IFS= read -r line; do" instead of "while read -r line; do"? I'm trying to make sure I understand what I'm doing and why it works. Thanks!
# 7  
Old 11-21-2014
The IFS= sets IFS empty for the following read command, so the read does no word splitting. This ensures that leading space characters are not suppressed.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Match text to lines in a file, iterate backwards until text or text substring matches, print to file

hi all, trying this using shell/bash with sed/awk/grep I have two files, one containing one column, the other containing multiple columns (comma delimited). file1.txt abc12345 def12345 ghi54321 ... file2.txt abc1,text1,texta abc,text2,textb def123,text3,textc gh,text4,textd... (6 Replies)
Discussion started by: shogun1970
6 Replies

2. UNIX for Beginners Questions & Answers

Print a python script down a list in a text file without printing a lot combinations

In a python script I have 2 files printing side by side on the same line. I want to have 1 of the files to be already displayed at once while the other file print down the list in the file and it still will produce new lines. I want to do it like that to reduce printing a lot of lines and... (0 Replies)
Discussion started by: bigvito19
0 Replies

3. Shell Programming and Scripting

Printing multiple lines on the same line between specific text

This is an extract from a large file. The lines that start with fc are ports on a fabric switch. In between each fc port there is information about the port. fc2/12 is up Port description is SEIEDISCOVER-3 Speed is 4 Gbps fc2/13 is down (Administratively down) fc2/14 is up Port... (1 Reply)
Discussion started by: kieranfoley
1 Replies

4. Shell Programming and Scripting

Bash script to replace text file from a lookup file

Hi. I need assistance with the replacing of text into a specific file via a bash script. My bash script, once run, currently provides a menu of computer names to choose.The script copies onto my system various files, depending what computer was selected in the menu.This is working OK. Now, I... (1 Reply)
Discussion started by: jonesn2000
1 Replies

5. Shell Programming and Scripting

Bash script to send lines of file to new file based on Regex

I have a file that looks like this: cat includes CORP-CRASHTEST-BU e:\crashplan\ CORP-TEST /usr/openv/java /usr/openv/logs /usr/openv/man CORP-LABS_TEST /usr/openv/java /usr/openv/logs /usr/openv/man What I want to do is make three new files with just those selections. So the three... (4 Replies)
Discussion started by: newbie2010
4 Replies

6. Shell Programming and Scripting

bash script range calculations

Hi, I have data in the following form: AB001 10 AB002 9 AB003 9 etc AB200 5 What I need to do is sum up the second value according to groups of the first, i.e. AB001 to AB030 the total being X, AB031 to AB050 the total being Y etc (there are 5 AB ranges of different sizes). I'm sure... (3 Replies)
Discussion started by: chrissycc
3 Replies

7. Shell Programming and Scripting

Bash script to test IP range on server

Hello, We have to configure servers with a range of IPs which is in itself a subject for another script assistance request -but- we have run into quite a few IP ranges with routing problems lately. I've been trying to figure out the best way to test a range of IPs, I mean, manually it's not... (4 Replies)
Discussion started by: boxgoboom
4 Replies

8. Shell Programming and Scripting

Searching the lines within a range of time period in a text file

Dear All, Please advice me, I have a text file with one field date and time like below given. I need to find out the lines whchi content the time stamp between Wed May 26 11:03:11 2010 and Wed May 26 11:03:52 2010 both can be included, using awk command which could be an interactive so that I... (6 Replies)
Discussion started by: chinmayadalai
6 Replies

9. Shell Programming and Scripting

[bash help]Adding multiple lines of text into a specific spot into a text file

I am attempting to insert multiple lines of text into a specific place in a text file based on the lines above or below it. For example, Here is a portion of a zone file. IN NS ns1.domain.tld. IN NS ns2.domain.tld. IN ... (2 Replies)
Discussion started by: cdn_humbucker
2 Replies

10. Shell Programming and Scripting

Bash Script w/ IP Range

Is there a basic IP range script or a site that has basic script written? I am making a script that is looking for a device on my network threw HTTP://. I and using a for loop with wget to download the page and using grep to search the code for the serial number. My range is 172.16.x.x/16 I... (3 Replies)
Discussion started by: captaindoogles
3 Replies
Login or Register to Ask a Question