LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 12-12-2009, 03:58 AM   #1
ashok.g
Member
 
Registered: Dec 2009
Location: Hyderabad,India
Distribution: RHEl AS 4
Posts: 215

Rep: Reputation: 32
awk code help for displaying file contents


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
Thank you,

Last edited by ashok.g; 12-12-2009 at 04:01 AM.
 
Old 12-12-2009, 04:17 AM   #2
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809

Rep: Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743
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.)
 
Old 12-12-2009, 04:36 AM   #3
ashok.g
Member
 
Registered: Dec 2009
Location: Hyderabad,India
Distribution: RHEl AS 4
Posts: 215

Original Poster
Rep: Reputation: 32
Quote:
Originally Posted by pixellany View Post
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?

5. Am I not do this with the help of awk?
 
Old 12-12-2009, 04:53 AM   #4
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809

Rep: Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743
Again, run the awk statement by itself in a terminal. First, run it with fixed values instead of variables.

You did not address this question:
Quote:
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.)
To put variables in the SED statement:

Code:
sed -n "${start},+${num}p" filename  ##starts on the value of "start" and prints more lines per the value of "num".
 
Old 12-12-2009, 04:59 AM   #5
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
if you want to use bash
Code:
checkdigit(){
    shopt -s extglob
    case "$1" in
        *[^0-9]*)
        return 1
        ;;
        * ) return 0
    esac
}

while true
do
    read -p "Enter the file name: " FILENAME
    [ ! -e "$FILENAME" ]  || [ -d "$FILENAME" ]  && echo "invalid filename" && continue
    read -p "Enter starting line: " STARTLINE
    read -p "Enter total lines to print: " TOTALPRINT
    checkdigit $STARTLINE
    r=$?
    checkdigit $TOTALPRINT
    t=$?
    case "$r $t" in
        "0 0" )
        c=0
        flag=1
        END=$(( TOTALPRINT + STARTLINE ))
        while read -r line
        do
            c=$((c+1))
            case "$c" in
                "$STARTLINE") flag=1;;
                "$END")  flag=0 ;;
            esac
            [ "$flag" -eq 1 ] && echo "$line"
        done < "$FILENAME"
        break
        ;;
        *) echo "Invalid numbers";;
    esac
done

if you want awk
Code:
read -p "Enter the file name: " FILENAME
read -p "Enter starting line: " STARTLINE
read -p "Enter total lines to print: " TOTALPRINT
awk -v s="$STARTLINE" -v e="$((TOTALPRINT+STARTLINE))" '
NR>=s && NR<=e' $FILENAME

Last edited by ghostdog74; 12-12-2009 at 05:03 AM.
 
Old 12-12-2009, 05:01 AM   #6
ashok.g
Member
 
Registered: Dec 2009
Location: Hyderabad,India
Distribution: RHEl AS 4
Posts: 215

Original Poster
Rep: Reputation: 32
Quote:
Finally, what is this supposed to be doing?
Code:

file=`cat $a`
For just storing the contents of file stored in $a again to "file". In this context it doesn't serve any purpose.

Quote:
Code:
sed -n "${start},+${num}p" filename  ##starts on the value of "start" and prints more lines per the value of "num".
I used this as:
Code:
sed -n '${$s},+${$t}p' $a
But, I am getting the error as:
Code:
sed: -e expression #1, char 13: unterminated `s' command
 
Old 12-12-2009, 05:16 AM   #7
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809

Rep: Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743
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.
 
Old 12-12-2009, 05:20 AM   #8
ashok.g
Member
 
Registered: Dec 2009
Location: Hyderabad,India
Distribution: RHEl AS 4
Posts: 215

Original Poster
Rep: Reputation: 32
I am not as good as you in shell scripting can you explain me some lines in your code?
Quote:
Originally Posted by ghostdog74 View Post
checkdigit(){
shopt -s extglob
case "$1" in
*[^0-9]*)
return 1
;;
* ) return 0
esac
}
What does "shopt -s extglob" do? Can you explain it with their meanings?
Quote:
Originally Posted by ghostdog74 View Post
read -p "Enter starting line: " STARTLINE
read -p "Enter total lines to print: " TOTALPRINT
What does these lines signify?
Quote:
Originally Posted by ghostdog74 View Post
while read -r line
do
c=$((c+1))
case "$c" in
"$STARTLINE") flag=1;;
"$END") flag=0 ;;
esac
[ "$flag" -eq 1 ] && echo "$line"
done < "$FILENAME"
break
;;
*) echo "Invalid numbers";;
esac
done
What does "read -r line" do?
 
Old 12-12-2009, 05:25 AM   #9
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809

Rep: Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743
Read the man page for the "read" command. For example, the -p option puts the prompt on the same line.
 
Old 12-12-2009, 05:47 AM   #10
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
Quote:
Originally Posted by ashok.g View Post
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.

For more information, browse to http://www.gnu.org/software/bash/manual/bashref.html and search for extglob.
 
Old 12-12-2009, 05:48 AM   #11
ashok.g
Member
 
Registered: Dec 2009
Location: Hyderabad,India
Distribution: RHEl AS 4
Posts: 215

Original Poster
Rep: Reputation: 32
Quote:
Originally Posted by ghostdog74 View Post
Code:
awk -v s="$STARTLINE" -v e="$((TOTALPRINT+STARTLINE))" 'NR>=s && NR<=e' $FILENAME
I am interested that " NR>=s && NR<=e " is used with out any if statements?
Can you explain me?
 
Old 12-12-2009, 05:55 AM   #12
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809

Rep: Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743
Quote:
Originally Posted by ashok.g View Post
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/
 
Old 12-12-2009, 06:00 AM   #13
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
Quote:
Originally Posted by ashok.g View Post
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
Code:
...  'NR>=s && NR<=e{ print $0 }' ...
until you get better at awk
 
Old 12-14-2009, 12:55 AM   #14
ashok.g
Member
 
Registered: Dec 2009
Location: Hyderabad,India
Distribution: RHEl AS 4
Posts: 215

Original Poster
Rep: Reputation: 32
Thanks one and all for your replies....
 
  


Reply

Tags
awk, contents, display



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
Run AWK script on contents of a variable EmrldDrgn Linux - Newbie 3 07-10-2009 05:05 AM
my cd contents file names not displaying morethan 8 digits. AMajeed AIX 5 08-08-2007 09:56 AM
browser displaying localhost pages in code howarddevore Ubuntu 4 07-31-2005 08:14 AM
Retrieving variables from contents of files using awk or any other bash tool genderbender Programming 8 01-21-2005 03:27 AM
win_c, floppy, cdrom are not displaying contents clockworkorange Linux - General 13 12-28-2001 10:40 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 07:40 AM.

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