Sponsored Content
Top Forums Shell Programming and Scripting Find/replace alpha characters in string Post 302932627 by Don Cragun on Friday 23rd of January 2015 12:57:31 AM
Old 01-23-2015
The sed command Klashxx gave you will work on some systems, but the -r option is not portable (and you haven't said what OS you're using).

For systems with a POSIX-conforming sed utility, the following sed script seems to do what you want:
Code:
strings='563MS333_101_afp_400-100_screening leading and trailing _
563MS333_104-525_rjk_525_screening leading and trailing _
563MS333_110_dlj_500-100_w24 leading and trailing _
563MS333_888-100_mmm_424_screening leading and trailing _
563MS333_101afp_400-100_screening trailing _
563MS333_104-525rjk_525_screening trailing _
563MS333_110dlj_500-100_w24 trailing _
563MS333_888-100mmm424_screening trailing _
563MS333_101afp400-100_screening no _
563MS333_104-525rjk525_screening no _
563MS333_110dlj500-100_w24 no _
563MS333_888-100mmm424_screening no _
563MS333_101_afp400-100_screening leading _
563MS333_104-525_rjk525_screening leading _
563MS333_110_dlj500-100_w24 leading _
563MS333_888-100_mmm424_screening leading _
563MS333_101_fp_400-100_screening No Change too few alphas
563MS333_101fp_400-100_screening No Change too few alphas
563MS333_101_fp400-100_screening No Change too few alphas
563MS333_101fp400-100_screening No Change too few alphas
563MS333_101_aafp_400-100_screening No Change too many alphas
563MS333_101aafp_400-100_screening No Change too many alphas
563MS333_101_aafp400-100_screening No Change too many alphas
563MS333_101aaafp400-100_screening No Change too many alphas'
printf '%s\n' "$strings" | while read line
do	printf 'Input line: %s\n' "$line"
	nline="$(printf '%s\n' "$line" |
		sed 's/^\(563MS333[-_[:digit:]]*[[:digit:]]\{3\}_\{0,1\}\)[[:alpha:]]\{3\}\(_\{0,1\}[[:digit:]]\{3\}\)/\1xxx\2/')"
	printf '  New line: %s\n' "$nline"
done

and produces the output:
Code:
Input line: 563MS333_101_afp_400-100_screening leading and trailing _
  New line: 563MS333_101_xxx_400-100_screening leading and trailing _
Input line: 563MS333_104-525_rjk_525_screening leading and trailing _
  New line: 563MS333_104-525_xxx_525_screening leading and trailing _
Input line: 563MS333_110_dlj_500-100_w24 leading and trailing _
  New line: 563MS333_110_xxx_500-100_w24 leading and trailing _
Input line: 563MS333_888-100_mmm_424_screening leading and trailing _
  New line: 563MS333_888-100_xxx_424_screening leading and trailing _
Input line: 563MS333_101afp_400-100_screening trailing _
  New line: 563MS333_101xxx_400-100_screening trailing _
Input line: 563MS333_104-525rjk_525_screening trailing _
  New line: 563MS333_104-525xxx_525_screening trailing _
Input line: 563MS333_110dlj_500-100_w24 trailing _
  New line: 563MS333_110xxx_500-100_w24 trailing _
Input line: 563MS333_888-100mmm424_screening trailing _
  New line: 563MS333_888-100xxx424_screening trailing _
Input line: 563MS333_101afp400-100_screening no _
  New line: 563MS333_101xxx400-100_screening no _
Input line: 563MS333_104-525rjk525_screening no _
  New line: 563MS333_104-525xxx525_screening no _
Input line: 563MS333_110dlj500-100_w24 no _
  New line: 563MS333_110xxx500-100_w24 no _
Input line: 563MS333_888-100mmm424_screening no _
  New line: 563MS333_888-100xxx424_screening no _
Input line: 563MS333_101_afp400-100_screening leading _
  New line: 563MS333_101_xxx400-100_screening leading _
Input line: 563MS333_104-525_rjk525_screening leading _
  New line: 563MS333_104-525_xxx525_screening leading _
Input line: 563MS333_110_dlj500-100_w24 leading _
  New line: 563MS333_110_xxx500-100_w24 leading _
Input line: 563MS333_888-100_mmm424_screening leading _
  New line: 563MS333_888-100_xxx424_screening leading _
Input line: 563MS333_101_fp_400-100_screening No Change too few alphas
  New line: 563MS333_101_fp_400-100_screening No Change too few alphas
Input line: 563MS333_101fp_400-100_screening No Change too few alphas
  New line: 563MS333_101fp_400-100_screening No Change too few alphas
Input line: 563MS333_101_fp400-100_screening No Change too few alphas
  New line: 563MS333_101_fp400-100_screening No Change too few alphas
Input line: 563MS333_101fp400-100_screening No Change too few alphas
  New line: 563MS333_101fp400-100_screening No Change too few alphas
Input line: 563MS333_101_aafp_400-100_screening No Change too many alphas
  New line: 563MS333_101_aafp_400-100_screening No Change too many alphas
Input line: 563MS333_101aafp_400-100_screening No Change too many alphas
  New line: 563MS333_101aafp_400-100_screening No Change too many alphas
Input line: 563MS333_101_aafp400-100_screening No Change too many alphas
  New line: 563MS333_101_aafp400-100_screening No Change too many alphas
Input line: 563MS333_101aaafp400-100_screening No Change too many alphas
  New line: 563MS333_101aaafp400-100_screening No Change too many alphas

 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Replace string and delete extra characters

Hopefully someone can help out here. This is probably fairly basic, but I've searched and tried several variations of the solutions presented in these forums, so I'll go ahead and ask. How can I locate a string in a file, delete the characters after the string and then replace the string with a... (2 Replies)
Discussion started by: slaubhan
2 Replies

2. Shell Programming and Scripting

Replace characters in a string using their ascii value

Hi All, In the HP Unix that i'm using when i initialise a string as Stalled="'30¬G'" Stalled=$Stalled" '30¬C'", it is taking the character ¬ as a comma. I need to grep for 30¬G 30¬C in a file and take its count. But since this character ¬ is not being understood, the count returns a zero. The... (2 Replies)
Discussion started by: roops
2 Replies

3. UNIX for Dummies Questions & Answers

Help with find and replace w/string containing special characters

Can I get some help on this please, I have looked at the many post with similar questions and have tried the solutions and they are not working for my scenario which is: I have a text file (myfile) that contains b_log=$g_log/FILENAME.log echo "Begin processing file FILENAME " >> $b_log ... (4 Replies)
Discussion started by: CAGIRL
4 Replies

4. Shell Programming and Scripting

search and replace characters in one string

I have lines like: Dog Cat House Mouse Dog Cat House Mouse Dog Cat House Mouse Dog Cat House Mouse I'd like to replace characters only in $3. H -> Z s -> W e -> x Resulting in something like (where $1, $2, and $4 are not changed): Dog Cat ZouWx Mouse Dog Cat ZouWx Mouse... (3 Replies)
Discussion started by: dcfargo
3 Replies

5. Shell Programming and Scripting

find replace a pattern and following characters in a word

Suppose that I have a string "one:#red two:#yellow three:#gr'een four:#blu^e" and I want to replace the pattern :# and the following characters in the word with nothing. The output string should look "one two three four" How can I do this with sed. Some points to consider (a) the last word in... (1 Reply)
Discussion started by: superuser84
1 Replies

6. Shell Programming and Scripting

Find number of characters in a column and replace

Hi all, I want to count total no. of characters in a column. and if no. of charaters are more than 3 then it must replace it by splitted string. ie, it must place a space after 3 characters. Ex: 21 435g asd3dd jklfjwe wer column number 3 has 4 alphanumeric character, so it must be splitted... (3 Replies)
Discussion started by: CAch
3 Replies

7. Shell Programming and Scripting

Replace characters in string with awk gsub

Hi I have a source file that looks like a,b,c,d,e,f,g,h,t,DISTI(USD),MSRP(USD),DIST(EUR),MSRP(EUR),EMEA-DISTI(USD),EMEA-MSRP(USD),GLOBAl-DISTI(USD),GLOBAL-MSRP(USD),DISTI(GBP), MSRP(GBP) I want to basically change MSRP(USD) to MSRP,USD and DIST(EUR) to DIST,EUR and likewise for all i'm using... (3 Replies)
Discussion started by: r_t_1601
3 Replies

8. Shell Programming and Scripting

Find and replace with 0 for characters in a specific position

Need command for position based replace: I need a command to replace with 0 for characters in the positions 11 to 20 to all the lines starts with 6 in a file. For example the file ABC.txt has: abcdefghijklmnopqrstuvwxyz 6abcdefghijklmnopqrstuvwxyz abcdefghijklmnopqrstuvwxyz... (4 Replies)
Discussion started by: thangabalu
4 Replies

9. Shell Programming and Scripting

Help to replace the string with special characters

{"name":"alR_pl-ENVIRONMENT_192_168_211_123_sDK_PROVISION_7","description":"aLR_pl-ENVIRONMENT_192_168_211_123_sDK_PROVISION_7","json_class":"Chef::Role","default_attributes":{},"override_attributes":{"yoapp":{"jboss":"5.1.0","port":"2243","warname":"soap","datacenter":"alR","ip":"192.168.211.123","... (3 Replies)
Discussion started by: nikhil jain
3 Replies

10. Shell Programming and Scripting

sed replace nth characters with string

Hi, I hope you can help me out please? I need to replace from character 8-16 with AAAAAAAA and the rest should stay the same after character 16 gtwrhtrd11111111rjytwyejtyjejetjyetgeaEHT wrehrhw22222222hytekutkyukrylryilruilrGEQTH hrwjyety33333333gtrhwrjrgkreglqeriugn;RUGNEURGU ... (4 Replies)
Discussion started by: stinkefisch
4 Replies
All times are GMT -4. The time now is 09:28 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy