Delete a pattern including any whitespace before it and after it


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Delete a pattern including any whitespace before it and after it
# 1  
Old 01-01-2020
Delete a pattern including any whitespace before it and after it

Hello.

Code:
A_LIGNE="cnezJ,V ,FMZ  fd,Mok CODKJ,F  SOME_WORD   fcnQ, VMQKV Q"
A_PATTERN="SOME_WORD"

Code:
sed 's/'$A_PATTERN'//g' <<< "$A_LINE"

will remove 'SOME_WORD' and give :
Code:
"cnezJ,V ,FMZ  fd,Mok CODKJ,F    fcnQ, VMQKV Q"

A_PATTERN="SOME_WORD[[:space:]]"
Code:
sed 's/'$A_PATTERN'//g' <<< "$A_LINE"

will remove 'SOME_WORD' and the space after it and give :
Code:
"cnezJ,V ,FMZ  fd,Mok CODKJ,F  fcnQ, VMQKV Q"

Now the question

--------------------------

My problem is that the pattern is at the beginning of the line

I would like to remove the whitespace before and after it.
I tried this :
A_PATTERN="[[:space:]]SOME_WORD[[:space:]]"
Code:
sed 's/'$A_PATTERN'//g' <<< "$A_LINE"

should remove 'SOME_WORD' and the space before and after it.
But it does not work.
The pattern is at the beginning of the line and have white space after it.
But may or may not have white space before it.

ps (I made a try on a file where the pattern start at the beginning of the line without white space before it.)

Any help is welcome
# 2  
Old 01-01-2020
Hi
Code:
A_PATTERN="SOME_WORD"
sed 's/\s*'$A_PATTERN'\s*/ /' <<<"$A_LIGNE"

Note replaces with a space
But if you need to remove the space before and after, then you do not need to substitute it with a space

--- Post updated at 23:59 ---

One more variant
Code:
sed 's/\(\s\)*'$A_PATTERN'\s*/\1/' <<<"$A_LIGNE"


Last edited by nezabudka; 01-01-2020 at 03:27 PM..
# 3  
Old 01-01-2020
No sed required:
Code:
TMP1="${A_LIGNE%$A_PATTERN*}" 
TMP2="${A_LIGNE#*$A_PATTERN}" 
echo "${TMP1%${TMP1##*[^ ]}} ${TMP2#${TMP2%%[^ ]*}}"
cnezJ,V ,FMZ  fd,Mok CODKJ,F fcnQ, VMQKV Q

Should work for patterns at the begin-of-line, in the middle, or at end-of-line.
This User Gave Thanks to RudiC For This Post:
# 4  
Old 01-02-2020
Quote:
Code:
A_LIGNE="cnezJ,V ,FMZ  fd,Mok CODKJ,F  SOME_WORD   fcnQ, VMQKV Q"

Code:
echo ${A_LIGNE/${A_PATTERN}}

Output:
Code:
cnezJ,V ,FMZ fd,Mok CODKJ,F fcnQ, VMQKV Q

A mock-up of your specific question:
Code:
A_LIGNE=" cnezJ V ,FMZ  fd,Mok CODKJ,F  SOME_WORD   fcnQ, VMQKV Q"
A_PATTERN="cnezJ"
echo ${A_LIGNE/${A_PATTERN}}

Output:
Code:
V ,FMZ fd,Mok CODKJ,F SOME_WORD fcnQ, VMQKV Q

# 5  
Old 01-02-2020
@Aia: the problem with your approach is that
- either ALL multiple spaces in the string are reduced to a single one (if unquoted)
- or the spaces around A_PATTERN are preserved (if quoted), which was not what the OP requested.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to extract/delete lines including 0%?

Hello, I have a data file consisting of many lines and my target is to delete all lines containing 0% After some sed processes, I convert it to shown below format: Sample webpage logfile: Expected output is: sed command is not working for below methods: sed -n '/0%/p' logfile > output... (1 Reply)
Discussion started by: baris35
1 Replies

2. Shell Programming and Scripting

How to use ls with pattern and including path?

Hello to all, Maybe someone could help me, my question is: How can a filter the print of command ls for the files with names of the form "abc*.txt" including the path? I've done this: If I move with command cd to /My/Path/Is/This/ and send this command: ls -lst abc*.txt -i... (37 Replies)
Discussion started by: Ophiuchus
37 Replies

3. Shell Programming and Scripting

Including EOL in egrep pattern for multiple lines

Hi all I need your help to get a high-performance solution. I am working on a extensive script to automate file restores using the bprestore tool on a Solaris 5.10 server (bash 3.00). I will only paste the needed parts of the script to avoid any confusion. To use the script the user has to... (2 Replies)
Discussion started by: Anonym
2 Replies

4. UNIX for Dummies Questions & Answers

delete trailing whitespace from end of each line in column 1 only

Hi All. How can I convert this: ABC_1_1 ABC_1_2 ABC_1_3 into this: ABC_1 1 ABC_1 2 ABC_1 3 I tried this command but it is not working: awk '{sub(/+$/,"\t", $1)}{print}' Any suggestions on how to fix this? Thank you :wall: Please use code tags when posting data and... (3 Replies)
Discussion started by: danieladna
3 Replies

5. Shell Programming and Scripting

remove contents including the tag if pattern matches

Hi all, Can anyone help me on this. I have several WP sites that are affected by sql injections. But the contents are different as follows western union india belgaum western union india bolegaon western union india barhaj western union india budhana western union india belda western... (6 Replies)
Discussion started by: sanjuabraham
6 Replies

6. Shell Programming and Scripting

Search for Pattern and Print including Lines in between

Gurus, I have a big file that needs to be sorted out and I cant figure out what to do. The file name is as below: Name: xxxx yyyy nnnn Description: dfffgs sdgsgsf hsfhhs afgghhjdgj fjklllll gsfhfh Updated: jafgadsgg gsg Corrected: date today The file consists of line like these. ... (13 Replies)
Discussion started by: The One
13 Replies

7. Shell Programming and Scripting

How to match (whitespace digits whitespace) sequence?

Hi Following is an example line. echo "192.22.22.22 \"33dffwef\" 200 300 dsdsd" | sed "s:\(\ *\ \):\1:" I want it's output to be 200 However this is not the case. Can you tell me how to do it? I don't want to use AWK for this. Secondly, how can i fetch just 300? Should I use "\2"... (3 Replies)
Discussion started by: shahanali
3 Replies

8. Shell Programming and Scripting

sed: Find start of pattern and extract text to end of line, including the pattern

This is my first post, please be nice. I have tried to google and read different tutorials. The task at hand is: Input file input.txt (example) abc123defhij-E-1234jslo 456ujs-W-abXjklp From this file the task is to grep the -E- and -W- strings that are unique and write a new file... (5 Replies)
Discussion started by: TestTomas
5 Replies

9. Shell Programming and Scripting

Delete whitespace

Hi, I have been trying to remove whitespace from a file using sed. Here is an example of what im trying to do: www1 = www1 www2 = www2 www3 = www3 and all the way to 300 and i want it to look like: www1=www1 www2-www2 www3=www3 again upto 300 Any help... (12 Replies)
Discussion started by: truck7758
12 Replies

10. Shell Programming and Scripting

Find files including subdirectory and Delete

Hello Experts, I m newbie. Could u pls help me to write script on Sun solaris- I have backup directory "/var/opt/backup/" where files are backed up in different directory "backup1" "backup2" "backup3". I want to write a shell script which i will put in crontab and daily midnight it will... (1 Reply)
Discussion started by: thepurple
1 Replies
Login or Register to Ask a Question