Visit Our UNIX and Linux User Community


append a line into a file in the top


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting append a line into a file in the top
# 1  
Old 10-15-2009
append a line into a file in the top

hi,
My code is
Code:
#!/bin/sh

echo "\n\nPlease enter the month of the year(YYYYMM) : \c"
read date_rep

INPUT_L9_FILE=L9_Recharge_Description_EOM_$date_rep.csv

#This part is used to summarise Grand_Total, Balance_Total of file L9_Recharge_Description_EOM_${1}.csv.

awk -F","  '{if(NR!=1) { Grand_Total[$1] += $2;Balance_Total[$1] += $3; }}
END { for (s in Grand_Total) {printf " %s,%f,%f \n",s,Grand_Total[s], Balance_Total[s] } }'  $INPUT_L9_FILE  >>   L9_Recharge_Description_summary_EOM_$date_rep.o

cat L9_Recharge_Description_summary_EOM_$date_rep.o|sort > L9_Recharge_Description_summary_EOM_$date_rep.csv

#This creating a directory by name and moving the outputs to the respective directory
mkdir summary_$date_rep
mv *_summary_EOM_$date_rep.csv summary_$date_rep

#This removes all the intermediate file with extension .o
rm -f *.o

Into my Input file :L9_Recharge_Description_EOM_200909.csv
Code:
L9_RECHARGE_DESCRIPTION,Grand_Total,Balance_Total
Breakage,0,27491.4

POS Recharge,10659.84,1516.93

PP Refund,81.01,91.29

Positive Adjustment,2052.24,554.26

Positive Adjustment Reversal,117.28,191.14

Recharge,129201.57,23396.38

Recharge Reversal,2821.36,2677.78

Transfer Increase,5402.11,951.89

Not give the complete file


Into my output file :L9_Recharge_Description_summary_EOM_$date_rep.csv
Code:
,680135.500000,14607180.900000
 Breakage,0.000000,151024.010000
 POS Recharge,59185.400000,7927.250000
 PP Refund,593.320000,625.200000
 Positive Adjustment Reversal,1166.560000,1466.920000
 Positive Adjustment,20456.650000,8413.810000
 Recharge Reversal,15924.230000,18501.520000

Now what change into the code should i make to put a header into the output file like this

Into my output file :L9_Recharge_Description_summary_EOM_$date_rep.csv
Code:
L9_RECHARGE_DESCRIPTION, Total of Grand total, Total fo balance total

,680135.500000,14607180.900000
 Breakage,0.000000,151024.010000
 POS Recharge,59185.400000,7927.250000
 PP Refund,593.320000,625.200000
 Positive Adjustment Reversal,1166.560000,1466.920000
 Positive Adjustment,20456.650000,8413.810000
 Recharge Reversal,15924.230000,18501.520000


Thanks .....

Last edited by pludi; 10-15-2009 at 07:30 AM.. Reason: code tags please...
# 2  
Old 10-15-2009
Code:
#!/bin/sh

echo "\n\nPlease enter the month of the year(YYYYMM) : \c"
read date_rep

INPUT_L9_FILE=L9_Recharge_Description_EOM_$date_rep.csv
echo "L9_RECHARGE_DESCRIPTION, Total of Grand total, Total fo balance total" >> $INPUT_L9_FILE

 ### Remaining code

# 3  
Old 10-15-2009
Code:
,680135.500000,14607180.900000
 Breakage,0.000000,151024.010000
 L9_RECHARGE_DESCRIPTION,0.000000,0.000000
 POS Recharge,59185.400000,7927.250000
 PP Refund,593.320000,625.200000
 Positive Adjustment Reversal,1166.560000,1466.920000
 Positive Adjustment,20456.650000,8413.810000
 Recharge Reversal,15924.230000,18501.520000
 Recharge,726584.070000,133711.370000
 Transfer Increase,30390.900000,23733.490000
 Transfer Reduce,112904.550000,137071.640000

this is the output of this change

sorting is taking place into this so ?????

---------- Post updated at 03:41 PM ---------- Previous update was at 03:39 PM ----------

Not into the input file into the output file L9_Recharge_Description_summary_EOM_$date_rep.csv
the change is required

Last edited by pludi; 10-15-2009 at 07:30 AM.. Reason: code tags please...
# 4  
Old 10-15-2009
Becuase, you are sorting in your script.

Code:
cat L9_Recharge_Description_summary_EOM_$date_rep.o|sort > L9_Recharge_Description_summary_EOM_$date_rep.csv

# 5  
Old 10-15-2009
Any other work around or change in code so i can manage the above . Only into the final output file i need the header .
# 6  
Old 10-15-2009
Code:
#! /bin/sh

echo "\n\nPlease enter the month of the year(YYYYMM) : \c"
read date_rep

INPUT_L9_FILE=L9_Recharge_Description_EOM_$date_rep.csv

