Visit Our UNIX and Linux User Community


reading a file extracting information writing to a file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting reading a file extracting information writing to a file
# 1  
Old 10-11-2011
reading a file extracting information writing to a file

Hi
I am trying to extract information out of a file but keep getting grep cant open errors
the code is below:

Code:
 
#bash
#extract orders with blank address details
#
# obtain the current date
# set today to the current date ccyymmdd format
 today=`date +%c%m%d | cut -c24-31`
 echo ${today}
#infile
INFILE=/usr4/xj/data/XJFOMT01
#create a file
OUTFILE=${HOME}/inc13874.${today}.txt
if [ -f ${OUTFILE}]
  then
    NUM=`wc -l ${OUTFILE}`
  else
    NUM=0
fi
#get all orders for today that have blank addresses
#perhaps place in file or on stack
while read line
 do
   details=$(grep ${today} ${line} | cut -c0-300 | awk 'substr($0,266,10)=="          "' | cut -c0-37)
   DEPOT=$(cut -c0-5 ${details})
   COM=$(cut -c6-7 ${details})
   ORDER=$(cut -c8-13 ${details})
   ODATE=$(cut -c14-21 ${details})
   NADID=$(cut -c26-37 ${details})
   echo $( 'ODATE ' ${ODATE} 'ORDER ' ${ORDER} 'DEPOT ' ${DEPOT} 'NAD ID ' ${NADID}  > ${OUTFILE})
 done < ${INFILE}

exit

