Solved

Cut/Paste commands.

Posted on 1999-01-20
9
232 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
NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

 

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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

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…
Installing FreeBSD… FreeBSD is a darling of an operating system. The stability and usability make it a clear choice for servers and desktops (for the cunning). Savvy?  The Ports collection makes available every popular FOSS application and packag…
Learn how to navigate the file tree with the shell. Use pwd to print the current working directory: Use ls to list a directory's contents: Use cd to change to a new directory: Use wildcards instead of typing out long directory names: Use ../ to move…
In a previous video, we went over how to export a DynamoDB table into Amazon S3.  In this video, we show how to load the export from S3 into a DynamoDB table.

831 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