Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6761443
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T14:11:30+00:00 2026-05-26T14:11:30+00:00

Although it is looking similar to my previous post but here purpose is different.

  • 0

Although it is looking similar to my previous post but here purpose is different.

udit@udit-Dabba ~/ah $ cat decrypt.txt
60 00 00 00 00 17 3a 20  00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 01  00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 02  *00 00 e0 f9 6a 61 61 6e
65 6b 61 68 61 6e 67 61  79 65 77 6f 64 69 6e* 00
00 00 03 29 

I want to extract last string of the file (here it is 29) in a shell varaible

I tried this …

size=`wc -w encrypt.txt`

awk -v size=$size 'BEGIN {RS=" ";ORS=" ";}' {if (NR>size-1 &&
                                         NR < size+1)print $0}' decrypt.txt

Output :
29

But when I changed the file slightly ..

  udit@udit-Dabba ~/ah $ cat decrypt.txt
  60 00 00 00 00 17 3a 20  00 00 00 00 00 00 00 00
  00 00 00 00 00 00 00 01  00 00 00 00 00 00 00 00
  00 00 00 00 00 00 00 02  *00 00 e0 f9 6a 61 61 6e
  65 6b 61 68 61 6e 67 61  79 65 77 6f 64 69 6e* 00
  65 6b 61 68 61 6e 67 61  00 00 03 29 

Output :
03

Why there is discrepency between the results ??

I am new to awk and shell features so I am not sure whether it is a right way to do so or not ???

I think there should be some variation of grep,sed,awk or any other linux command which may solve my problem but I am not aware of it.

Please guide me for this.

Thanx in advance.

Purpose :

Make two variables in a shell script which should store last and second last strings of an input file.

Limitation :

Every input file contains a blank line at the end of file.

(Like in above mentioned file , after the file contents there would be one more blank line just like hitting ENTER key and that can not be changed because it is being generated through a C program at run time.)

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T14:11:31+00:00Added an answer on May 26, 2026 at 2:11 pm

    To get the last field:

    awk '{ if (NF > 0) { last = $NF } } END { print last }' "$@"
    

    The second last field is trickier for the case where there is just one field on the last line (so you need the last field from the line before).

    awk '{ if (NF > 0)
           {
               if (NF == 1) { lastbut1 = last; last = $1; }
               else { lastbut1 = $(NF-1); last = $NF; }
           }
         }
         END { print lastbut1 " " last; }' "$@"
    

    This produces a blank and the last value if the file contains but one value. It produces just a blank if there are no values at all.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I tried looking for posts like this one but although many are similar, none
I'm looking to create a very small cataloguing app for personal use (although I'd
I am looking for resources on Microsoft Sync Framework. Although MSDN and Google Search
I'm looking to integrate a payment gateway into a Ruby on Rails applicaton. Although
I am looking for an efficient eigensolver ( language not important, although I would
I am looking for theory, algorithms and similar for how to compare music. More
I've been looking at MongoDB and I'm fascinated. It appears (although I have to
Note: although seemingly similar, this is not a duplicate of Overriding fillInStackTrace for a
Looking at In jQuery, want to remove all HTML inside of a div ,
I've been searching around trying to find an answer both here and google, although

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.