Solved

Cut/Paste commands.

Posted on 1999-01-20
9
233 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
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
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

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.

 

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 10 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

Attention: This article will no longer be maintained. If you have any questions, please feel free to mail me. jgh@FreeBSD.org Please see http://www.freebsd.org/doc/en_US.ISO8859-1/articles/freebsd-update-server/ for the updated article. It is avail…
Let's say you need to move the data of a file system from one partition to another. This generally involves dismounting the file system, backing it up to tapes, and restoring it to a new partition. You may also copy the file system from one place to…
Learn how to get help with Linux/Unix bash shell commands. Use help to read help documents for built in bash shell commands.: Use man to interface with the online reference manuals for shell commands.: Use man to search man pages for unknown command…
Learn how to find files with the shell using the find and locate commands. Use locate to find a needle in a haystack.: With locate, check if the file still exists.: Use find to get the actual location of the file.:

839 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