awk -F","  '{if(NR!=1) { Grand_Total[$1] += $2;Balance_Total[$1] += $3; }}
END { for (s in Grand_Total) {printf " %s,%f,%f \n",s,Grand_Total[s], Balance_Total[s] } }'  $INPUT_L9_FILE  >>   L9_Recharge_Description_summary_EOM_$date_rep.o

cat L9_Recharge_Description_summary_EOM_$date_rep.o|sort > L9_Recharge_Description_summary_EOM_$date_rep_1.csv
echo echo "L9_RECHARGE_DESCRIPTION, Total of Grand total, Total fo balance total" >> L9_Recharge_Description_summary_EOM_$date_rep.csv
cat L9_Recharge_Description_summary_EOM_$date_rep_1.csv >> L9_Recharge_Description_summary_EOM_$date_rep.csv
#This creating a directory by name and moving the outputs to the respective directory
mkdir summary_$date_rep
mv *_summary_EOM_$date_rep.csv summary_$date_rep

#This removes all the intermediate file with extension .o
rm -f *.o
rm -f L9_Recharge_Description_summary_EOM_$date_rep_1.csv || true

Use this one. It should work.

Previous Thread | Next Thread
Test Your Knowledge in Computers #57
Difficulty: Easy
In a Database Management System (DBMS), the content and the location of the data is defined by the meta data.
True or False?

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Move a line to top of the file

Hi, I have a following file and it has only one occurrence of line that says "Output view:". It could be in middle somewhere ( i don't know the exact location ). I want to move it as the first line of the file. Input AAA BBBB CCCC Output view: XXXX YYYY ZZZZ Output should be: Output... (13 Replies)
Discussion started by: jakSun8
13 Replies

2. Shell Programming and Scripting

Append file name to the beginning of each line

I want to append file names at the beginning of a line for each row file content abc.txt.gz 123|654|987 bcd.txt.gz 876|trf|kjh I want a single output file with below format abc.txt.gz|123|654|987 bcd.txt.gz|876|trf|kjh This one is working but only with unzip files,need to have... (3 Replies)
Discussion started by: rakesh5300
3 Replies

3. Shell Programming and Scripting

conditional append one line in file.

Hi, Unix gurus, I have a requirement as following: checking existing file, if the file only contain one line. then append "No data" else keep existing file as is. can i achieve this by in command line without write a script. :wall: Thanks in advance. (4 Replies)
Discussion started by: ken002
4 Replies

4. Shell Programming and Scripting

How to append text to the second line of a file

Say I have a text file like: 1 3 4 How would I use ksh to put the number '2' into the second line of that file? I'm using OpenBSD so the sed syntax might be a bit different (I have no idea how to use sed, though) (4 Replies)
Discussion started by: guitarscn
4 Replies

5. UNIX for Dummies Questions & Answers

add a new line on top of a file

infile a b c A E F 1 2 3 outfile new line a b c A E F 1 2 3 I tried: sed '1i\ new line' infile > outfilecat outfile new linea b c A E F 1 2 3 I don't want the new line be added to the existing first line. Thanks Joseph (7 Replies)
Discussion started by: jdhahbi
7 Replies

6. Shell Programming and Scripting

shell script to read a line in gps receiver log file and append that line to new file

Hi, I have gps receiver log..its giving readings .like below Trying 127.0.0.1... Connected to localhost. Escape character is '^]'. GPSD,R=1 $GPGSV,3,1,11,08,16,328,40,11,36,127,00,28,33,283,39,20,11,165,00*71... (3 Replies)
Discussion started by: gudivada213
3 Replies

7. Shell Programming and Scripting

Append each line to next previous line in a file

Hi all, Please help me in providing sample code to append the following 4 lines in one row. Input : A1/EXT "BAPBSC10/07B/00" 523 090530 0115 RXOCF-430 HY1711 1 EXTERNAL ALARM DOOR ALARM Output should be : A1/EXT "BAPBSC10/07B/00" 523 090530 0115 ... (8 Replies)
Discussion started by: sudhakaryadav
8 Replies

8. Shell Programming and Scripting

best way to insert a line at the top of a file?

say I want to insert "this is a test" as the first line into file A, besides echo "this is a test" > /tmp/tmpfile cat /tmp/tmpfile fileA >> /tmp/result, is there any simple way I can do it? thanks (7 Replies)
Discussion started by: fedora
7 Replies

9. AIX

want to remove some line from top of file.

Hi All, I have AIX 5.3 server. I have one big file. in that i want to remove 5000 line from top. is there any command for this? Thanks, Vishal (6 Replies)
Discussion started by: vishalpatel03
6 Replies

10. UNIX for Dummies Questions & Answers

How to Append a Value to each line of the file

Hi, We have Multiple source files which has some data, I need a K shell script which will append number '1' as the last character to all the lines of the first file and then increments the value and appends '2' to all the lines to the next file and so on. For Example Incoming file 1 ... (11 Replies)
Discussion started by: dsshishya
11 Replies

Featured Tech Videos