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


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Need help in inserting a value.........
# 8  
Old 08-05-2008
Bug

Quote:
Originally Posted by summer_cherry
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

Hi Summer Cherry,

It worked fine, even though I made some minor formatting Change...............

Thanks a lot......................


Regards,
Santy
# 9  
Old 08-05-2008
Quote:
Originally Posted by summer_cherry
hope below one can help, it suppose all your files are named with .txt suffix.

Code:
for i in `ls *.txt`


Not only is ls unnecessary, but it will break the script if there is whitespace in any of the filenames. It should be:

Code:
for i in *.txt

Quote:
Code:
do
	val=`cat $i | tail -1 | awk '{print $1}'`


The OP was not clear whether the line in question was the last of the file; that only works if it is.

Also, there is no need for cat or tail:
Code:
val=`awk 'END {print $1}' "$i"`

Quote:
Code:
	nawk -v val="$val" '{
		if(index($0,"STOP")!=0)
			print $0"  "val
		else
			print
	}' $i
done

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