LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   awk code help for displaying file contents (https://www.linuxquestions.org/questions/programming-9/awk-code-help-for-displaying-file-contents-775123/)

ashok.g 12-12-2009 03:58 AM

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,

pixellany 12-12-2009 04:17 AM

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.)

ashok.g 12-12-2009 04:36 AM

Quote:

Originally Posted by pixellany (Post 3788647)
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?

pixellany 12-12-2009 04:53 AM

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".

ghostdog74 12-12-2009 04:59 AM

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


ashok.g 12-12-2009 05:01 AM

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

pixellany 12-12-2009 05:16 AM

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.

ashok.g 12-12-2009 05:20 AM

I am not as good as you in shell scripting can you explain me some lines in your code?
Quote:

Originally Posted by ghostdog74 (Post 3788663)
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 (Post 3788663)
read -p "Enter starting line: " STARTLINE
read -p "Enter total lines to print: " TOTALPRINT

What does these lines signify?
Quote:

Originally Posted by ghostdog74 (Post 3788663)
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?

pixellany 12-12-2009 05:25 AM

Read the man page for the "read" command. For example, the -p option puts the prompt on the same line.

catkin 12-12-2009 05:47 AM

Quote:

Originally Posted by ashok.g (Post 3788675)
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.

ashok.g 12-12-2009 05:48 AM

Quote:

Originally Posted by ghostdog74 (Post 3788663)
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?

pixellany 12-12-2009 05:55 AM

Quote:

Originally Posted by ashok.g (Post 3788700)
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/

ghostdog74 12-12-2009 06:00 AM

Quote:

Originally Posted by ashok.g (Post 3788700)
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

ashok.g 12-14-2009 12:55 AM

Thanks one and all for your replies.... :)


All times are GMT -5. The time now is 10:56 AM.