Help to fetch first two characters from a word in perl


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Help to fetch first two characters from a word in perl
# 1  
Old 05-21-2010
Help to fetch first two characters from a word in perl

Hi All,
I have a word "DE_PR_Package__Basic" , i need to check if the first two characters of this words is DE or something else using perl script.
Can anyone pls let me know how to proceed?

Thanks in advance.
Giri!
# 2  
Old 05-21-2010
Depends on how you want to proceed with that information. To just print the line if a word matches your requirements:
Code:
perl -ne 'print if /\bDE_[\w_]+?/;' input.txt

If you want just the remainder of the word:
Code:
perl -ne 'print $1 if /\bDE_([\w_]+?)/' input.txt

This User Gave Thanks to pludi For This Post:
# 3  
Old 05-21-2010
check if this is what you are looking for:
Code:
$word = "DE_PR_Package__Basic";
if ($word=~ m/^DE/) {
    print "matches\n";
}
else {
    print "does not match\n";
}

in the regex, ^ is the start of string. You might want to add _ after DE so that the regex is ^DE_
This User Gave Thanks to Yogesh Sawant For This Post:
# 4  
Old 05-21-2010
Quote:
Originally Posted by pludi
Depends on how you want to proceed with that information. To just print the line if a word matches your requirements:
Code:
perl -ne 'print if /\bDE_[\w_]+?/;' input.txt

If you want just the remainder of the word:
Code:
perl -ne 'print $1 if /\bDE_([\w_]+?)/' input.txt


Thanks a lot Pludi, it was very useful for me.

---------- Post updated at 04:46 AM ---------- Previous update was at 04:45 AM ----------

Quote:
Originally Posted by Yogesh Sawant
check if this is what you are looking for:
Code:
$word = "DE_PR_Package__Basic";
if ($word=~ m/^DE/) {
    print "matches\n";
}
else {
    print "does not match\n";
}

in the regex, ^ is the start of string. You might want to add _ after DE so that the regex is ^DE_
Thanks a lot Yogesh, It was very useful for me.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Cut & Fetch word from string

I have a file with some SQL query, I want to fetch only Table Name from that file line by line. INPUT FILE SELECT * FROM $SCHM.TABLENAME1; ALTER TABLE $SCHM.TABLENAME1 ADD DateOfBirth date; INSERT INTO $SCHM.TABLENAME1 (CustomerName, Country) SELECT SupplierName, Country FROM $SCHM.TABLENAME2... (2 Replies)
Discussion started by: Pratik Majithia
2 Replies

2. Shell Programming and Scripting

Fetch the lines which contains special characters

Hi All, My source file contains special characters(Latin characters).I need to fetch only the lines which contains the special characters. The problem is i don't know which all latin/special characters can come in the source. Is there anyway to extract the lines which contain letters other... (3 Replies)
Discussion started by: joe!!
3 Replies

3. Shell Programming and Scripting

Fetch entries in front of specific word till next word

Hi all I have following file which I have to edit for research purpose file:///tmp/moz-screenshot.png body, div, table, thead, tbody, tfoot, tr, th, td, p { font-family: "Liberation Sans"; font-size: x-small; } Drug: KRP-104 QD Drug: Placebo Drug: Metformin|Drug:... (15 Replies)
Discussion started by: Priyanka Chopra
15 Replies

4. Shell Programming and Scripting

Search for the word and exporting 35 characters after that word using shell script

I have a file input.txt which have loads of weird characters, html tags and useful materials. I want to display 35 characters after the word "description" excluding weird characters like $&lmp and without html tags in the new file output.txt. Help me. Thanx in advance. I have attached the input... (4 Replies)
Discussion started by: sachit adhikari
4 Replies

5. Shell Programming and Scripting

Search for the word and exporting 35 characters after that word using shell script?

I have a file input.txt which have loads of weird characters, html tags and useful materials. I want to display 35 characters after the word description excluding weird characters like $$#$#@$#@***$# and without html tags in the new file output.txt. Help me. Thanx in advance. My final goal is to... (11 Replies)
Discussion started by: sachit adhikari
11 Replies

6. Shell Programming and Scripting

Match the word or words and fetch the entries

Hi all, I have 7 words Now I have 1 file which contain data in large number of rows and columns and 6th column contain any of these words or may be more than one words among above 7 words: I want script should search for the above mentioned 7 words in the 6th column ... (9 Replies)
Discussion started by: manigrover
9 Replies

7. Shell Programming and Scripting

match sentence and word adn fetch similar words in alist

Hi all, I have ot match sentence list and word list anf fetch similar words in a separate file second file with 2 columns So I want the output shuld be 2 columns like this (3 Replies)
Discussion started by: manigrover
3 Replies

8. Shell Programming and Scripting

Fetch particular characters from a column

Hi I wanted to fetch particular characters from a column.I have a file having columns separated by pipe operator. The contents of the file are Task | Task started on a component | B678C56D-96DA-4FFC-B40E-9A032A2EB12E-0000004 | service.adapter:UCF Version Creator ... (2 Replies)
Discussion started by: Prachi Gupta
2 Replies

9. Shell Programming and Scripting

extract whole thing in word, leaving behind last word. - perl

Hi, i've a string /u/user/DTE/T_LOGS/20110622_011532_TEST_11_HD_120/HD/TESi T_11_HD_120/hd-12 i need to get string, like /u/user/DTE/T_LOGS/20110622_011532_TEST_11_HD_120/HD the words from HD should get deleted, i need only a string till HD, i dont want to use any built in... (4 Replies)
Discussion started by: asak
4 Replies

10. Shell Programming and Scripting

awk fetch numbers after the word

Hi, I would want to fetch all the numbers after a word the number of characters could very. how can I do that? below is the example of the data and the expected output sample data 03 xxxx occurs 1090 times. 04 aslkja occurs 10 times. I would want to fetch 10 & 1090 separately. (13 Replies)
Discussion started by: ahmedwaseem2000
13 Replies
Login or Register to Ask a Question