How to omit a part of the string


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to omit a part of the string
# 1  
Old 09-13-2011
Question How to omit a part of the string

I have a file, f1 with content:

File f1:
Code:
abc_English_Dlr
def_123_English_Dlr
qwe_98_yu_English_Dlr
dsw_1_English_Dlr

I want to remove "_English_Dlr" from it.

I used cut command but the problem is there may be a case for getting 1 or more fields before _English_Dlr . So Cut command is useless.

Can oneone help me this using SED ??

Thanks!
# 2  
Old 09-13-2011
Code:
vi YOUR_FILE
:%s/_English_Dlr//
:wq

# 3  
Old 09-13-2011
Code:
$ cat f
abc_English_Dlr
def_123_English_Dlr
qwe_98_yu_English_Dlr
dsw_1_English_Dlr

Code:
$  sed 's/_English_Dlr//' f
abc
def_123
qwe_98_yu
dsw_1


Guru.
# 4  
Old 09-13-2011
Code:
$ sed 's,_English_Dlr,,g' infile
$
$ nawk '{sub(/_English_Dlr/,"")};1' infile
$


Last edited by jayan_jay; 09-13-2011 at 07:42 AM.. Reason: with nawk ...
# 5  
Old 09-13-2011
Code:
$ nawk -F"_English_Dlr" '{print $1}' test
abc
def_123
qwe_98_yu
dsw_1

# 6  
Old 09-13-2011
Code:
perl -lpe '$_=reverse' INPUTFILE | cut -d '_' -f3- | perl -lpe '$_=reverse'

Smilie
# 7  
Old 09-13-2011
Code:
$ perl -lane '@a=split(/_English_Dlr/);print $a[0]' test    
abc
def_123
qwe_98_yu
dsw_1

---------- Post updated at 04:32 PM ---------- Previous update was at 04:19 PM ----------

One more in perl

Code:
 
$ perl -lne '$_=~s/_English_Dlr.*//;print ' test                                                                                                   
abc
def_123
qwe_98_yu
dsw_1

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Intersection by part of the string

Hi, I like to intersect two files based on their first columns. Here is the code which does the trick for me: awk 'NR==FNR{A;next}$1 in A' file1 file2 However, this only looks for exact matches between the two files in the first column. I need them to be printed even if part of the string... (10 Replies)
Discussion started by: a_bahreini
10 Replies

2. Shell Programming and Scripting

Part of string with script

Hi All, I have few files named. abcd.docx abcde.doc abcdef.temp I wish if I could extract the string upto .(dot),and not the extension. Thanks a lot. Kind regards, Indranil. (4 Replies)
Discussion started by: Indra2011
4 Replies

3. Shell Programming and Scripting

Deleting part of a string : string manipulation

i have something like this... echo "teCertificateId" | awk -F'Id' '{ print $1 }' | awk -F'te' '{ print $2 }' Certifica the awk should remove 'te' only if it is present at the start of the string.. anywhere else it should ignore it. expected output is Certificate (7 Replies)
Discussion started by: vivek d r
7 Replies

4. Shell Programming and Scripting

Replace a part of the string

Hi I need to Replace a part of string in between one complete string. For e.g.. in the file the value is as: jobnm_$code_xyz_001 In script we are having a variable code=$3, where $3=ab final output should be jobnm_ab_xyz_001. But it is not working. Your help will be... (1 Reply)
Discussion started by: vee_789
1 Replies

5. Shell Programming and Scripting

Need to take one part from a string

I have a string something like "/opt/src/default.cfg" OR /opt/src/common/one This whole string stored in an array. The problem is this string is not constant and it will keep on changing as lot of strings are stored in the array and it will be look like :- case 1 /opt/src/default.cfg ... (8 Replies)
Discussion started by: Renjesh
8 Replies

6. Shell Programming and Scripting

Part of a string

Hi mates, I am doing a script in ksh. I have the following string: /opt/one/two/four/five/myFile.txt And I have a variable: echo "${variable}" -> /opt/one/two/ I would like to have just the string: four/five/myFile.txt What is the better way to do that? Thanks in... (3 Replies)
Discussion started by: gonzaloron
3 Replies

7. Shell Programming and Scripting

Getting part of a string

Hi, I have a string assinged to a varaible as below. FILE=/var/adm/message If $FILE is the value where it stores the path of message file. I would like to extract the location of the file message as below LOCATION=/var/adm FILE may have value like /var/adm/xxxx/message ... (2 Replies)
Discussion started by: raghu.amilineni
2 Replies

8. UNIX for Dummies Questions & Answers

Printing a part of a string

hi I have the input as follows ABC =893 JKL = "jee" alias PQR = 9833 alias STUVE = "kuiue" I should be able to print as follows ABC JKL alias PQR alias STUVE Thanks Suresh (7 Replies)
Discussion started by: ssuresh1999
7 Replies

9. Shell Programming and Scripting

Need help regarding replacing a part of string

Hi all suppose i have a string "abacus sabre", i need to replace occurences 'ab' with 'cd' and i need to store this result into same string and i need to return this result from script to the calling function, where as the string is passed from calling function. i tried like this ... (1 Reply)
Discussion started by: veerapureddy
1 Replies

10. Shell Programming and Scripting

cutting part of string

Hi, I wanted to cut a specific portion from given string. How would I do that? Example: /u09/core/inbound/abc.txt is my string. I want abc.txt in a variable. Please help me. Regards, Dhaval (3 Replies)
Discussion started by: dhaval_khamar
3 Replies
Login or Register to Ask a Question