Operating System - HP-UX
1833740 Members
2592 Online
110063 Solutions
New Discussion

Question regarding cat......

 
Joe Profaizer
Super Advisor

Question regarding cat......

I need to build the command: cat SanNon1MAR2005.txt SanNon2MAR2005.txt > SanNonMAR2005
when given a variable containing "SanNonMAR2005". And that's all we have to work with.

I need to split the contents of the variable into 2 pieces (SanNon) and (MAR2005) and insert a "1" and a "2" where necessary to build the stmt above.

If InputFile is my variable then 'echo $Inputfile | cut -c1-6' would give me SanNon. That's about as far as I've gotten....

6 REPLIES 6
A. Clay Stephenson
Acclaimed Contributor

Re: Question regarding cat......

There are many ways to do this and if you look at an earlier patterm matching thread today you would get some good Perl ideas but to fix your approach:

PT1=$(echo "${Inputfile}" | cut -c1-6)
PT2=$(echo "${Inputfile}" | cut -c7-)

V1="${PT1}1${PT2}"
echo "V1 = ${V1}"
V2="${PT1}2${PT2}"
echo "V2 = ${V2}"

If it ain't broke, I can fix that.
RAC_1
Honored Contributor

Re: Question regarding cat......

var='SanNonMAR2005'
echo ${var#SanNon}
echo ${var%MAR*}

Anil
There is no substitute to HARDWORK
Sandman!
Honored Contributor

Re: Question regarding cat......

Joe,

Here's what you are looking for:
=============================================
Inputfile=SanNonMAR2005

echo $Inputfile | awk -F"M" 'BEGIN{OFS="M"} {print $1"1",$2}'
=============================================

the above code will give you "SanNon1MAR2005.txt". Replace the "1" in the awk command with the appropriate number (say 2) to build your cat command.

hope this helps!
A. Clay Stephenson
Acclaimed Contributor

Re: Question regarding cat......

As I mentionioned earlier Perl is really the way to go for pattern matching.

Try this:
-----------------------------------
#!/usr/bin/sh

typeset -i STAT=1
typeset FNAME=''
typeset PT1=''
typeset MON=''
typeset YR=''
while [[ ${STAT} -ne 0 ]]
do
echo "Enter Filename: \c"
read FNAME
perl -e 'if ($ARGV[0] =~ /(.+)(JAN|FEB|MAR|APR|MAY|JUN|JUL|AUG|SEP|OCT|NOV|DEC)(\d{4})$/ ) { printf ("%s\t%s\t%s\n",$1,$2,$3); exit(0); } else { exit(1); }' "${FNAME}" | read PT1 MON YR
STAT=${?}
if [[ ${STAT} -ne 0 ]]
then
echo "Bad Format: FnameMONYYYY" >&2
fi
done
typeset CMD="cat ${PT1}1${MON}${YR}.txt ${PT1}2${MON}${YR}.txt > \
${PT1}${MON}${YR}"
echo "Cmd = \"${CMD}\""
exit ${STAT}
---------------------------------------

Note: The perl command including the read PT1 MON YR is all one line. The power of this approach is that it actually checks your filename for valid composition (valid monthname, 4 digit year). I've looked this over carefully and it should work. so that all you need to do is ${CMD} to actually exucute the built-up string.


If it ain't broke, I can fix that.
Amit Agarwal_1
Trusted Contributor

Re: Question regarding cat......

You can use awk for this.

echo $InputFile | awk '{ printf("%s1%s2", substr($0, 1, 6), substr($0, 7));}'
-Amit
Sandman!
Honored Contributor

Re: Question regarding cat......

The "cat" command can be constructed with the awk excerpt given below:

==============================================================
# export InputFile="SanNonMAR2005"

# echo $Inputfile | awk -F"M" '{system("cat "$1"1M"$2".txt "$1"2M"$2".txt > "$0)}'
==============================================================

This will execute the code you want, i.e.

# cat SanNon1MAR2005.txt SanNon2MAR2005.txt > SanNonMAR2005

cheers!