Solved

Cut/Paste commands.

Posted on 1999-01-20
9
230 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
 

Author Comment

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

Please put as answer and take the points

Thanx
0
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 
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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

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…
Using libpcap/Jpcap to capture and send packets on Solaris version (10/11) Library used: 1.      Libpcap (http://www.tcpdump.org) Version 1.2 2.      Jpcap(http://netresearch.ics.uci.edu/kfujii/Jpcap/doc/index.html) Version 0.6 Prerequisite: 1.      GCC …
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…
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.

707 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now