cut First charecter in any string


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting cut First charecter in any string
# 1  
Old 05-29-2007
cut First charecter in any string

I wannt to cut first char of any string.

str=A2465443

out put will be.
str1=A
str2=2465443.
I tried
str2=${?%srt}
str1=${str#$str2}

it is not working.
# 2  
Old 05-29-2007
Code:
# var="something"
# echo ${var:0:1}
s

# 3  
Old 05-29-2007
try this

echo "A1234" | cut -c 2-

echo "A1234" | cut -c 0-1
# 4  
Old 05-29-2007
Code:
$ str=A2465443
$ str2=${str#?}
$ str1=${str%$str2}
$ echo "<$str>=<$str1><$str2>"
<A2465443>=<A><2465443>
$

Jean-Pierre.
# 5  
Old 05-29-2007
Quote:
Originally Posted by rinku
I wannt to cut first char of any string.

str=A2465443

out put will be.
str1=A
str2=2465443.
I tried
str2=${?%srt}
str1=${str#$str2}

it is not working.
str2=${str#?}
str1=${str%$str2}

$ echo $str1
A
$ echo $str2
2465443
# 6  
Old 05-29-2007
Quote:
Originally Posted by ghostdog74
Code:
# var="something"
# echo ${var:0:1}
s

This will not work in Korn shell
# 7  
Old 05-29-2007
Quote:
Originally Posted by praveenkumar_l
This will not work in Korn shell
It will work in ksh93 and some other recent Korn shell variants.

Code:
$ print ${.sh.version}
Version M-12/28/93d
$ echo ${var:0:1}
s


Another (portable Smilie? ) variant:

Code:
printf "%.1s\n" "$var"


Last edited by radoulov; 05-29-2007 at 08:32 AM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Cut string from shell

How can I cut the date and leave it in 3 variable? date="20181219" year=substr(date,1,4) monthsubstr(date,4,2) daysubstr(date,6,2) result year=2018 month=12 day=19 this does not work for me (3 Replies)
Discussion started by: tricampeon81
3 Replies

2. UNIX for Dummies Questions & Answers

Cut only time from string

Hi Guys, I have following different lines separated by multiple spaces. And i need to cut only the time part (16:53) from the string. How can we achieve it? remedy 29210 1 1 07:55 ? 00:06:20 /opt/bmc/java/jre1.6.0_17/bin/java -Xms1024m -Xmx2048m -XX:MaxPermSize=1024m... (12 Replies)
Discussion started by: Khushbu
12 Replies

3. Shell Programming and Scripting

Cut the string

---------- Post updated at 10:31 AM ---------- Previous update was at 10:28 AM ---------- Hello, I am trying to get the string cut based on the following needs: String1=Submitted request 25574824 for CONCURRENT SQLAP RUAPACTUALSEXT Y RUAPACTUALS122313100616.dat "2013/01/12 14:50:44"... (6 Replies)
Discussion started by: cartrider
6 Replies

4. Shell Programming and Scripting

Cut a string for last 8 characters

Hello All I have a file like this abc.tpt.ctl bdc.tpt.ctl cdw.tpt.ctl I have looped every line using the for Loop, now I want to take each line and cut the .tpt.ctl part of it and store it in a variable and use the variable in same loop. The part I am stuck at is how do I cut the last... (9 Replies)
Discussion started by: nnani
9 Replies

5. Shell Programming and Scripting

Cut the string

Hi in a directory i've files having the following name for_category_info_19990101984301 for_catgry_meta_19991111214601 ------- I just want the name till year and month i.e; for_category_info_199901 for_catgry_meta_199911 How can i achieve the above string Thanks (2 Replies)
Discussion started by: smile689
2 Replies

6. UNIX for Dummies Questions & Answers

How to cut a string from a variable

I want to cut column 1 to 5 echo $somevariable | cut -c 1-5 not working (6 Replies)
Discussion started by: ezee
6 Replies

7. Shell Programming and Scripting

backward string cut

I need only the last .ko files to be stripped from the whole result., ie libiscsi2.ko, scsi_transport_iscsi2.ko etc.. kernel/drivers/scsi/libiscsi2.ko kernel/drivers/scsi/scsi_transport_iscsi2.ko kernel/drivers/scsi/scsi_transport_iscsi.ko kernel/fs/nls/nls_utf8.ko... (4 Replies)
Discussion started by: anilcliff
4 Replies

8. UNIX for Dummies Questions & Answers

how to cut prefix from a string

I have a file: chromosome1:436728 chromosome2:32892 ..... chromosome22:23781 I just want to get the number, not the prefix "chromosomeX", so I want to remove all the prefix ahead of the numbers. How can I do that?? Thanks!!! (PS: give me some very simple command so that I can understand... (4 Replies)
Discussion started by: kaixinsjtu
4 Replies

9. Shell Programming and Scripting

Comapring files charecter by charecter using AWK/Shell Script

Hi... I have a requrement to compare two files. for e.g. File 1 2007/08/19 09:48:10 DH-032 $APTA1: Device AATD8029 2007/08/19 09:48:10 DH-045 $APTA1: Device AATD8029 2007/08/19 09:48:10 DH-043 $APTA1: Device AATD8029 File 2 2007-08-19 09:48:10 DH-032... (1 Reply)
Discussion started by: evvander
1 Replies

10. Shell Programming and Scripting

read string, check string length and cut

Hello All, Plz help me with: I have a csv file with data separated by ',' and optionally enclosed by "". I want to check each of these values to see if they exceed the specified string length, and if they do I want to cut just that value to the max length allowed and keep the csv format as it... (9 Replies)
Discussion started by: ozzy80
9 Replies
Login or Register to Ask a Question