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:
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
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.):
Demo: ---------- 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:
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!
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.
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)
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)
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)
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)
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)
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)
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)
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)
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)
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)