LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 10-27-2015, 02:09 PM   #1
smturner1
Member
 
Registered: Oct 2009
Location: MI
Distribution: Arch 2.6.35
Posts: 107

Rep: Reputation: 1
script to create a file


I am trying to create a file from an existing file or a file list and I am grep'ing an item out of that file to get the records to populate that new file. I have determined if the files are compressed and whether the file is a list of file names or if it is a single file.

The issue that I am having is constructing the loop around the correct items so that my file is created by grabbing each item out of the file/files.

CL Syntax: file_create.sh input_file(File_List) item(item_list)

Any help you can provide is greatly appreciated.

Code:
#!/bin/ksh
set -x


#-------------------------------------------#
#Check to see if the file(s) are compressed #
#-------------------------------------------#
#---------------------------------------#
#Is this an invoice file or a file list #
#---------------------------------------#
#-------------#
#invoice file #
#-------------#

if [ $1 == "ABC*\.txt*" ]; then
   inv_file=$1

#------------------------#
#Is this file compressed #
#------------------------#

   file $inv_file | grep -i "compress" | read chk

   if [ -z "$chk"  ]; then
      display=cat
   else
      display=zcat
   fi
else

#----------#
#file list #
#----------#
#--------------------------------------------------#
#Is this file compressed. Create cat/zcat variable #
#--------------------------------------------------#
for x in `cat $1`;do
   file $x | grep -i "compress" | read chk

   inv_file=$x
done

   if [ -z "$chk"  ]; then
      display=cat
   else
      display=zcat
   fi

fi

#---------------------------------------#
#Is this a list of items or a single item#
#---------------------------------------#

if [-f "$2"];then
for i in `cat $2`;do
   item=$i
   done
else
  item=$2
fi

#-------------------------------------------------#
# Create the file from the inputs passed into the #
# Commandline                                     #
#-------------------------------------------------
while
$display $inv_file | head -1 >> invoice_output
$display $inv_file | grep $item >> invoice_output
wc -l invoice_output | nawk '(print $1)' | read count
echo "T|$count">>invoice_output

Last edited by smturner1; 10-29-2015 at 08:25 AM.
 
Old 10-28-2015, 08:05 AM   #2
michaelk
Moderator
 
Registered: Aug 2002
Posts: 25,700

Rep: Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895
Code:
#----------#
#file list #
#----------#
#--------------------------------------------------#
#Is this file compressed. Create cat/zcat variable #
#--------------------------------------------------#
for x in `cat $1`;do
   file $x | grep -i "compress" | read chk

   inv_file=$x
done

   if [ -z "$chk"  ]; then
      display=cat
   else
      display=zcat
   fi

fi
Do you want to grab the contents of the file list or the contents of each file within the list?


Code:
$display $inv_file | grep $btn >> invoice_output
What is $btn?
 
Old 10-28-2015, 08:09 AM   #3
zhjim
Senior Member
 
Registered: Oct 2004
Distribution: Debian Squeeze x86_64
Posts: 1,748
Blog Entries: 11

Rep: Reputation: 233Reputation: 233Reputation: 233
Quote:
#----------#
#file list #
#----------#
#--------------------------------------------------#
#Is this file compressed. Create cat/zcat variable #
#--------------------------------------------------#
for x in `cat $1`;do
file $x | grep -i "compress" | read chk

inv_file=$x
done

if [ -z "$chk" ]; then
display=cat
else
display=zcat
fi
If I get that correct $1 contains the file list but you only set cat|zcat once after looping through it all. This might lead to trouble if one of the files is not compressed. You only check the last file in the list for compression.
You would also only $display the last one of the file list due to overwriting $inv_file.

Either use an array to save all files and $display them at the end. Or $display them right within the for loop of the file list.

Not touching on the item_list cause i just dont get it.
 
Old 10-29-2015, 08:47 AM   #4
smturner1
Member
 
Registered: Oct 2009
Location: MI
Distribution: Arch 2.6.35
Posts: 107

Original Poster
Rep: Reputation: 1
I am trying to account for a couple scenarios. First scenario, an input file ($1) that contains a list of files and a file ($2) that contains a list of items. The second scenario, is a single file with one or more records containing an item from $2. $2 can be a single item (ABC123) or a file containing many items in a list form. $1 can be a single compressed file or a list of compressed/uncompressed files. $2 is never compressed.

I know that my issue is related to my looping constructs. The reason I know this is because every single command works when I execute it on the CLI. The issue as you guys have noted is that the commands are only being executed on the last inv_file and item.

BTW - $btn shoul dhave been $item. Sorry for the confusion.
 
Old 10-29-2015, 12:06 PM   #5
michaelk
Moderator
 
Registered: Aug 2002
Posts: 25,700

Rep: Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895
Code:
while
$display $inv_file | head -1 >> invoice_output
More clarification is required. If $1 is a list that includes compressed files are you trying to grab the first line of every file?
 
Old 10-29-2015, 12:39 PM   #6
smturner1
Member
 
Registered: Oct 2009
Location: MI
Distribution: Arch 2.6.35
Posts: 107

