LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 04-14-2013, 04:44 AM   #1
woodyhead
LQ Newbie
 
Registered: Apr 2013
Posts: 6

Rep: Reputation: Disabled
Talking Shell Script Help


I'm new to shell scripting and I want to be able to print the lines in between the head and tail of a .txt file.

for example, if I wrote on the command line: ./scriptname filename 4 10
would print lines 5 6 7 8 9 10?


cheers!
 
Old 04-14-2013, 05:03 AM   #2
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
What have you tried this far and where are you struggling with?
 
Old 04-14-2013, 05:07 AM   #3
woodyhead
LQ Newbie
 
Registered: Apr 2013
Posts: 6

Original Poster
Rep: Reputation: Disabled
So far I am able to print the head and the tail, the problem is when it comes to printing the lines inbetween and the head and tail..

my code so far...
#!/bin/bash

echo "Printing head of $1"
head $1

echo "Printing tail of $1"
tail $1

thank you!
 
Old 04-14-2013, 05:10 AM   #4
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
You should have a look at the sed command, which can print ranges.

Sed resources:
 
1 members found this post helpful.
Old 04-14-2013, 07:15 AM   #5
vineethsp
LQ Newbie
 
Registered: Mar 2013
Location: India
Distribution: RedHat Enterprise Linux Server Edition 5.5
Posts: 13

Rep: Reputation: Disabled
Quote:
Originally Posted by woodyhead View Post
I'm new to shell scripting and I want to be able to print the lines in between the head and tail of a .txt file.

for example, if I wrote on the command line: ./scriptname filename 4 10
would print lines 5 6 7 8 9 10?


cheers!
try taking head first.... nd then tail..

for example the inputs given by you were 4 and 10, so you can give

Quote:
head 10
tail (10-4)
i don't know for the syntax errors but a kind of sudo code would look like

Quote:
a=[arguement 2]
b=[arguement 2] - [arguement 1]
cat [filename] | head a | tail b
you can google for proper syntax.....
 
Old 04-14-2013, 08:11 AM   #6
woodyhead
LQ Newbie
 
Registered: Apr 2013
Posts: 6

Original Poster
Rep: Reputation: Disabled
Thats a great help thank you vineethsp !
However as I am a newbie Im still stuck with the proper syntax for it!
I know to pass the parameters are $0 $1 $2 ... etc.(http://www.dreamsyssoft.com/unix-she...s-tutorial.php)

Can't think how a and b would work though? agh

Thanks
 
Old 04-14-2013, 08:25 AM   #7
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
Code:
#!/bin/bash

FILE="$1"
START="$2"
END="$3"

sed -n "${START},${END}p" ${FILE}

exit 0
 
2 members found this post helpful.
Old 04-14-2013, 08:29 AM   #8
jpollard
Senior Member
 
Registered: Dec 2012
Location: Washington DC area
Distribution: Fedora, CentOS, Slackware
Posts: 4,912

Rep: Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513
How about something simple...
Code:
count= $(wc -l file)
tail $((start - count)) | head $((count - start - end))
But I like the sed approach better.

Last edited by jpollard; 04-14-2013 at 08:30 AM.
 
Old 04-14-2013, 08:51 AM   #9
vineethsp
LQ Newbie
 
Registered: Mar 2013
Location: India
Distribution: RedHat Enterprise Linux Server Edition 5.5
Posts: 13

Rep: Reputation: Disabled
Quote:
Originally Posted by woodyhead View Post
Thats a great help thank you vineethsp !
However as I am a newbie Im still stuck with the proper syntax for it!
I know to pass the parameters are $0 $1 $2 ... etc.(http://www.dreamsyssoft.com/unix-she...s-tutorial.php)

Can't think how a and b would work though? agh

Thanks
you can use the values in variable as

Quote:
file=$1
a=$3
b=`expr $3 - $2`
cat $file| head $a | tail $b
Hope this helps you \m/
 
Old 04-14-2013, 09:46 AM   #10
woodyhead
LQ Newbie
 
Registered: Apr 2013
Posts: 6

Original Poster
Rep: Reputation: Disabled
I'm getting there slowly haha!

I am getting a error message when I run the script

"expr: non-numeric argument" and I used 2 and 3 to run it, it said "2: no such file or dierectory"

thanks
 
Old 04-14-2013, 11:05 AM   #11
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
If you're getting errors, show us the exact code you ran that produced it. We can't tell you what you're doing wrong if we can't see what you did.

And please use ***[code][/code]*** tags around your code and data, to preserve the original formatting and to improve readability. Do not use quote tags, bolding, colors, "start/end" lines, or other creative techniques.


In any case, however, don't use expr. Forget that it even exists. It's completely superfluous in all modern shells, which have features built-in that can do everything that old fossil can do and more.

http://mywiki.wooledge.org/BashGuide..._Ever_Do_These

Use bash's arithmetic evaluation, string manipulation, and testing features instead.
 
Old 04-14-2013, 11:12 AM   #12
woodyhead
LQ Newbie
 
Registered: Apr 2013
Posts: 6

Original Poster
Rep: Reputation: Disabled
sorry about the formatting, I hope I've done it right now...

***
Code:
#!/bin/bash
#script name

filename=$0
a=$2
b=`$2 - $1`
cat $filename | head $a | tail $b
***

cheers!
 
Old 04-14-2013, 11:25 AM   #13
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
But how are you running the script? What do the values $0, $1, and $2 hold?

I don't think you realize that $0 stores the name of the script, according to how it was launched. It's very rare that you should actually need to use that parameter. $1 holds the first argument to the script.

PS:

1) Useless Use Of Cat.

2) The syntax of head and tail use "-n" to specify their line numbers.