There is probably a very basic error, I have change ` to $( ) with no improvement and changed ${details} to $details no improvement
and ODATE is not visible

any help appreciated

Bruce
# 2  
Old 10-11-2011
If you are satisfied with the value of "today", try placing it in double quotes:
Code:
...grep "${today}" ...

# 3  
Old 10-12-2011
thanks.
The script has had to be changed due to the fact that the grep or cut command required the data to be passed in a different way otherwise the original 'line' or 'details' was altered.

Code:
#bash
#extract orders with blank address details
#
# obtain the current date
# set today to the current date ccyymmdd format
 today=`date +%c%m%d | cut -c21-28`
 echo ${today}
#
#infile
INFILE=/usr4/xj/data/XJFOMT01
echo ${INFILE}
#create a file
OUTFILE=${HOME}/inc13874.${today}.txt
if [ -f ${OUTFILE} ]
  then
    NUM=`wc -l ${OUTFILE}`
  else
    NUM=0
fi
echo ${NUM} '  ' ${OUTFILE}
#get all orders for today that have blank addresses
#perhaps place in file or on stack
   $( echo 'ODATE  ORDER  DEPOT NAD ID ' > ${OUTFILE} )
while read line
 do
   echo ${line}
   details=$( echo "${line}" | cut -c0-300 | awk 'substr($0,266,10)=="          "' |cut -c0-37 )
    DEPOT=$( echo "${details}" | cut -c0-5 )
   COM=$( echo "${details}" | cut -c6-7 )
   ORDER=$( echo "${details}" | cut -c8-13 )
   ODATE=$( echo "${details}" | cut -c14-21 )
   NADID=$( echo "${details}" | cut -c26-37 )
echo 'line ' "${ODATE}"
#if [ "${ODATE}" == "${today}" ]
#  then
#   $( echo 'ODATE ' "${ODATE}" 'ORDER ' "${ORDER}" 'DEPOT ' "${DEPOT}" 'NAD ID ' "${NADID}" >> "${OUTFILE}" )
#fi
 done < ${INFILE}
#ADDRESS=$(grep ${ORDER} XJFADT01 | cut -c47-320)
# mailx -s "blank orders inc13874" emailaddress < ${OUTFILE}
exit

The current problem is that the variables are not being populated

---------- Post updated at 02:06 PM ---------- Previous update was at 02:05 PM ----------

the
Code:
echo ${line}

returns the expected data by within do loop variables remain blank.
# 4  
Old 10-12-2011
Without seeing a sample of the input file, we will be guessing here.

Please display a sample of your file.
# 5  
Old 10-12-2011
This is a sample of the record - line is shown in 80 byte segments but is actually a continuous stream of 900ish characters all characters are text.

Code:
SYD  WW20049120110928SAU SYD010000045001         VYG774AHE 234546235036N00100001
00001000000000012BX                 2011092800011700        G  48N HZ N      MIN
EALLMINE             050   BY   0000000000000   0000000002000AUD0000000000000000
201109282350G774AHE    C BRUCE                                             21 SI
MMER PIECE               FENS
     5374         BROWNLOW                         02     456456
   BRUCE                                         Y
 YY16001  SYD  5646464        AU                  N
             N                            N
             C   N

I am wondering if I can open and close a file in the same script. Then reopen.
open output
close
open input

---------- Post updated at 03:50 PM ---------- Previous update was at 03:49 PM ----------

there are many records in the file I have shown only 1 to indicate how the data looks
# 6  
Old 10-12-2011
Following awk statement, are you sure it is dong what you want?
Code:
details=$( echo "${line}" | cut -c0-300 | awk 'substr($0,266,10)=="          "' | cut -c0-37 )

You can debug the issue by executing the script with set -x.

There are lot of cut commands which may be the culprit.

--ahamed

--ahamed
# 7  
Old 10-12-2011
At the first line:
Code:
 today=`date +%c%m%d | cut -c24-31`
 echo ${today}

what should it display?
When I try (OK AIX 4.2.1...) I get:
Code:
page:/etc $  today=`date +%c%m%d | cut -c24-31`
page:/etc $  echo ${today}
11012

Is that what you want? (I would have expected 111012...)
If its 111012 then why not simply:
Code:
page:/etc $ TODAY=$(date "+%y%m%d")
page:/etc $ echo $TODAY
111012

Ah Im using ksh...

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Reading XML file and extracting value

Dear All, I am reading one XML file to extract value from the particular tag:- Sample xml is below:- <KeyValuePairs> <Key>TestString</Key> <Value>Test12_Pollings</Value> </KeyValuePairs> I want to read the value for the KEY tag and there will be multiple key tags :- awk... (4 Replies)
Discussion started by: sharsour
4 Replies

2. Shell Programming and Scripting

Reading and writing in same file

Hi All, Here is my requirement. I am grepping through the log files and cutting some fields from the file to generate a csv file. Now I have to check if 2nd field is having some fixed value then with the help of 4th field I have to look in same log and run another grep command to retrieve the... (11 Replies)
Discussion started by: kmajumder
11 Replies

3. Shell Programming and Scripting

Searching for Log / Bad file and Reading and writing to a flat file

Need to develop a unix shell script for the below requirement and I need your assistance: 1) search for file.log and file.bad file in a directory and read them 2) pull out "Load_Start_Time", "Data_File_Name", "Error_Type" from log file 4) concatinate each row from bad file as... (3 Replies)
Discussion started by: mlpathir
3 Replies

4. Shell Programming and Scripting

Matching two file contents and extracting associated information

Hi, I am new to shell programming and need help. I have File1 with some ID numbers and File2 with ID number and some associated information. I want to match the ID numbers from File1 to contents in File2 and output a third file which pulls out the ID numbers and the associated information with... (2 Replies)
Discussion started by: newpro
2 Replies

5. Shell Programming and Scripting

Reading data from DataBase and Writing to a file

Hi All, Please help me in writing data to a file in one row. In database there is a column which contains large data which does not fit in the file in one row. The column contains list of paths. I want to write these paths to a file in one row. Please find the code below writes : ... (2 Replies)
Discussion started by: rajeshorpu
2 Replies

6. Programming

I need help with file reading/writing in C

Hello everybody, I'm trying to code a program which makes the following: It sends an ARP request frame and when it gets the reply, extracts the IP address of source and writes it to a .txt file. This is gonna be done with many hosts (with a for() loop), so, the text file would look like... (2 Replies)
Discussion started by: Zykl0n-B
2 Replies

7. UNIX for Dummies Questions & Answers

Log File Writing and Reading

Hi all, I have the following shell script code which tries to sftp and writes the log into the log file. TestConnection () { echo 'Connection to ' $DESTUSERNAME@$DESTHOSTNAME $SETDEBUG if ]; then rm $SCRIPT ; fi touch $SCRIPT echo "cd" $REMOTEDIR >> $SCRIPT echo "quit" >>... (10 Replies)
Discussion started by: valluvan
10 Replies

8. UNIX for Dummies Questions & Answers

reading ,writing,appending ,manipulating a file.

Hi my prob statement is to create a new file or to append to the 1tst file the followign chages. File 1: txt file. portfolio No a b c d abc 1 Any Any Any charString cds 2 values values values charString efd 3 can can can charString fdg 4 come come come charString... (4 Replies)
Discussion started by: szchmaltz
4 Replies

9. UNIX for Advanced & Expert Users

Reading a file and writing the file name to a param file.

Hi All, Not sure if this would be in a dummies sectiin or advanced. I'm looking for a script if someone has doen something like this. I have list of files - adc_earnedpoints.20070630.txt adc_earnedpoints.20070707.txt adc_earnedpoints.20070714.txt adc_earnedpoints.20070721.txt... (1 Reply)
Discussion started by: thebeginer
1 Replies

10. Programming

Reading and Writing file on LAN

Hi gurus I am not a C programmer but I need to read and write files on a computer on LAN using IP address. Suppose on a computer that has an IP 192.168.0.2 Any help or code example. I did in JAVA using URL, but do not know how to do in ANSI-C. In java: ------- URL url = new... (3 Replies)
Discussion started by: lucky001
3 Replies

Featured Tech Videos