Truncate Specific Parts of a String


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Truncate Specific Parts of a String
# 1  
Old 02-16-2010
Java Truncate Specific Parts of a String

How do you truncate specific parts of a string.

Example:
1 This is the string

Goal:
This is the string

As you can see I'm trying to simply remove the first two characters of the string the number one and the space between the one and the word "this."

Your help is appreciated.

v/r
Roy
# 2  
Old 02-16-2010
To remove the number 1 and the space that follows it:
Code:
$ echo '1 This is the string' | sed 's/^1 //'
This is the string


The following will remove any leading non-space characters and the first space found:
Code:
$ echo 'blahbleh This is the string' | sed 's/^[^ ]* //'
This is the string

# 3  
Old 02-16-2010
if the first field is any thing not just "1" use below:-

Code:
echo '[anything] This is the string' | nawk  '{$1=""; sub(/^[ ]*/,"",$0)  }1'

SmilieSmilieSmilie
# 4  
Old 02-16-2010
MySQL

Thank you everyone, I got it; thanks.

I ended-up using:

getmsg=`grep "^1" testmsg.txt | sed "s/^1 //"`
echo $getmsg

Thanks,
Roy

Smilie
# 5  
Old 02-16-2010
You can do that without grep
Code:
getmsg=`sed -n "s/^1 //p" testmsg.txt `

# 6  
Old 02-16-2010
Quote:
Originally Posted by ahmad.diab
if the first field is any thing not just "1" use below:-

Code:
echo '[anything] This is the string' | nawk  '{$1=""; sub(/^[ ]*/,"",$0)  }1'

SmilieSmilieSmilie

That will mangle strings which contain consecutive whitespace (space, tab), since $0 is rebuilt internally using the OFS.

Code:
echo '[anything] This is    the string' | nawk  '{$1=""; sub(/^[ ]*/,"",$0)  }1'

... will print out ...

Code:
This is the string

instead of
Code:
This is    the string

# 7  
Old 02-16-2010
Quote:
Originally Posted by royarellano
How do you truncate specific parts of a string.

Example:
1 This is the string

Goal:
This is the string

As you can see I'm trying to simply remove the first two characters of the string the number one and the space between the one and the word "this."

There is no reason to use an external command (sed, awk, cut, etc.) which is orders of magnitude slower than doing it in the shell itself.

Code:
string='1 This is the string'

## remove the first two characters:
newstring=${string#??}

## remove up to and including the first space
newstring=${string#* }



---------- Post updated at 01:12 PM ---------- Previous update was at 01:03 PM ----------

Quote:
Originally Posted by alister
That will mangle strings which contain consecutive whitespace (space, tab), since $0 is rebuilt internally using the OFS.

Code:
echo '[anything] This is    the string' | nawk  '{$1=""; sub(/^[ ]*/,"",$0)  }1'

... will print out ...

Code:
This is the string

instead of
Code:
This is    the string

Code:
echo '[anything] This is    the string' | nawk '{sub(/^[! ]* */,"",$0);print}

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Delete specific parts in a .txt file

Hi all, I desperately need a small script which deletes everything in a particular .txt file when "Abs = {" appears till "},", and also when "B-1 = {" appears till "}," I would like all the text in between of those instances to be deleted, however, other text to be unedited (kept as it is).... (12 Replies)
Discussion started by: c_lady
12 Replies

2. Shell Programming and Scripting

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... (9 Replies)
Discussion started by: Tectona
9 Replies

3. Shell Programming and Scripting

perl, splitting out specific parts of the string

Hi there, I have an output from a command like this # ypcat -k netgroup.byuser| grep steven steven.* users_main,users_sysadmin,users_global,users_backup_team and wanted to pull the 'users' netgroups returned into a perl array, that will look like this users_main... (2 Replies)
Discussion started by: rethink
2 Replies

4. Shell Programming and Scripting

truncate string to integer or decimal

From strings stored in variables, I need to isolate and use the first numerical value contained within them. I will need to know how to produce an integer as well as a floating point decimal. It needs to work on any string regardless of what types of characters (if any) are preceding or following... (3 Replies)
Discussion started by: bradlecat
3 Replies

5. UNIX for Dummies Questions & Answers

Script required to truncate all the lines except a specific snippet.

Hi, I have a file with the following structure. XXXXX........... YYYYY........... ................. .................. ZZZZZZ...... qwerty_start.............. .................. ................. .................. querty_end................ .............................. (3 Replies)
Discussion started by: abinash
3 Replies

6. UNIX for Dummies Questions & Answers

Help with copying specific parts of a file structure

Hello. I need help with copying part of a file structure to another directory while still keeping the structure. For example look below: ../folder1/sub1/txt.txt ../folder1/sub2/pic.png ../folder2/sub1/pic.png ../folder2/sub2/txt.txt So in this I would like to copy only the directories and... (3 Replies)
Discussion started by: the
3 Replies

7. 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

8. Shell Programming and Scripting

How can i break a text file into parts that occur between a specific pattern

How can i break a text file into parts that occur between a specific pattern? I have text file having various xml many tags like which starts with the tag "<?xml version="1.0" encoding="utf-8"?>" . I have to break the whole file into several xmls by looking for the above pattern. All the... (9 Replies)
Discussion started by: abhinav192
9 Replies

9. 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

10. Shell Programming and Scripting

Removing parts of a specific field

All, I have a field in a comma seperated file with hundreds of lines and about 20 columns and I wish to remove all numbers after the decimal point in field 4 on each line and output the rest to another file or write it back to itself. File is like this 20070126, 123.0, GBP, 1234.5678,... (9 Replies)
Discussion started by: kieranh
9 Replies
Login or Register to Ask a Question