Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google site



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

Closed Thread
English Japanese Spanish French German Portuguese Italian Powered by Powered by Google
 
Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 08-05-2008
Registered User
 

Join Date: Jun 2008
Posts: 6
Need help in inserting a value.........

Hello,

I'm having a files with variable no of lines. An example for this file is as follows

~xxx
STRT 0.0000000000 :
STOP :
~xxxxx
0.000000 557.109552 -557.109552
1511.482910 -954.373377 954.373377

in the STOP line below the 0.00000000000 I need the first coloumn of the last line ie in this case 1511.482910. In each file the value will be changing.
I'm having more than 500 of such files.
I believe a shell script or awk can handle this. Please help me.
Sponsored Links
  #2 (permalink)  
Old 08-05-2008
cfajohnson's Avatar
Shell programmer, author
 

Join Date: Mar 2007
Location: Toronto, Canada
Posts: 2,538
If the line you want is the last line of the file:


Code:
tail -n1 "$FILE" | cut -f1

If not, how do you determine which line you want?
  #3 (permalink)  
Old 08-05-2008
Registered User
 

Join Date: Jun 2008
Posts: 6
Question

Quote:
Originally Posted by cfajohnson View Post
If the line you want is the last line of the file:


Code:
tail -n1 "$FILE" | cut -f1

If not, how do you determine which line you want?
for finding out the value of the last line's first coloumn I will use
awk '{field = $1 }; END {print field}' filename
but my need is to print this value next to STOP.
  #4 (permalink)  
Old 08-05-2008
 

Join Date: May 2008
Location: Sydney, Australia
Posts: 1,009
Do you mean like this?


Code:
awk '{field = $1 }; END {print "STOP",field}' filename

If not... your requirement still isn't clear.
  #5 (permalink)  
Old 08-05-2008
Registered User
 

Join Date: Jun 2007
Location: Beijing China
Posts: 1,133
hope below one can help, it suppose all your files are named with .txt suffix.


Code:
for i in `ls *.txt`
do
	val=`cat $i | tail -1 | awk '{print $1}'`
	nawk -v val="$val" '{
		if(index($0,"STOP")!=0)
			print $0"  "val
		else
			print
	}' $i
done

  #6 (permalink)  
Old 08-05-2008
Registered User
 

Join Date: Mar 2006
Posts: 145
It would have been easier for willing repliers
if you had provided us with line numbers so that it gets conspicuous what characters still belong to the same line.
This is rather ambiguous
Quote:
in the STOP line below the 0.00000000000 I need the first coloumn of the last line
Depending whatever you consider a valid field separator for this file
if it really is in the same STOP line,
it should be as simple as e.g.

Code:
$ awk -F' : ' '$1~/^STOP/{print$2}' file_to_parse

But I guess this wasn't meant?
If it were on the other hand on a different line following the STOP marker
(can't fathom what ~xxxxx shall denote) then you would have to parse this line.
Sticking with the assumed line count, and assuming GNU grep was available
you could then maybe do something like

Code:
$ grep -A3 ^STOP file_to_parse|tail -1|cut -d\  -f1

But I don't want to fall prey to era's useless use content here,
why this could better be accomplished in a single awk statement
after you have told us how the line counting goes.
  #7 (permalink)  
Old 08-05-2008
cfajohnson's Avatar
Shell programmer, author
 

Join Date: Mar 2007
Location: Toronto, Canada
Posts: 2,538
Quote:
Originally Posted by santyshyam View Post
for finding out the value of the last line's first coloumn I will use
awk '{field = $1 }; END {print field}' filename
but my need is to print this value next to STOP.
You haven't answered the question: How do you determine which line contains the value?

In the example you gave, that line is not "next to STOP".

Sponsored Links
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Inserting a new line ssmallya Shell Programming and Scripting 1 07-18-2008 04:21 AM
inserting line?? anj UNIX for Dummies Questions & Answers 7 11-03-2007 12:18 PM
Inserting 1st arg into middle boris Shell Programming and Scripting 2 04-13-2007 03:41 PM
Inserting a carriage rtn in a sed cmd sirtrancealot Shell Programming and Scripting 6 07-14-2006 06:08 AM
Inserting a space dbrundrett Shell Programming and Scripting 3 02-27-2004 12:44 PM



All times are GMT -4. The time now is 05:22 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2010. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0