Visit Our UNIX and Linux User Community


Extracting a word from a variable


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Extracting a word from a variable
# 1  
Old 09-16-2011
Extracting a word from a variable

Hi Guys,

Need you quick assistance on the below, trying to extract a word from a variable

i.e. acmi101[space]acmi102[space]acmi103[space]acmi104

When i use the following code awk '{gsub(/cmi102/,"")};1' it leaves a space in the variable, need to get rid of the space that it leaves. Any ideas.

the above result, would be

Code:
acmi101[space][space]acmi103[space]acmi104

need it to be

Code:
acmi101[space]acmi103[space]acmi104

need to get rid of the extra space.


Moderator's Comments:
Mod Comment Please use code tags!
# 2  
Old 09-16-2011
Code:
gsub(/cmi102  */,"")

# 3  
Old 09-16-2011
Fanstastic.......now why didnt I think of that. Many Thanks radoulov

---------- Post updated at 11:58 AM ---------- Previous update was at 11:32 AM ----------

radoulov,

The gsub(/cmi102 */,"") does not get rid of the acmi102, just removes the cmi102, but the "a" is still present, tried using the below to no avail...any help?

Code:
gsub(/+cmi102 */,"")

---------- Post updated at 12:00 PM ---------- Previous update was at 11:58 AM ----------

also note, that the "a" could be a many letters, so I could not used gsub(/acmi102 */,""), as I might be searching for axfbcmi102....etc....

Last edited by radoulov; 09-16-2011 at 09:11 AM.. Reason: Code tags!
# 4  
Old 09-16-2011
Code:
gsub(/[^ ]*cmi102 */,"")


Last edited by radoulov; 09-16-2011 at 09:10 AM.. Reason: Code tags.

Previous Thread | Next Thread
Test Your Knowledge in Computers #434
Difficulty: Medium
jQuery is a vanilla JavaScript library designed to simplify DOM-oriented client-side HTML scripting along with offering cross-browser compatibility.
True or False?

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

sed - extracting the first word only if match

Hello. from a text file, I want to get only the first word ( before blank ) following code= grep -i -e "WORD1" "/path/to/text/file.txt | sed -n 's/WORD1\+//p' | sed -n 's/code=/\1/p' return an error. sed: -e expression #1, char 12: invalid reference \1 on `s' command's RHSFor debugging... (12 Replies)
Discussion started by: jcdole
12 Replies

2. Shell Programming and Scripting

Replacing first word while extracting

Hello All, I am extracting a part of file. the file looks as follows USING CHARACTER SET UTF8 DEFINE JOB ( DEFINE SCHEMA Flat_File_Schema ( cntnt_id VARCHAR(10) ); DEFINE OPERATOR o_mload TYPE update SCHEMA * ATTRIBUTES ( VARCHAR TdpId = @TdpId (5 Replies)
Discussion started by: nnani
5 Replies

3. Shell Programming and Scripting

Bash - Extracting whole word containing substring

hello. I get this text when using some command : S | Name | Type | Version | Arch | Repository --+-----------------+---------+---------+------+------------- | AdobeReader_enu | package | 9.5.4-1 | i486 | zypper_local I need to get "AdobeReader_enu" from the the pattern "Ado"... (7 Replies)
Discussion started by: jcdole
7 Replies

4. UNIX for Dummies Questions & Answers

Extracting part of a word

I have the code message={TP=2012:09:23:00:00:00:GMT,SD=2012:09:23:00:00:00:GMT,SP=2,FT=CCGT,FG=3605} I want to extract the FG=3605 parts of this. Please help. I am trying to do this using awk or unix. (5 Replies)
Discussion started by: JenniferTopham
5 Replies

5. Shell Programming and Scripting

Problems extracting word using SED

I have a file containing strings such as: UPDATE PS_CA_BI_FF2_TA3 SET DELETE_ME = 'Y' WHERE PROCESS_INSTANCE BI.LAST_UPDATE_DTTM FROM PS_CA_BP_LINES LINE, PS_INTFC_BI BI WHERE EXISTS ( SELECT 'X' FROM PS_CA_BILL_PLAN BP WHERE BP.CONTRACT_NUM %Select(COUNTER4) SELECT COUNT(*) FROM PS_INTFC_BI... (2 Replies)
Discussion started by: simpletech369
2 Replies

6. Shell Programming and Scripting

extracting part of a line excluding particular word from it

here is the line on which i want to process `empNo` int(13) NOT NULL AUTO_INCREMENT, it sometimes doesnt have comma at the end too `empNo` int(13) NOT NULL AUTO_INCREMENT i want to extract all except "AUTO_INCREMENT" not only this line i ,want the code to work on any line if it has... (5 Replies)
Discussion started by: vivek d r
5 Replies

7. Shell Programming and Scripting

extracting sentences that only contain a word

Hi guys Need your help how do I extract sentences with only a word i.e. today is hot hot very humid humid2 Sample output hot (6 Replies)
Discussion started by: jamestan
6 Replies

8. UNIX for Dummies Questions & Answers

extracting sentences that only contain a word

Hi guys Need your help how do I extract sentences with only a word i.e. today is hot hot very humid humid2 Sample output hot very (0 Replies)
Discussion started by: jamestan
0 Replies

9. Shell Programming and Scripting

Extracting the strings matching a pattern from a word

Hi All , I need to extract the strings that are matching with the pattern : CUST.<AnyStringOfAnyLength>.<AnyStringOfAnyLength> from a file and then write all these string into another file. e.g. If a file SOURCE contains following lines : IF(CUST.ABCD.EFGH==1) THEN CUST.ABCD.EFGH =... (7 Replies)
Discussion started by: swapnil.nawale
7 Replies

10. Shell Programming and Scripting

Extracting from second word

Hi all, I need to extract the Particular string from the whole word,the input file is : 123,345,aaaa,555,....,.... I need all the record from 345 so i need to eliminate the first record. Output: 345,aaa,5555,....,.....,..... Thanks in advance. (3 Replies)
Discussion started by: ithirak17
3 Replies

Featured Tech Videos