3) QUOTE ALL OF YOUR VARIABLE EXPANSIONS. You should never leave the quotes off a parameter expansion unless you explicitly want the resulting string to be word-split by the shell and any possible globbing patterns expanded. This is a vitally important concept in scripting, so train yourself to do it correctly now. You can learn about the exceptions when you need them.

http://mywiki.wooledge.org/Arguments
http://mywiki.wooledge.org/WordSplitting
http://mywiki.wooledge.org/Quotes

Code:
#!/bin/bash

filename=$1
a=$3
b=$(( $3 - $2 ))

head -n "$a" "$filename" | tail -n "$b"

exit 0

Last edited by David the H.; 04-14-2013 at 11:31 AM. Reason: moar
 
1 members found this post helpful.
Old 04-14-2013, 11:29 AM   #14
woodyhead
LQ Newbie
 
Registered: Apr 2013
Posts: 6

Original Poster
Rep: Reputation: Disabled
When I run the script in command line I want to be able to run it by:

Code:
./scriptname.sh filename 3(head) 5(tail)
so it prints the lines in between the two numbers?

thanks!
 
Old 04-14-2013, 12:14 PM   #15
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
Yeah? Well, as explained, $1 will be the filename, $2 the "head" number, and $3 the "tail" number. Now it's up to you to figure out how to use them properly.

By the way, I didn't actually test the code above before I posted it. I don't think it gives you exactly what you want. You'll have to modify the math and code flow to get it to output the proper lines. Or use different a different tool, as has also been shown to you.

In any case we've given you all the resources you need to do what you want, so get on it! Post back what you come up with.

Last edited by David the H.; 04-14-2013 at 12:16 PM. Reason: fixes
 
1 members found this post helpful.
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Shell script for run an shell script on server using ssh bloodstreetboy Linux - Server 5 01-12-2013 03:23 AM
How to pass command line arguments from one shell script to another shell script VijayaRaghavanLakshman Linux - Newbie 5 01-20-2012 09:12 PM
Executing a Shell script with 654 permissions inside another shell script. changusee2k Linux - Newbie 2 06-07-2011 07:58 PM
shell script problem, want to use shell script auto update IP~! singying304 Programming 4 11-29-2005 05:32 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 01:09 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration