Hardcoding & Record counts in a file


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Hardcoding & Record counts in a file
# 1  
Old 08-22-2011
Hardcoding & Record counts in a file

HI ,

I am having a huge comma delimiter file, I have to append the following four lines before the starting of the file through a shell script.

FILE NAME = TEST_LOAD
DATETIME = CURRENT DATE TIME
LOAD DATE = CURRENT DATE
RECORD COUNT = TOTAL RECORDS IN FILE

Code:
Source data

1,2,3,4,5,6,7
8,9,1,2,3,4,5

Code:
Target Data

FILE NAME = TEST_LOAD
DATETIME = 220820110812
LOAD DATE = 22082011
RECORD COUNT = 2
1,2,3,4,5,6,7
8,9,1,2,3,4,5


Using the awk NR I can get the record counts, but I am not sure how to insert the headers and the record count at the starting of the file, as the file is huge i don't want to copy the file and make the changes.

is there any way by which i can add the four lines which out duplicating the file.

Please suggest.

Regards,
Shruthi
# 2  
Old 08-22-2011
No way. You have to make a new copy of the file explicitly or implicitly (perl or GNU sed with -i option).

Last edited by yazu; 08-22-2011 at 10:01 AM..
# 3  
Old 08-22-2011
HI ,

I tried the below code but it's giving the error at the datetime part.

Code:
awk 'BEGIN { print "file_name = test_count" }  print { Datetime = "date +%Y%m%d%H%M%S"} print {date = "date"} print {record_count = NR} { print $0 }'  test.csv > test_gen.csv

can you pls tell where I am doing it wrong.

Regards,
Shruthi
# 4  
Old 08-22-2011
You don't need awk. Do something like this:
Code:
echo 'file_name = test_count' >new
date >>new
wc -l original_file >>new
cat original_file >> new

# 5  
Old 08-22-2011
Quote:
Originally Posted by shruthidwh
I am not sure how to insert the headers and the record count at the starting of the file, as the file is huge i don't want to copy the file and make the changes.

is there any way by which i can add the four lines which out duplicating the file.
Quote:
Originally Posted by yazu
No way. You have to make a new copy of the file explicitly or implicitly (perl or GNU sed with -i option).
That's incorrect. ed can be used to modify a file without creating another file in the filesystem.

The following may give the original poster some ideas:
Code:
filename=data
ed -s "$filename" <<EOED
1i
FILENAME = $filename
DATETIME = $(date +%Y%m%d%H%M%S)
RECORD_COUNT = $(awk 'END {print NR}' "$filename")
.
w
q
EOED

Caveat: It's never been a problem for me, but keep in mind that ed uses a single dot on a line by itself to delimit the data being inserted. Should the inserted data itself contain a dot by itself on a line, it would need to be protected with an unambiguous decoration. After the insertion, the substitute command can be used to undecorate.
# 6  
Old 08-22-2011
Yes. You can use ed or any editor or write a very simple program to read a file in memory, delete a file and write the new one. But "the file is huge" and ed should make some copy or use a virtual memory or something like. I don't know may be it's possible to play somehow with inodes to add bytes to the start of file but I didn't hear about it.
# 7  
Old 08-22-2011
yazu is correct, there's no trivial way to add bytes to the front of a file. Any method would involve the same thing, generating a new file with the right bytes in the front.
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Remove duplicated records and update last line record counts

Hi Gurus, I need to remove duplicate line in file and update TRAILER (last line) record count. the file is comma delimited, field 2 is key to identify duplicated record. I can use below command to remove duplicated. but don't know how to replace last line 2nd field to new count. awk -F","... (11 Replies)
Discussion started by: green_k
11 Replies

2. Shell Programming and Scripting

CSV File:Filter duplicate records from column1 & another column having unique record

Hi Experts, I have csv file with 30, 40 columns Pasting just 2 column for problem description. Need to print error if below combination is not present in file check for column-1 (DocumentNumber) and filter columns where value in DocumentNumber field is same. For all such rows, the field... (7 Replies)
Discussion started by: as7951
7 Replies

3. Shell Programming and Scripting

Counts not matching in file

I can not figure out why there are 56,548 unique entries in test.bed. However, perl and awk see only 56,543 and that # is what my analysis see's as well. What happened to the 5 missing? Thank you :). The file is attached as well. cmccabe@DTV-A5211QLM:~/Desktop/NGS/bed/bedtools$wc -l... (2 Replies)
Discussion started by: cmccabe
2 Replies

4. Shell Programming and Scripting

Extract timestamp from first record in xml file and it checks if not it will replace first record

I have test.xml <emp><id>101</id><name>AAA</name><date>06/06/14 1811</date></emp> <Join><id>101</id><city>london</city><date>06/06/14 2011</date></join> <Join><id>101</id><city>new york</city><date>06/06/14 1811</date></join> <Join><id>101</id><city>sydney</city><date>06/06/14... (2 Replies)
Discussion started by: vsraju
2 Replies

5. Shell Programming and Scripting

Read record from the text file contain multiple separated values & assign those values to variables

I have a file containing multiple values, some of them are pipe separated which are to be read as separate values and some of them are single value all are these need to store in variables. I need to read this file which is an input to my script Config.txt file name, first path, second... (7 Replies)
Discussion started by: ketanraut
7 Replies

6. Shell Programming and Scripting

Read record from the text file & assign those values to variables in the script

For eg: I have sample.txt file with 4 rows of record like: user1|password1 user2|password2 user3|password3 user4|password4 The username and password is sepsrated by '|' I want to get the 1st row value from the file and assign it to two different variables(username and password) in my... (1 Reply)
Discussion started by: priya001
1 Replies

7. Shell Programming and Scripting

New file should store all the 7 existing filenames and their record counts and ftp th

Hi, I need help regarding below concern. There is a script and it has 7 existing files(in a path say,. usr/appl/temp/file1.txt) and I need to create one new blank file say “file_count.txt” in the same script itself. Then the new file <file_count.txt> should store all the 7 filenames and... (1 Reply)
Discussion started by: pr293
1 Replies

8. Shell Programming and Scripting

Sending e-mail of record counts in 3 or more files

I am trying to load data into 3 tables simultaneously (which is working fine). Then when loaded, it should count the total number of records in all the 3 input files and send an e-mail to the user. The script is working fine, as far as loading all the 3 input files into the database tables, but... (3 Replies)
Discussion started by: msrahman
3 Replies

9. UNIX for Dummies Questions & Answers

how to get a file name & record count of csv file

HI , I am new to shell scripting , I have a requirement that I send a file for data quality ( original.csv) & i will be getting 4 files daily into a particular directory in return with cleansed data . the files may be clean.csv, unclean.csv , ... (2 Replies)
Discussion started by: sirik
2 Replies

10. Solaris

file size counts??

Hello experts, I do - $ ls -lhtr logs2007* Is it possible that i can get the results of- totals size in MB/KB for ALL "logs2007*" note: in the same directory I have "logs2006*" & "logs2007*" files. (4 Replies)
Discussion started by: thepurple
4 Replies
Login or Register to Ask a Question