[SOLVED] awk code help for displaying file contents
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Hi folks,
I am trying to display the contents of a file from given line no. to the total no. of lines. I tried upto some code but its not giving the output I want. Please help me.
Code:
#display the file contents from given line number to no of lines given
echo enter the file name
read a
if [ ! -e $a ]
then
echo "file doesnt exist"
elif [ -d $a ]
then
echo "file is a directory hence cannot diaplay"
else
echo "Enter the starting line number of file"
read s
echo "Enter the total no.of lines to print"
read t
file=`cat $a`
echo -e "$file" | awk '{while ( NR <= $s && NR >= $t )print $0}'>temp
file=`cat $temp`
echo -e "$file"
read
fi
You need to tell us where the problem is---what does the script do now? What are the error messages, etc.?
Does the awk statement work by itself in a terminal (without running the script)?
Printing a range of line numbers is (for me) easier with SED. Example:
Code:
sed -n '5,+10p' filename ##prints lines starting with #5, plus the next 10 lines
Finally, what is this supposed to be doing?
Code:
file=`cat $a`
You have a filename in the variable "a". That statement will put the entire contents of that file into the variable "file". (Not what you want.)
1. While I am running this script, I am not getting anything after it takes some information like total no. of line. Also, I am not exiting to the prompt.
2. I think the problem is all with awk.
3. I am getting the output with sed as you said. But I want to insert arguments as user defined like $s and $t. How can I do that?
If you want to run the SED command using the values of the variables "s" and "t", then you want this:
Code:
sed -n "${s},+${t}p" $a
Note the double quotes, and only one "$" for each variable
Quote:
For just storing the contents of file stored in $a again to "file". In this context it doesn't serve any purpose.
You missed my point....You are using the variable "file" in your AWK statement. The value of "file" will be the entire contents of the filename stored in the variable "a". I'm quite sure that's not what you want.
Third request: Run the AWK command by itself to make sure that it is working as you expect.
What does "shopt -s extglob" do? Can you explain it with their meanings?
It sets a shell option (shopt) that enables "extended globbing". Globbing is (obscure) shell-speak for filename pattern matching which has evolved to other pattern matching, not only filename.
I am interested that " NR>=s && NR<=e " is used with out any if statements?
Can you explain me?
And **I** am interested in knowing if you are reading the man pages and other documentation. if "man awk" does not answer this, then try this excellent set of tutorials: http://www.grymoire.com/Unix/
I am interested that " NR>=s && NR<=e " is used with out any if statements?
Can you explain me?
i find it hard to explain if you have not read the manual. I hope someone with better english standard than me can explain to you. Meanwhile, please use this instead
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.