appending text to a file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting appending text to a file
# 1  
Old 02-01-2012
Question appending text to a file

I am trying to count number of record in a file and then append a trailer to that file.

Trailer should e in the format as below
T| <count of records>

count of records ---> this count should be one less than the actual count obtained, as the file will have a header.

I have drafted a script as below

count = wc - l $fn ---> $fn will provide the file name
count = $count - 1 ---> actual count - 1

I don't know how to create the trailer with the count. Please help me out.

Thanks in advance.
# 2  
Old 02-01-2012
Code:
count=`wc -l $fn | awk '{print $1-1}'`
echo "T|$count" >> $fn

Guru.

Last edited by guruprasadpr; 02-01-2012 at 02:37 AM..
This User Gave Thanks to guruprasadpr For This Post:
# 3  
Old 02-01-2012
Hi,
you can append to the file in the following format

Code:
 echo "T|$count " >>$fn.txt

incase if it needs to printed in new line

Code:
echo -e "\n T|$count " >> $fn.txt

thanks,
venkat
This User Gave Thanks to venkatareddy For This Post:
# 4  
Old 02-01-2012
Lets say, you have some set of files ending with .dat and u want to create the Trailer like T|$(count-1)

So,

Code:
for fn in *.dat
do
awk 'END{print "T| " NR-1}' ${fn} >> ${fn}
done

This User Gave Thanks to knight_eon For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Problem in appending text to a file located in remote server

ssh -q "server_name sudo echo 'dbagroup::1234' >> sudo /etc/group"if i execute the above code its not getting appended. my requirement is to login to remote server and append dbagroup::1234 in /etc/group i am able to achieve this with tee -a command want to know why its not working with >>... (5 Replies)
Discussion started by: chidori
5 Replies

2. UNIX for Dummies Questions & Answers

Appending a column of numbers in ascending order to a text file

I have a text file where I want to append a column of numbers in ascending orders. Input: 57 abc 25 def 32 ghi 54 jkl Output:57 abc 57 abc 1 25 def 2 32 ghi 3 54 jkl 4 How do I go about doing that? Thanks! (11 Replies)
Discussion started by: evelibertine
11 Replies

3. UNIX for Dummies Questions & Answers

Appending two text files

Hi, i am using unix server and bash shell.. i have two csv files... i have file 1 as below... arun bvb ssx ccc and file 2 as below manas friu dfgg cat (3 Replies)
Discussion started by: arunmanas
3 Replies

4. UNIX for Dummies Questions & Answers

appending in a single text file

actually, i have to grep the value of cpu usage from six differrent servers and it has to generated in a single report( in a single text or excel file). i have used the below command for grepping the value. top -n 1 > arun.txt cat arun.txt | grep 'init' | cut -c 49-53 > output1.csv like... (2 Replies)
Discussion started by: arunmanas
2 Replies

5. Shell Programming and Scripting

Increasing a number and appending it to next line of a text file

Hi all, I have text file having a number P100. what i need is when i run a script, it should add 1 to the above number and append it to the next line of a same text file.. when i use the script next time it should check the last line and add 1 to the last number and so on.. like the text... (5 Replies)
Discussion started by: smarty86
5 Replies

6. Shell Programming and Scripting

Appending text to a file

Hi, Want to append the text to a new file, echo "set `sqlplus -S abc/xyz123@localdb<<EOS" >> chk_test_append.sh echo "EOS`" >> chk_test_append.shbut getting the below error : what wrong is written ? With Regards (4 Replies)
Discussion started by: milink
4 Replies

7. UNIX for Dummies Questions & Answers

appending text on top of another file

Dear All, I have two files One is script file in which I am writing commands to append a text in a normal file. I want to insert the text on top of the file. I dont want to use sed and awk commands nor temp file. is it possible? (3 Replies)
Discussion started by: anjali
3 Replies

8. Shell Programming and Scripting

Appending text of one file into other

hello, I am trying to append\insert the text that exists in file A into file B. for instance: File A contains: abcdef File B contains: ghijklm can i insert into file A the content in file B without damaging\deleting the input into file A? is there a command in shell that enables such... (1 Reply)
Discussion started by: danland
1 Replies

9. Shell Programming and Scripting

appending string to text file based on search string

Hi, I need to append string "Hi" to the beginning of the lines containing some specific string. How can I achieve that? Please help. Malay (1 Reply)
Discussion started by: malaymaru
1 Replies
Login or Register to Ask a Question