Visit Our UNIX and Linux User Community


how to remove a substring


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting how to remove a substring
# 1  
Old 07-21-2009
Question how to remove a substring

I want to modify a string in shell script (/bin/sh). Could you please help me.

I have a variable which can store the values like below.
v10.5.2.1p001C_TN1
or v10.5.2.1p002_TN1

I have to search first whether p' character exists into the string which is stored in a variable.. If yes, then I have to remove the p001C and p002' correspondingly. I mean I have to remove the content from p and before _TN

Thanks in advance.
# 2  
Old 07-21-2009
does this suits you?

Code:
-bash-3.2$ cat test
v10.5.2.1p001C_TN1
v10.5.2.1p002_TN1
v10.5.2.1_TN1
v10.5.2.2_TN1
-bash-3.2$ awk -F[p_] '{print $1"_"$NF}' test
v10.5.2.1_TN1
v10.5.2.1_TN1
v10.5.2.1_TN1
v10.5.2.2_TN1
-bash-3.2$

# 3  
Old 07-22-2009
Try this:
Code:
sed 's/p..*_/_/' test

# 4  
Old 07-22-2009
Awesome! Thank you.

Could you please do another favor. By using your command, i am trying to store the value into a variable, but it seems it is not going Could you please see how can i do so.

As per the following code I am reading file .activate_ver and replace the string stored into it. But myAppWebVersion is still empty.


bash-4.0$ more .activate_ver
v6.5.2.1p001C_MR1

bash-4.0$ more test.sh
#!/bin/sh
cat ./.activate_ver | awk -F[p_] '{print $1"_"$NF}' | sort > myAppWebVersion
echo "version is $myAppWebVersion"

bash-4.0$ test.sh
version is
bash-4.0$



IF YOU CAN COULD YOU PLEASE ALSO EXPLAIN ME awk -F[p_] '{print $1"_"$NF}' COMMAND.
# 5  
Old 07-22-2009
you can do it like this. you can try edidataguy's code also Smilie

Code:
#!/bin/sh
myAppWebVersion=`cat ./.activate_ver | awk -F[p_] '{print $1"_"$NF}' | sort`
echo "version is $myAppWebVersion"

the code "awk -F[p_] '{print $1"_"$NF}'" which means that there are two delimiters which is "p" and "_". Therefore the string is divided into three

Code:
$1           $2      $3/$NF - Last column
v10.5.2.1 p 001C _ TN1

i just printed $1, underscore (_), and $NF

if there is no "p" then it would be the same

Code:
$1           $2/$NF - Last column
v10.5.2.1 _ TN1

# 6  
Old 07-22-2009
This was my first posting on this forum.

Thanks a lot to both of you. You are doing a great service to the development community, especially when economy is bad and work pressure is too much.

Previous Thread | Next Thread
Test Your Knowledge in Computers #467
Difficulty: Easy
In computer networking, telecommunication and information theory, broadcasting is a method of transferring a message to all recipients using reverse polish notation.
True or False?

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Remove lines matching a substring in a specific column

Dear group, I have following input text file: Brit 2016 11 18 12 00 10 1.485,00 EUR Brit 2016 11 18 12 00 10 142,64 EUR Brit 2016 11 18 12 00 10 19,80 EUR Brit 2016 11 18 12 00 10 545,00 EUR Brit 2016 11 18 12 00 10 6.450,00 EUR... (3 Replies)
Discussion started by: gfhsd
3 Replies

2. UNIX for Dummies Questions & Answers

Remove when substring meets condition

Hi Masters, I need to remove lines when date format is below certain date My file input 20140906|ALASKA|USASEL|TARPUNG|2014-03-01|82176614040|20|1 20140906|ALASKA|USATENG|CRUIEX|2014-08-01|81267079997|5|0 20140906|ALASKA|USASEL|CRUIEMBANG|2013-10-01|82280779814|9|0... (4 Replies)
Discussion started by: radius
4 Replies

3. Shell Programming and Scripting

Using sed, awk or perl to remove substring of all lines except the first

Greetings All, I would like to find all occurences of a pattern and delete a substring from the all matching lines EXCEPT the first. For example: 1234::group:user1,user2,user3,blah1,blah2,blah3 2222::othergroup:user9,user8 4444::othergroup2:user3,blah,blah,user1 1234::group3:user5,user1 ... (11 Replies)
Discussion started by: jacksolm
11 Replies

4. Shell Programming and Scripting

Remove a substring from string

Good morning friends, how can i remove a string with linux scripting from a file? In specific i want to remove from a file all the tweet names and links eg @aerta and links such as http://dst.co/pIiu3i9c Thanx!!! (4 Replies)
Discussion started by: paladinaeon
4 Replies

5. Shell Programming and Scripting

how to remove this substring?

Hello, I need to remove a substring from a string in a ksh script, the string is like this: LOCATION=+DATADG1/CMSPRD1/datafile/system.235.456721 or LOCATION=/u03/oradata/CMSPRD/SYSTEM.dbf I need to strip the last file name from that path, and get: +DATADG1/CMSPRD1/datafile or,... (8 Replies)
Discussion started by: seafan
8 Replies

6. UNIX for Dummies Questions & Answers

Getting Substring

Hi, I hav a string lets say aa.txt:bb:txt length of the string can vary.. I have to keep the token inside a array and the delimiter is : plz send me the code (2 Replies)
Discussion started by: Deekay.p
2 Replies

7. Shell Programming and Scripting

substring

I have a string '<Hi>abc</Hi>" How to print "abc" (6 Replies)
Discussion started by: sandy1028
6 Replies

8. UNIX for Dummies Questions & Answers

Need help with substring

I need to check the occurrence of one string within another. code ******************** if ;then do something done ******************** Thanks (7 Replies)
Discussion started by: w020637
7 Replies

9. Shell Programming and Scripting

Substring HELP!

Hi, I am trying to do something which I thought was very simple but still being a beginner, has proved not to be. Input: val1 val2 val3 val4 val5 val6 . . . etc Desired Output: Every row in which value of val6 is a number starting with 0.0 or contains a capital E. The input... (2 Replies)
Discussion started by: awknerd
2 Replies

10. UNIX for Dummies Questions & Answers

substring

Hi, I have a value of a filepath in a variable DATAFILE with value as "customtop/gpsore37/gepspo/1.0/bin/ashoka.csv ". Now i want the value of last 4 charcters in to another variable. That is EXTENSION = .csv How can i do this in Shell scripting Thanks in advance Alla Kishore (8 Replies)
Discussion started by: alla.kishore
8 Replies

Featured Tech Videos