How to create .txt file by using function?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to create .txt file by using function?
# 1  
Old 03-11-2013
How to create .txt file by using function?

My Calling script is like below:
Code:
for file in `echo $LIST_OF_FILES` --listing filenames eg, xyz_meta_20110501_00000789.tar
do
file_name=`basename $file`
<call a function to create .txt file in below format>
 
done


Want to generate a .txt file that contains below data in ksh:
HTML Code:
SOURCE=XYZ
FILE_TYPE= META/DAT --generated dynamically if the files 2nd string is meta then META , if 2nd string is dat then DAT
NUM_REC=Will be generated dynamically through the script from the META/DAT file.wc -l?
BYTES= get the no. of bytes from the META/DAT file.wc-c?
DELIMITER=”|”
FLAG=”E”
Is it possible to write a function which will create the file in the above format? Thanks

Last edited by radoulov; 03-11-2013 at 06:52 AM..
# 2  
Old 03-11-2013
RedHat

first of all echo is not required in
Code:
for file in `echo $LIST_OF_FILES`

just make sure you are in the same dir where the files are or use absoulte path and you can pass files to the loop as
Code:
for file in file1 file2 file3----- fileN

now coming to the function

Code:
#!/bin/ksh

create_file()
{
logfilename=`echo $1|cut -d"." -f1`

source=`echo $1 | cut -d"_" -f1`
file_type=`echo $1 | cut -d"_" -f1`
num_rec=`wc -l $1 | awk '{print $1}'`
bytes=``wc -c $1 | awk '{print $1}'`

printf "SOURCE=${source}\nFILE_TYPE=${file_type}\nNUM_REC=${num_rec}\nBYTES=${bytes}\nDELIMETER=\"|\"\nFLAG=\"E\"" > ${logfilename}.txt

}


for file in xyz_meta_20110501_00000789.tar abc_dat_20110501_00000789.tar
do
file_name=`basename $file` ## this is not required unless you are using absolute path
create_file $file_name
done

this will create your log files as
Code:
xyz_meta_20110501_00000789.txt
abc_dat_20110501_00000789.txt

This User Gave Thanks to sam05121988 For This Post:
# 3  
Old 03-11-2013
If you're using a recent version of bash or ksh, put this into your function:
Code:
FN=${1%.*}".txt"
Y=${1#*_}
read numrec bytes REST < <(wc -cl $1)

echo SOURCE=${1%%_*}     >"$FN"
echo FILE_TYPE=${Y%%_*} >>"$FN"
echo NUM_REC=$numrec    >>"$FN"
echo BYTES=$bytes       >>"$FN"
echo "DELIMITER=\"|\""  >>"$FN"
echo "FLAG=\"E\""       >>"$FN"

# 4  
Old 03-11-2013
If you want uppercase for FILE_TYPE you may need to call tr eg:

Code:
echo FILE_TYPE=${Y%%_*} | tr -s '[:lower:]'  '[:upper:]'  >> $FN

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

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Copy the content from txt file and create a html file

I have a txt file with a list of error messages in a xml tag format, and each error message is separated with a identifier(endresult).Need to split that and copy and create a new html file.Error message has some special character. how to escape the special character and insert my data into the... (7 Replies)
Discussion started by: DevAakash
7 Replies

2. Programming

Writing a UNIX shell script to call a C function and redirecting data to a .txt file

Hi, I am complete new to C programming and shell scripting. I just wrote a simple C code to calculate integral using trapezoid rule. I am prompting user to pass me No. of equally spaced points , N , upper and lower limit. My code looks as follows so far: #include<stdio.h> #include<string.h>... (2 Replies)
Discussion started by: bjhjh
2 Replies

3. Programming

A Function To Create A 1 Second Sinewave WAVE Beep File In Python.

sinebeep.py Creating an audio WAVE file called... beep.wav ...that can be played using almost ANY audio player available. This simple DEMO snippet of code generates a 1 second sinewave WAVE file. It IS saved inside the CURRENT drawer so that you can find it... ;o) Note that the... (1 Reply)
Discussion started by: wisecracker
1 Replies

4. UNIX for Dummies Questions & Answers

How to create a .csv file from 2 different .txt files?

Hi, I need to create a .csv file from information that i have in two different tab delimited .txt file. I just want to select some of the columns of each .txt file and paste them into a .cvs file. My files look like: File 1 transcript_id Seq. Description Seq. Length ... (2 Replies)
Discussion started by: alisrpp
2 Replies

5. Shell Programming and Scripting

create txt file form data file

File A.txt LL07 LL07_B_1 20 LL85 LL85_A_1 40 LL85 LL85_B_1 40 LL85 LL85_C_1 30 LL37 LL37_A_1 60 LL37 LL37_B_1 20 LL37 LL37_C_1 50 I want cretae diffrent tex file base of above file Should be threee text file LL07.txt LL85.txt LL37.txt Eaach text file have below data... (2 Replies)
Discussion started by: asavaliya
2 Replies

6. Shell Programming and Scripting

create txt file form data file and add some line on it

Hi Guys, I have file A.txt File A Data AK1521 AK2536 AK3164 I want create text file of all data above and write some data on each file. want Output on below folder /home/kka/out AK1521.txt Hi Welocme (3 Replies)
Discussion started by: asavaliya
3 Replies

7. Shell Programming and Scripting

Select some lines from a txt file and create a new file with awk

Hi there, I have a text file with several colums separated by "|;#" I need to search the file extracting all columns starting with the value of "1" or "2" saving in a separate file just the first 7 columns of each row maching the criteria, with replacement of the saparators in the nearly created... (4 Replies)
Discussion started by: capnino
4 Replies

8. Shell Programming and Scripting

how to create file.txt and add current date in file content

Hey guy, how to make bash script to create foo.txt file and add current date into file content and that file always append. example: today the script run and add today date into content foo.txt and tomorrow the script will run and add tomorrow date in content foo.txt without remove today... (3 Replies)
Discussion started by: chenboly
3 Replies

9. UNIX for Dummies Questions & Answers

create file .txt

hi.i want to create a bash script called countfiles.sh, that will count the pack of files under the current file that their term satisfy a specific pattern. The script must show the total per file or on the whole. thank tou (4 Replies)
Discussion started by: battaglia
4 Replies

10. Shell Programming and Scripting

How to create SQRT function in catenate file

HI, I have a file which i catenate and using the fields in the file, I would like to get sqrt of it. I tried to man the function but it normally would need an echo as well as bc. What I am intending to find out is catenate a file where let say cat a.txt| awk ' { t= h*($3+$2); t=... (7 Replies)
Discussion started by: ahjiefreak
7 Replies
Login or Register to Ask a Question