[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

Cut/Paste commands.

Posted on 1999-01-20
9
Medium Priority
?
239 Views
Last Modified: 2010-04-21
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.
0
Comment
Question by:anilkumarv
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
9 Comments
 
LVL 84

Expert Comment

by:ozo
ID: 2009324
cut -c 1-3
0
 

Author Comment

by:anilkumarv
ID: 2009325
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
 
LVL 84

Expert Comment

by:ozo
ID: 2009326
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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 

Author Comment

by:anilkumarv
ID: 2009327
I am getting still error: fi is unexpected.

Please put as answer and take the points

Thanx
0
 
LVL 84

Expert Comment

by:ozo
ID: 2009328
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
 

Author Comment

by:anilkumarv
ID: 2009329
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
 
LVL 84

Expert Comment

by:ozo
ID: 2009330
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
 
LVL 3

Accepted Solution

by:
mliberi earned 20 total points
ID: 2009331
- 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
 

Expert Comment

by:bcn
ID: 2009332
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

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

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.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Hello fellow BSD lovers, I've created a patch process for patching openjdk6 for BSD (FreeBSD specifically), although I tried to keep all BSD versions in mind when creating my patch. Welcome to OpenJDK6 on BSD First let me start with a little …
My previous tech tip, Installing the Solaris OS From the Flash Archive On a Tape (http://www.experts-exchange.com/articles/OS/Unix/Solaris/Installing-the-Solaris-OS-From-the-Flash-Archive-on-a-Tape.html), discussed installing the Solaris Operating S…
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Suggested Courses

656 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question