Visit Our UNIX and Linux User Community


Conditional search and delete using SED / Shell script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Conditional search and delete using SED / Shell script
# 1  
Old 12-20-2011
Conditional search and delete using SED / Shell script

Hi,
I want to perform a conditional search and remove my search string.

Input string: "abcdaabcadgfaarstab"
Character to search: "a"
Condition: Remove all "a" in the input string except if it is "aa"
Output string: "bcdaabcdgfaarstb"

Can you please help me in this?
# 2  
Old 12-20-2011
Code:
echo "abcdaabcadgfaarstaba" | perl -e '$x=<>; $x=~s/([^a])a([^a])/$1$2/g; $x=~s/^a([^a])|([^a])a$/$1$2/g; print "$x"'

This User Gave Thanks to balajesuri For This Post:
# 3  
Old 12-20-2011
But is there a solution without using perl. Something using sed, awk as we do not have perl in target.
# 4  
Old 12-20-2011
Try...
Code:
echo "abcdaabcadgfaarstab" | sed 's/\(^\|[^a]\)a\([^a]\|$\)/\1\2/g'

This User Gave Thanks to Ygor For This Post:
# 5  
Old 12-20-2011
@Ygor: I too tried that initially. But it wouldn't work if there was an 'a' at the end, as in "abcdaabcadgfaarstaba". Surprising, but it isn't considering "a" in the end even though the regex has a $.. So, I wrote one to check and substitute separately.

@dominiclajs: Try this:
Code:
echo "abcdaabcadgfaarstaba" | sed "s:\([^a]\)a\([^a]\):\1\2:g" | sed "s:^a\([^a]\)\|\([^a]\)a$:\1\2:g"


Last edited by balajesuri; 12-20-2011 at 02:40 AM..
# 6  
Old 12-20-2011
Little tricky .. Smilie
Code:
$ echo "abcdaabcadgfaarstab" | sed 's,aa,?,g;s,a,,g;s,?,aa,g'


Previous Thread | Next Thread
Test Your Knowledge in Computers #295
Difficulty: Easy
Ubuntu releases updated versions predictably every 3 months.
True or False?

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Conditional delete -- New glitch

Hi Please dont consider this as duplicated post.. I am using below pattern to find delete files to bringdown disc size.. however how i can make sure ist going to correct folder and searching for files... while print "echo rm " LastFile correctly print files names for deletion, but when i... (7 Replies)
Discussion started by: onenessboy
7 Replies

2. Shell Programming and Scripting

Conditional delete

Hi Friends, I have somefiles like 20180720_1812.tar.gz 20180720_1912.tar.gz 20180720_2012.tar.gz 20180720_2112.tar.gz 20180721_0012.tar.gz 20180721_0112.tar.gz 20180721_0212.tar.gz 20180721_0312.tar.gz in a directory and so on..these files gets created every 3 hours where as... (28 Replies)
Discussion started by: onenessboy
28 Replies

3. Shell Programming and Scripting

Conditional string search

Hi, I need to search/grep userid and its value based on a condition that the user id is under the heading "*central_test*" in a big file of about 70808MB. The * represents some strings before and after the string "centrla_test". Could anyone please guide. OS is hp-ux. (2 Replies)
Discussion started by: sam_bd
2 Replies

4. Homework & Coursework Questions

sed Multiple Pattern search and delete the line

Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted! 1. The problem statement, all variables and given/known data: I have file which has got the following content sam 123 LD 41 sam 234 kp sam LD 41 kam pu sam LD 61 Now... (1 Reply)
Discussion started by: muchyog
1 Replies

5. Shell Programming and Scripting

Conditional Search/Replace

I'm looking for an awk or (preferably) sed solution to search a pipe delimited file for any occurrence of an email address that does not include a designed domain, and replace the email address with a blank. E.g. hello|smith@designateddomain.com|jones@anotherdomain.edu|1234| turns into: ... (2 Replies)
Discussion started by: tiggyboo
2 Replies

6. Shell Programming and Scripting

sed search pattern and delete lines

Hello, i have a question. My problem is that i have a file like: TEST JOHN ADAM MICHAEL SEBASTIAN ANDY i want find for MICHAEL and want delete lines like this: TEST (4 Replies)
Discussion started by: eightball
4 Replies

7. Shell Programming and Scripting

search and conditional execution

Hi, I have a list of files with different filenames like nam0001.txt,pan0001.txt etc coming in /data/inbox from various places. I needed a shell script which would:- 1)searches for a word from the filenames (coming in inbox directory) provided in input parameter. 2)if found, put in... (2 Replies)
Discussion started by: Saiesh
2 Replies

8. Shell Programming and Scripting

Help on shell script conditional execution when CPU Idle > 60%

I need a shell script that will monitor a few conditions and not execute until the these conditions are met. The problem I'm having is that I can not perform a database snapshot (backup) of a sybaseIQ database unless the CPU Status Idle % is above 60% or the snapshot (backup) fails. If... (2 Replies)
Discussion started by: pancona99
2 Replies

9. Shell Programming and Scripting

Help with search and delete/add/modify script

Gurus, I need to run a script on multiple XML files in different directories and do the following (the output can be redirected to create a new file) 1. Search a pattern like "abc.mno.xyz" in an XML file, once detected, the script should delete one line above and 3 lines below (including the... (2 Replies)
Discussion started by: inditopgun
2 Replies

10. Shell Programming and Scripting

shell script to search a string and delete the content

Hi, I've a shell script e.g. #!/bin/bash echo "Enter the next hop id" read nhid echo "enter the IP address" read IP echo "enter the interface name" read name echo "enter the enable/disable state" read state exit 0 now from this script i want to search strings in another (.cam) ... (6 Replies)
Discussion started by: vic_mnnit
6 Replies

Featured Tech Videos