Finding first 6 characters


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Finding first 6 characters
# 1  
Old 06-26-2013
Finding first 6 characters

Hi ,
I'm using KSH88

I tried the following example to get the last 6 characters from a string
Code:
 echo 'abcdefghids' | sed 's/.*\(.\{6\}\)$/\1/'

What chages i need to do to get the first 6 characters from the string
my desired output should be abcdef

Thank you
# 2  
Old 06-26-2013
why not cut

A simple cut would be
Code:
echo 'abcdefghids' | cut -c -6

This User Gave Thanks to krishmaths For This Post:
# 3  
Old 06-26-2013
Hello,

Could you please try to use the folliwng to get the last 6 characters as per your request.

Code:
 
awk '{print substr($0,(length($0)-6))}' FILE Name

Please let me know in case you have any queries.



Thanks,
R. Singh
This User Gave Thanks to RavinderSingh13 For This Post:
# 4  
Old 06-26-2013
parameter expansion

Code:
 
var=abcdefghids
echo ${var:$((-6))}

This User Gave Thanks to vidyadhar85 For This Post:
# 5  
Old 06-26-2013
grep

Hey,

Try sth like this,

If your grep supports -o option, It will give you first 6 characters of the string.

Code:
echo 'abcdefghij'|grep -o '^.\{6\}'

Cheers!!
-R
# 6  
Old 06-26-2013
To smile689,

vidyadhar85 has the best solution as it does not spawn another process. If you have a lot of data to process, spawning a process each time can be very time consuming. If you shell doesn't quite like his syntax (or you want something similar, but in just ksh) you can use this slightly messier one:-
Code:
var=abcdefghis
newvar="${var%${var##??????}}"
echo $newvar



Robin
Liverpool/Blackburn
UK
# 7  
Old 06-26-2013
Quote:
Originally Posted by vidyadhar85
parameter expansion

Code:
 
var=abcdefghids
echo ${var:$((-6))}

Unfortunately ${var:offset} is not available in ksh88
Quote:
Originally Posted by rbatte1
To smile689,

vidyadhar85 has the best solution as it does not spawn another process. If you have a lot of data to process, spawning a process each time can be very time consuming. If you shell doesn't quite like his syntax (or you want something similar, but in just ksh) you can use this slightly messier one:-
Code:
var=abcdefghis
newvar="${var%${var##??????}}"
echo $newvar

[..]
Good solution, if you add double quotes the subsitution gets protected from possible special characters in the replacement string :
Code:
var=abcdefghis
printf "%s\n" "${var%"${var##??????}"}"


--
(still thanks for that, @alister)

Last edited by Scrutinizer; 06-26-2013 at 03:39 PM..
This User Gave Thanks to Scrutinizer For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Finding distinct characters from flat file

Hi....I need one help.... I'm having a files which is having the data as follows... a b c c d d d e f Now I need to find out distinct characters from this file and the output should be as follows - a b c d e f Can you please help me on this? I'm using KSH script. (18 Replies)
Discussion started by: Krishanu Saha
18 Replies

2. Shell Programming and Scripting

Finding Strings between 2 characters in a file

Hi All, Assuming i have got a file test.dat which has contains as follows: Unix = abc def fgt jug 111 2222 3333 Linux = gggg pppp qqq C# = ccc ffff llll I would like to traverse through the file, get the 1st occurance of "=" and then need to get the sting... (22 Replies)
Discussion started by: rtagarra
22 Replies

3. UNIX for Dummies Questions & Answers

Finding specific series of strings or characters

After spending sometime playing around with my script I just cannot get it to do what I want. So I decided to ask. My file looks something like this: I am using the following code to extract sequences that contain dashes awk '/^>/{id=$0;next}{if (match($1,"-")) print id "\n" $0}' infile ... (17 Replies)
Discussion started by: Xterra
17 Replies

4. UNIX for Dummies Questions & Answers

finding and moving files based on the last three numerical characters in the filename

Hi, I have a series of files (upwards of 500) the filename format is as follows CC10-1234P1999.WGS84.p190, all in one directory. Now the last three numeric characters, in this case 999, can be anything from 001 to 999. I need to move some of them to a seperate directory, the ones I need to... (5 Replies)
Discussion started by: roche.j.mike
5 Replies

5. Shell Programming and Scripting

Finding File Names Ending In 3 Random Numerical Characters

Hi, I have a series of files (upwards of 500) the filename format is as follows CC10-1234P1999.WGS84.p190 each of this files is in a directory named for the file but excluding the extension. Now the last three numeric characters, in this case 999, can be anything from 001 to 999, I need to... (3 Replies)
Discussion started by: roche.j.mike
3 Replies

6. Shell Programming and Scripting

finding junk characters

Hi, Is there anyway to find the junk characters in a file.Consider the file has data as given below: 123|abc^M|Doctor^C #record 1 234|def|Med #record 2 345|dfg^C|Wrong^V #record 3 The junk characters are highlighted and this is a pipe delimited file. Is there anyway to... (20 Replies)
Discussion started by: ashwin3086
20 Replies

7. Shell Programming and Scripting

finding characters with new line

I have a file sample.txt with the below contents: Aaa - providioning add || dev - reeec dev kapl || ball - HERO || bal - provisioning pro || for given name i need the output to be the contents between - and || (excluding both) for eg : input - Aaa output -... (10 Replies)
Discussion started by: kinny
10 Replies

8. UNIX for Dummies Questions & Answers

Finding absolute pathnames longer than 100 characters

Please help. This simple problem is really stumping me. Is there are way to find absolute pathnames for all files on your system that are longer than 100 characters? I'm using bash shell to attempt it, but have come up with nothing so far. I appreciate any help offered. Nauty (2 Replies)
Discussion started by: nauty
2 Replies

9. UNIX for Dummies Questions & Answers

Unix Command for finding Nul Characters

Hi Folks, I have this windowed applescript application I use for sending out emails, it uses the Postfix daemon buit into my Mac Os X box, and a 3 SMTP servers (out of hundreds I send emails to) were refusing my messages replying "host smtp2.braspress.com.br said: 550 Requested action not... (2 Replies)
Discussion started by: fundidor
2 Replies

10. UNIX for Dummies Questions & Answers

finding exact characters

I want to grep from a file an exact character match. I tried grep -c "$a $b" $file where a=6 and b=2 the problem is that I get: 6 2 and 6 20 I just need a count of the occurrence. I'm using the Bourne shell. I've also tried grep -c '$a $b' $file; not sure how to do this - any suggestions? (3 Replies)
Discussion started by: jrdnoland1
3 Replies
Login or Register to Ask a Question