ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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.
#!/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
#----------#
#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?
#----------#
#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.
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.
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)"
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.
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
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 ...
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.