Cut/Paste commands.

I am using K-Shell, to write a shell script.
I want to extract a part of variable in my script.

For example I have $1(first argument) as abc123.txt, how
to extract abc(first 3 characters) from $1.

Please help me
Thanx.
anilkumarvAsked:
Who is Participating?
 
mliberiConnect With a Mentor Commented:
- about extracting the first 3 chars from a variable:
you can do it without forking a 'cut' process with the following statement: "typedef -L3 L3=$1"

- about if statement syntax error:

YourCode------------

#! /usr/local/bin/ksh
       
filename=`date +%y%m%d`
dirname=`date +%y%m`
       
if(#$==1) then
{
  filename=$1
  dirname=$1|cut -c 1-4
  mkdir -p ./outfiles/$dirname
  parse $filename ./out/$dirname/$filename.out
  exit
}
------------------------------------------------------
contains lot of errors:

1) the if statement syntax is
if .....
  then
    ...
    ...
fi
so "{" and "}" are meaningless

2) the test expression must be a valid command, so you can use
test ...
[[ ... ]]
(( ... ))

the "(" and ")" pair simply tells the ksh to start a subshell and run the command "#$==0" (that isn't a valid command) inside it.

3) to get the number of argument given to the shell you must use "$#" instead of "#$"

4) ksh tokenize your command with before parsing it so spaces are very important. You must place a space between the keyword "if" and the first "(" of the test command

Modify your if statement according to the following:

if test $# -eq 0
  then
    ...
    exit
fi

if you prefer you can use the syntax:

if (($#==0))
  then
    ...
    exit
fi
0
 
ozoCommented:
cut -c 1-3
0
 
anilkumarvAuthor Commented:
Hai Ozo !!!
Thanx for your comment.
Please put as an answer and get the points.

By the way,I am getting error in using if statement.Could you please check my If syntax.
Error is '(' unmatched .

-------------My Code-------------------------------------------------------------

#! /usr/local/bin/ksh
 
filename=`date +%y%m%d`
dirname=`date +%y%m`
 
if(#$==1) then
{
  filename=$1
  dirname=$1|cut -c 1-4
  mkdir -p ./outfiles/$dirname
  parse $filename ./out/$dirname/$filename.out
  exit
}

# I will procede from here for further work.

----------------Code End-------------------------------------------
0
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
ozoCommented:
if(( $#==1 ))then
  filename=$1
  dirname=`echo $1|cut -c 1-4`
  mkdir -p ./outfiles/$dirname
  parse $filename ./out/$dirname/$filename.out
  exit
fi
0
 
anilkumarvAuthor Commented:
I am getting still error: fi is unexpected.

Please put as answer and take the points

Thanx
0
 
ozoCommented:
I don't like to put in an answer that doesn't work,
Can you post the code which causes the error: fi is unexpected.
0
 
anilkumarvAuthor Commented:
I am just pasting total script.Please check for Syntax error:fi not expected.
Thanx.


#! /usr/local/bin/ksh
 
#####################################################
#This Script is to process log files into formatted
#Text files and upload to the database.
#Author:Anil Kumar; Modified: Dec'1998
#####################################################
 
filename=`date +%y%m%d`
dirname=`date +%y%m`
 
#----Get the File from Command line-----------
if((#$==1))then
        filename=$1
#       dirname=$1|cut -c 1-4
#       mkdir -p ./outfiles/$dirname
#       parse $filename ./out/$dirname/$filename.out
        exit
fi
 
#--------Uploading the formated data in to database-----------
#for i in `ls ./outfiles/*'
#do
#bcp -S<server> -U<user> -P<pwd> <database>.<table> in $i
#done
 
 
#---------END------------

0
 
ozoCommented:
It looks like you typed #$ where you probably meant $#
Is the #!/usr/local/bin/ksh on the first line of your script?
(and is your /usr/local/bin/ksh different from /usr/bin/ksh?)
0
 
bcnCommented:
It`s a good solution and very easy.

You must a file_input that contains a name.htm
You must create is shell get_name

awk '
{
    pos_point=index($1,".")
    cadena=substr($1, 0, pos_point-1)
    printf(cadena)

}' file_in > file_out

If you type get_name the shell (get_name) it will generate a
file_out with only the name without extension.

Thanks.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.