Remove string between number and character

 
Thread Tools Search this Thread
Top Forums UNIX for Beginners Questions & Answers Remove string between number and character
# 1  
Old 06-09-2017
Remove string between number and character

hello !

I have to remove string between a number and set of characters. For example,

Code:
35818    -stress  - -       -stress  - - - - - - DB-3754 
44412  caul kid notify      DB-3747 
54432  roberto -, notify   DB-3725 
55522  aws _ _int _ _classified 2_a _a 2_m _m 2_classified 2_search _search 2_shoing _shoing 2_windows _windows shp_ shp_ DB-3755 DB-3721 DB-3361 DB-3327 DB-3688 DB-3750 DB-3677 DB-3728 DB-3730

From the above, what I need is,

Code:
35818    DB-3754 
44412    DB-3747 
54432   DB-3725 
55522    DB-3755 DB-3721 DB-3361 DB-3327 DB-3688 DB-3750 DB-3677 DB-3728 DB-3730

From the examples that I saw in here I know how to remove string using sed as below

Code:
sed -s '/[0-9]{6}/,/DB-/d'

but that is not working.

Any help is truly appreciated.

Also, one more thing if I may, instead of one to many, how do I convert it to one to one i.e., how do i make this -

Code:
55522    DB-3755 DB-3721 DB-3361 DB-3327 DB-3688

to

Code:
55522    DB-3755 
55522   DB-3721 
55522   DB-3361 
55522   DB-3327 
55522   DB-3688



Moderator's Comments:
Mod Comment Please use CODE tags as required by forum rules!

Last edited by RudiC; 06-09-2017 at 06:14 AM.. Reason: Added CODE tags.
# 2  
Old 06-09-2017
Hello ManoharMa,

kindly use code tags as per forum rules for your commands/codes/Input_file into your posts.
Could you please try following and let me know if this helps.
Code:
awk '{sub(/[a-zA-Z][^DB]*/,"");sub("-DB","DB")} 1'   Input_file

EDIT: Above command will not change last DB line with multiple words, so if you want to have all DB lines into single column then following may help you too.
Code:
awk '{sub(/[a-zA-Z][^DB]*/,"");sub("-DB","DB");num=split($0, array," ");for(i=2;i<=num;i++){print array[1],array[i]}}'    Input_file

Thanks,
R. Singh

Last edited by RavinderSingh13; 06-09-2017 at 06:26 AM.. Reason: Added improved solution now.
This User Gave Thanks to RavinderSingh13 For This Post:
# 3  
Old 06-09-2017
How about
Code:
awk '{C = NF; while ($C ~ /^DB-/) print $1, $(C--)}' file
35818 DB-3754
44412 DB-3747
54432 DB-3725
55522 DB-3730
55522 DB-3728
55522 DB-3677
55522 DB-3750
55522 DB-3688
55522 DB-3327
55522 DB-3361
55522 DB-3721
55522 DB-3755

This User Gave Thanks to RudiC For This Post:
# 4  
Old 06-09-2017
RavinderSingh13 and RudiC,

I can't be thankful enough for trying to help me here. Maybe I am not being clear enough.

so, here is what the input -

Code:

Code:
35818    -stress  - -       -stress  - - - - - - DB-3754 
44412  caul kid notify      DB-3747


here is the expected output -

Code:

Code:
35818  DB-3754 
44412  DB-3747




Code:
Code:
awk '{sub(/[a-zA-Z][^DB]*/,"");sub("-DB","DB")} 1'

gave me,

Code:
35818
44412

and all the related DB stuff is lost.


Moderator's Comments:
Mod Comment Seriously! Please use CODE tags as required by forum rules!

Last edited by RudiC; 06-09-2017 at 07:15 AM.. Reason: Added CODE tags AGAIN.
# 5  
Old 06-09-2017
What in the result of the proposal in post#3 are you unhappy with?
Code:
awk '{C = NF; while ($C ~ /^DB-/) print $1, $(C--)}' file
35818 DB-3754
44412 DB-3747