Original Poster
Rep: Reputation: 1
michaelk,

Yes, I am grabbing the header record from the source file in the list of files/file ($1) and pushing it into the invoice_output file. Then I need to loop through the
Code:
$display $inv_file | grep $item >> invoice_output
to find each record that has the $item (usoc), grab the entire record and add it to the invoice_output. Then it needs to go back to the outer loop, where the header portion exists and add the Trailer which should be "T|(line_number)"
 
Old 10-29-2015, 02:14 PM   #7
michaelk
Moderator
 
Registered: Aug 2002
Posts: 25,700

Rep: Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895
Worst case scenario from your description I think...
Code:
source file
   header
   file 1 (compressed)
        -> file1
        -> file2
   file 3 (not compressed 
        -> file1
        -> file2
Are the lists one item per line? What about a loop that iterates the source file and writes all the list items to a temporary file.

To compare the $2 to the temporary file look at the grep -f option.
 
Old 11-01-2015, 08:58 AM   #8
onebuck
Moderator
 
Registered: Jan 2005
Location: Central Florida 20 minutes from Disney World
Distribution: SlackwareŽ
Posts: 13,925
Blog Entries: 44

Rep: Reputation: 3159Reputation: 3159Reputation: 3159Reputation: 3159Reputation: 3159Reputation: 3159Reputation: 3159Reputation: 3159Reputation: 3159Reputation: 3159Reputation: 3159
Moderator response

Moved: This thread is more suitable in <Programming> and has been moved accordingly to help your thread/question get the exposure it deserves.
 
Old 11-01-2015, 10:59 AM   #9
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
Well I am glad someone can follow this because I am very confused???

I think it would help if you would provide an idea of what a file might look like inside and also the item list in file2?

Also, would you please explain what a 'file list' is? Should this mean there are multiple parameters on the command line (ie more than 2)?
 
Old 11-03-2015, 11:52 AM   #10
smturner1
Member
 
Registered: Oct 2009
Location: MI
Distribution: Arch 2.6.35
Posts: 107

Original Poster
Rep: Reputation: 1
Here we go...

A file ($1) is an invoice file compressed/uncompressed. There is a header record, detail records, summary records, and a trailer record.

A file List ($1) is a list of invoice files (see above for definition) that may or may not be compressed. Since this script will be executed in the home dir where all of the files reside file path unnecessary.

An $item is a serial number that exists in both the detail record and the summary record. If I grep out abc123 from a source file I could get multiple detail records, but only a single summary record. I want both record types.

The concept: I want to be able to create an invoice file from a production source file based on an invoice file or list of invoice files. I also would like to use a single item or a list of items as the criteria for the detail and summary records.

The lists are one item per line. Both of them.
 
Old 11-03-2015, 12:17 PM   #11
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
Cool, thanks for the details

So if I understand correctly, the process would be:

1. Confirm you received 2 arguments (currently not done)

2. Figure out is $1 is compressed or not ... it would seem here we make an assumption on what type of compression or you can guarantee always the same type?

3. Based on the first line of the file will tell you if you are looking at a single file or one with a list (currently not done). I am also assuming the first line will tell us this

4. Using info from 3 above, we either loop over files or execute the necessary commands, including $2, to get the details

Does this sound about right? If so, some will need answers from you to proceed
 
Old 11-03-2015, 02:07 PM   #12
michaelk
Moderator
 
Registered: Aug 2002
Posts: 25,700

Rep: Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895
From the first post it appears that "ABC*\.txt*" defines an invoice file versus a file list.

It is unclear if the file list also has a header record etc. I assume that you are creating a summary or trailer record by counting the lines in the file i.e.
wc -l invoice_output | nawk '(print $1)' | read count
echo "T|$count">>invoice_output

I think you need to process the invoice file separately from the file list.

Using the eval command you can iterate through the file list whether compressed or uncompressed. The ifs or pseudo code but the loop is real but untested.
Code:
# if $1 is a file list
if [ $1=="compressed" ] 
 cmd=zcat
else
 cmd=cat
fi

for line in $(eval $cmd $1)
do
   if [ $line==file]
      if [ $line=="compressed] 
        zcat $line >> temp.file
     else
        cat $line >> temp.file
     fi
  else
     echo $line >> temp.file  # this is just a guess.
  fi
done 

create header record ...
if [$2 == file]
   grep -f $2 $temp.file >> invoice_output
else
   grep $2 $temp.file >> invoice_output
create summary and trailer record ...
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
shell script to grep using one file and create files using another file samanp Programming 4 08-22-2012 09:27 PM
Create a script that asks the user for a file and then appends the date to the file larry5757 Linux - Newbie 2 12-04-2009 06:33 AM
Create a script to display file name, Inode, and size of any file. Has to be a script JaxsunApex Linux - Newbie 7 01-29-2007 08:15 PM
Script to create a log file satimis Programming 15 01-16-2006 06:39 AM
Create text file with a script zael Programming 3 06-02-2004 03:27 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:15 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration