How to truncate a string to x number characters?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to truncate a string to x number characters?
# 1  
Old 10-01-2012
How to truncate a string to x number characters?

Hello:

I have a large file which contains lines like the following:

1/t123ab, &Xx:1:1234:12345:123456@ABCDEFG... at -$100.00%

/t is a tab, spaces are as indicated
the string "&Xx:1:1234:12345:123456$ABCDEFG..." has a slightly variable number of numbers and letters, but it always starts with "&Xx" and includes 4 ":"
I want to truncate that string to 20 characters plus "..." at the end.

So basically, I would like to do the following

sed 's/&Xx[any_number_of_characters]\.\.\./&Xx[17_characters]\.\.\./'

leaving all the rest untouched.
However

sed 's/&Xx.+\.\.\./test\.\.\./' did not make any changes

sed 's/&Xx........../test/' does change part of the string into "test", so the "." is recognized as representing "any character but new line"
sed 's/&Xx.{10}/test/' does not alter the file

I am using Linux Mint version 12

are there other ways to achieve this truncation? (awk?)

Thanks in advance.
# 2  
Old 10-01-2012
Assumptions:
1) no more than 1 occurrence of the mentioned string (the one starting with &Xx) in a line,
2) no . character between the starting &Xx and ending ... in the string.
Code:
a='1	123ab, &Xx:1:1234:12345:123456@ABCDEFG... at -$100.00%'

echo "$a"|awk 'match($0,/&Xx[^.]*[.]{3}/){
if(RLENGTH>23)
 $0=substr($0,1,RSTART-1) substr($0,RSTART,20) substr($0,RSTART+RLENGTH-3)
}1'

1       123ab, &Xx:1:1234:12345:123... at -$100.00%


Last edited by elixir_sinari; 10-01-2012 at 07:57 AM..
This User Gave Thanks to elixir_sinari For This Post:
# 3  
Old 10-01-2012
Code:
perl -lne '{ s/(&Xx:.*)\.{3}/substr($1,0,20)."..."/ge;print ;}'

# 4  
Old 10-01-2012
Quote:
Originally Posted by msabhi
Code:
perl -lne '{ s/(&Xx:.*)\.{3}/substr($1,0,20)."..."/ge;print ;}'

This will avoid unnecessary substitutions:
Code:
perl -lne '/&Xx/ && do { s/(&Xx:.*?)\.{3}/substr($1,0,20)."..."/ge;print ;}'

# 5  
Old 10-01-2012
The assumption is correct: there are no "." characters in the string except the three at the very end and there s only one occurrence of the string in a line.

There are however also lines that do not contain such a string.

Neither the awk command nor the perl -lne did truncate the string on my computer.

I have no clue why, as the output from elixir_sinari indicates that awk would do the job.

perl -lne '/&Xx/ && do { s/(&Xx:.*?)\.{3}/substr($1,0,20)."..."/ge;print ;}'

eliminated all lines that did not contain the string - which was not an intended outcome, but left the string untouched
# 6  
Old 10-01-2012
@Tectone :
Yes please use my updated code with elixirs modification !

Anyways please take a look below..
Code:
$ perl -lne '{ s/(&Xx:.*?)\.{3}/substr($1,0,20)."..."/ge;print ;}' input_file
1       123ab, &Xx:1:1234:12345:123... at -$100.00%
somestring
newstring

Thats working fine for me !
This User Gave Thanks to msabhi For This Post:
# 7  
Old 10-01-2012
Try this...
Code:
awk '{match($0,"([0-9]+.*&Xx)(.*)",a);print a[1],substr(a[2],0,17)"..."}' infile

If solaris, use nawk

--ahamed
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Truncate all characters and numbers in string - using perl

hi, I have an data from file where it has 20110904 234516 <<hdd-10#console|0c.57,passed,5,28,READ,0,20822392,8,5,4,0,40,0,-1,0,29909,25000,835,3.3,0,0,0,0,implied,0,0,2011/9/5-2:3:17,2011/9/5-2:3:47,X292_0F15,TAP ,NQ09,J40LTG\r\r\n I want to remove characters till #console| i.e want... (1 Reply)
Discussion started by: asak
1 Replies

2. UNIX for Dummies Questions & Answers

AWK - number of specified characters in a string

Hello, I'm new to using AWK and would be grateful for some basic advice to get me started. I have a file consisting of 10 fields. Initially I wish to calculate the number of . , ~ and ^ characters in the 9th field ($9) of each line. This particular string also contains alphabetical... (6 Replies)
Discussion started by: Olly
6 Replies

3. Shell Programming and Scripting

Truncate string variable

Hi, I want to truncate a string variable, returned in the script. In perl I used the below and it worked. BRNo=BR12345 $BR = substr($BRNo, 2, 7) How can I do it in sh. Thanks ! (8 Replies)
Discussion started by: script2010
8 Replies

4. Shell Programming and Scripting

Truncate file name to 40 characters

Hello all. I would like to make a script (or two shell scripts) that will do the following. I need the maximum file name and directory name to be 38 characters long. As well, if shortening the file name ends up making all of the files in that directory have the same name, then I would like... (9 Replies)
Discussion started by: marcozd
9 Replies

5. Shell Programming and Scripting

number of characters in a string

Hi there, I have some user input in a variable called $VAR, and i need to ensure that the string is 5 or less characters .... does anybody know how i can count the characters in the variables ? any help would be great, cheers (2 Replies)
Discussion started by: rethink
2 Replies

6. Programming

Count the number of repeated characters in a given string

i have a string "dfasdfasdfadf" i want to count the number of times each character is repeated.. For instance, d is repeated 4 times, f is repeated 4 times.. can u give a program in c (1 Reply)
Discussion started by: pgmfourms
1 Replies

7. Shell Programming and Scripting

truncate a string from the end

hi, guys. I have a question. If I have a long string like this: 8.0K:/home/test/brownj How can I get a substring which starts from the last slash to the end of the string, so in this case, it will be brownj Thank you very much for you time in advance -Keyang (4 Replies)
Discussion started by: daikeyang
4 Replies

8. Shell Programming and Scripting

Truncate the content within alt attribute to first 250 characters.

I have a xml file which contains image tag as follows: <image><img src="wstc_0007_0007_0_img0001.jpg" width="351" height="450" alt="This is the cover page. Brazil &#x2022; Japan &#x2022; Korea &#x2022; Mexico &#x2022; Singapore &#x2022; Spain" type="photograph" orient="portrait"/></image> ... (5 Replies)
Discussion started by: parshant_bvcoe
5 Replies

9. Shell Programming and Scripting

Counting the number of occurances of all characters (a-z) in a string

Hi, I am trying out different scripts in PERL. I want to take a line/string as an input from the user and count the number of occurrances of all the alphabets (a..z) in the string. I tried doingit like this : #! /opt/exp/bin/perl print "Enter a string or line : "; $string = <STDIN>; chop... (5 Replies)
Discussion started by: rsendhilmani
5 Replies

10. UNIX for Dummies Questions & Answers

Truncate last <n> characters from a file

I am trying to concatenate 2 files, but before concatenation, I would like to strip off the final character from the first file. The final character is a form feed (ascii 012 / hex 0C) and there will be an unknown number of these characters in the file. It is only the very last one which I want... (1 Reply)
Discussion started by: Gwailo88
1 Replies
Login or Register to Ask a Question