This User Gave Thanks to RudiC For This Post:
# 6  
Old 06-09-2017
RudiC,

I am not a regular unix user and as and when I find interesting issues that my wife finds me to help her with, I try and fail and come and ask you folks.

You guys are keeping a very nice show out here in trying to help us folks.

Truly grateful for the help extended. !!!!
This User Gave Thanks to ManoharMa For This Post:
# 7  
Old 06-09-2017
A bit more robust is to have the min and max. And a for loop seems more clear.
Code:
awk '{for (C=NF; C && $C~/^DB-/; C--) print $1, $C}' file

And there is low effort to check each field and print in the correct order
Code:
awk '{for (C=2; C<=NF; C++) if ($C~/^DB-/) print $1, $C}' file

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

gawk to remove last character in a line or string

I am outputting a line like this print $2 "/" $4The last character though is a ":" and I want to remove it. Is there any neat way to remove it? Or am I forced to do something like this: print $2 "/" substr($4, 1, length($4) - 1)Thanks. (6 Replies)
Discussion started by: benalt
6 Replies

2. Shell Programming and Scripting

Bash: How to remove the last character of a string?

In bash, how can one remove the last character of a string? In perl, the chop function would remove the last character. However, I do not know how to do the same job in bash. Many thanks in advance. (12 Replies)
Discussion started by: LessNux
12 Replies

3. Shell Programming and Scripting

remove the first and last character of a string

How can i remove the first and last character of strings like below: "^^^613*" "admt130" "^^^613*" "123456" "adg8484" "DQitYV09dh1C" Means i wanna remove the quotes(""). Please help (17 Replies)
Discussion started by: proactiveaditya
17 Replies

4. Shell Programming and Scripting

remove all occurrences of a character at the beginning of a string

Hi there, i need some help to remove all occurrences of a certain character at the beginning of a string. Example: my string is 00102030 and i want to remove all zeros from beginning of string so the result is 102030 (3 Replies)
Discussion started by: gigagigosu
3 Replies

5. Shell Programming and Scripting

Sed is doing my head in! How do you remove the first character of a string?

Hello! Please bare with me, I'm a total newbie to scripting. Here's the sudo code of what I'm trying to do: Get file name Does file exist? If true get length of file name get network id (this will be the last 3 numbers of the file name) loop x 2 If... (1 Reply)
Discussion started by: KatieV
1 Replies

6. Shell Programming and Scripting

How to remove last character in a string read from file

Hello, The last character is a comma , I have tried the following: sed -e 's/\,$//' filename-to-read however - there are still commas at the end of each line...:confused: (5 Replies)
Discussion started by: learning
5 Replies

7. Shell Programming and Scripting

How to remove the first character on a string in a variable

Hi all, Does anyone know how to code in ksh that will remove the first character in a string variable and replace that variable without the first character? Example: var1=ktest1 will become var1=test1 var2=rtest2 will become var2=test2 Need help please. (10 Replies)
Discussion started by: ryukishin_17
10 Replies

8. Shell Programming and Scripting

shell script to remove the last character(.) of a string

hi I have a list of words in a text file. these words are appended by "." at their end. They look something like this. word1. word2. word3. word4. word5. I need to remove the last character "." from all the words. The output must look something like this. word1 word2 word3... (7 Replies)
Discussion started by: ss3944
7 Replies

9. Shell Programming and Scripting

Remove box like special character from end of string

Hi All, How to remove a box like special character which appears at the end of a string/line/record. I have no clue what this box like special character is. It is transparent square like box. This appears in a .DAT file at the end of header. I'm to compare a value in header with a parameter.... (16 Replies)
Discussion started by: Qwerty123
16 Replies

10. Shell Programming and Scripting

Pulling the first and last character/number from a string.

Let's say I have a word "foobar23" in a file, and I want to pull the first "f" and last "3" character out of the world, how would I accomplish that? # cat file foobar23 I want the output to be: f3 (3 Replies)
Discussion started by: LinuxRacr
3 Replies
Login or Register to Ask a Question