Need help in inserting a value.........


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Need help in inserting a value.........
# 1  
Old 08-05-2008
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.
# 2  
Old 08-05-2008
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  
Old 08-05-2008
Question

Quote:
Originally Posted by cfajohnson
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  
Old 08-05-2008
Do you mean like this?

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

If not... your requirement still isn't clear.
# 5  
Old 08-05-2008
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  
Old 08-05-2008
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  
Old 08-05-2008
Quote:
Originally Posted by santyshyam
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".

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Inserting new line

Hi all, I want ot print a header with new line in the end. I am using echo like echo "this is header" I want output like This is header $ I tried -e also but it's not working. echo - e Can anyone help please? (6 Replies)
Discussion started by: dsa
6 Replies

2. UNIX for Dummies Questions & Answers

Inserting the last field first

I have a list of more than 1000 files on the following format. roman_pottery_in_the_archaeological_record_2007.pdf power_politics_and_religion_in_timurid_iran_2007.pdf toward_a_theory_of_human_rights_religion_law_courts_2006.pdf i_was_wrong_the_meanings_of_apologies_2008.pdf I want to... (2 Replies)
Discussion started by: kristinu
2 Replies

3. Shell Programming and Scripting

Inserting Delimiters

Hi Team, I am trying to get the data in below format Jan 01 | 19:00:32 | xyz | abc | sometext | string however I am not sure of the total number strings which can come in the record hence i cant use something like below as it can end $6 or it can go further cat file| awk... (8 Replies)
Discussion started by: rakesh_411
8 Replies

4. Shell Programming and Scripting

Records Inserting

Hi Guys , I Need to insert records into a file just above the last row . like i have a file which has records as shown below : 00012919 7836049 S 00012920 7836049 S 00012921 3828157 Y 00012922 3828157 Y 00012923 3828157 S T005290070331000012923 i want to... (1 Reply)
Discussion started by: robert89
1 Replies

5. Shell Programming and Scripting

Help with inserting a line

Hello, I am new to this forum. I have a question in Unix shell scripting. Here is my requirement I have 1000 files and I need to insert a header with one line at the top of each of the 1000 files. Please let me know if you have any solution for this Thanks in advance. Raj (4 Replies)
Discussion started by: trajashekar
4 Replies

6. Shell Programming and Scripting

Inserting a trailer

I am new to unix... cn u plz tel me how to add a trailer as a last record of each file. (3 Replies)
Discussion started by: amitpta
3 Replies

7. Shell Programming and Scripting

inserting lines

Hi I would like to add lines to a file at specific locations. eg: If I have a file containing data... ABC DEF GHIJKLKMNOP RSTUVWXYZ and I have a requirement to insert a lines a) "LINE_FIRST" before first line in the file b) "LINE_X" before third line in the file c) "LINE_LAST"... (2 Replies)
Discussion started by: tostay2003
2 Replies

8. Shell Programming and Scripting

Inserting a new line

I have a file where each line starts with a "update" statement. I want to insert a new line, after each line, with the words "go" in the new line. Can someone help me out with this? (1 Reply)
Discussion started by: ssmallya
1 Replies

9. UNIX for Dummies Questions & Answers

inserting line??

guys can anyone tel me how to insert a line in a file(bash) using line number i wanna insert the line using LINE NUMBER...i know for inserting above or below a perticular line we can use sed a\ or i\.... thanks in advance.... (7 Replies)
Discussion started by: anj
7 Replies

10. Shell Programming and Scripting

Inserting a space

I am trying to reformat some UK postal codes. I have a csv where field 12 is the postal code. The postal codes length has a maximum of 7. Basically, I would like a bit of code to look at field 12 and if the postal code has a length of 7, then insert a space into the field fourth from the right,... (3 Replies)
Discussion started by: dbrundrett
3 Replies
Login or Register to Ask a Question