[All variants] remove first pair of parentheses


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting [All variants] remove first pair of parentheses
# 1  
Old 02-08-2015
[All variants] remove first pair of parentheses

How to remove first pair of parentheses and content in them from the beginning of the line?

Here's the list:
Code:
(ok)-test
(ok)-test-(ing)
(some)-test-(ing)-test
test-(ing)

Desired result:
Code:
test
test-(ing)
test-(ing)-test
test-(ing)

Here's what I already tried with GNU sed:
Code:
sed -e 's/^(.*)//'
sed -r 's/^\(.*\)//'

Solution preferences order: sh/bash, grep, sed, awk, perl, other

Moderator's Comments:
Mod Comment Please use code tags for data samples (not quote tags)

Last edited by Scrutinizer; 02-08-2015 at 11:56 PM.. Reason: Changed quote tags to code tags
# 2  
Old 02-08-2015
How about:

Code:
sed 's/^[(][^)]*[)]-//'

This User Gave Thanks to Chubler_XL For This Post:
# 3  
Old 02-08-2015
maybe this will work too?

Code:
awk '/^\(/ {sub(/\(/,"");sub(/\)/,"")}1'  infile

This User Gave Thanks to senhia83 For This Post:
# 4  
Old 02-08-2015
Quote:
Originally Posted by senhia83
maybe this will work too?
Yes, it does remove parentheses, but not the content in them.
Next time I should ask precisely.
# 5  
Old 02-08-2015
How about:
Code:
$ cat usertail 
TMP=~/tmp.$$
REM="ok some"
echo "(ok)-test
(ok)-test-(ing)
(some)-test-(ing)-test
test-(ing)" > $TMP

for rem in $REM
do 	sed s,\($rem\),,g  -i $TMP
done
sed s,^\-,,g $TMP

$ ./usertail
test
test-(ing)
test-(ing)-test
test-(ing)

Hope this helps
This User Gave Thanks to sea For This Post:
# 6  
Old 02-09-2015
Quote:
Originally Posted by useretail
Yes, it does remove parentheses, but not the content in them.
Next time I should ask precisely.
Perhaps, but your output file specification was precise. I think Chubler_XL got it right, no?

Another way of writing it would be:
Code:
sed 's/^([^)]*)-//' file

Note that the thing different from your original attempt is that the . is replaced by [^)] (to force lazy matching, rather than the standard greedy matching) and the - is specified, so it will be removed too.

Last edited by Scrutinizer; 02-09-2015 at 12:23 AM..
This User Gave Thanks to Scrutinizer For This Post:
# 7  
Old 02-09-2015
Hi.

Using bash operator "=~" with extended regular expressions:
Code:
#!/usr/bin/env bash

# @(#) s2	Demonstrate extended regular expressions, bash =~.

# Utility functions: print-as-echo, print-line-with-visual-space, debug.
# export PATH="/usr/local/bin:/usr/bin:/bin"
LC_ALL=C ; LANG=C ; export LC_ALL LANG
pe() { for _i;do printf "%s" "$_i";done; printf "\n"; }
pl() { pe;pe "-----" ;pe "$*"; }
db() { ( printf " db, ";for _i;do printf "%s" "$_i";done;printf "\n" ) >&2 ; }
db() { : ; }
C=$HOME/bin/context && [ -f $C ] && $C

FILE=${1-data1}

pl " Input data file $FILE:"
cat $FILE

pl " Expected results:"
cat expected-output.txt

pl " Results:"
PATTERN='(^[(][^)]*[)]-)(.*)'
while read line
do
  db ' Working on line ['"$line"']'
  if [[ $line =~ $PATTERN ]]
  then
    printf "%s\n" "${BASH_REMATCH[2]}"
  else
    printf "%s\n" $line
  fi
done <$FILE

exit 0

producing:
Code:
$ ./s2

Environment: LC_ALL = C, LANG = C
(Versions displayed with local utility "version")
OS, ker|rel, machine: Linux, 2.6.26-2-amd64, x86_64
Distribution        : Debian 5.0.8 (lenny, workstation) 
bash GNU bash 3.2.39

-----
 Input data file data1:
(ok)-test
(ok)-test-(ing)
(some)-test-(ing)-test
test-(ing)

-----
 Expected results:
test
test-(ing)
test-(ing)-test
test-(ing)

-----
 Results:
test
test-(ing)
test-(ing)-test
test-(ing)

Best wishes ... cheers, drl
This User Gave Thanks to drl For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Merge 4 bim files by keeping only the overlapping variants (unique rs values )

Dear community, I am facing a problem and I kindly ask your help: I have 4 different data sets consisted from 3 different types of array. On each file, column 1 is chromosome position, column 2 is SNP id etc... Lets say I have the following (bim) datasets: x2014: 1 rs3094315... (4 Replies)
Discussion started by: fondan
4 Replies

2. Shell Programming and Scripting

[All variants] Change settings

Hi, I have a big settings confg (file attached). There are a few separate tasks that I have to accomplish. All scripting/programming languages are appreciated. 1. I need to parse all values and output to stdout. Sample output (truncated): VALUEA 2017-01-01 Lores ipsum Lorem ipsum dolor sit... (11 Replies)
Discussion started by: useretail
11 Replies

3. Answers to Frequently Asked Questions

Add string to parentheses

Suppose I have this code : int main () { int i = NULL; /* incorrect */ return 0; } and I want to put the word between the two parentheses like this : int main (void) { int i = NULL; /* incorrect */ return 0; } which command is used to do it in Linux ? (2 Replies)
Discussion started by: steve120
2 Replies

4. Shell Programming and Scripting

Help with extracting data within parentheses

This is my input file: a|b|c(ef)|g|h(km)|p My output file should look like: a|b|ef|g|km|p That is, pipe is the delimiter. The data within pipe must be displayed as it is but if it encounters any data within parentheses, then only the data within parentheses has to be displayed ( the data... (2 Replies)
Discussion started by: ksatish89
2 Replies

5. Shell Programming and Scripting

Remove very first pair of duplicate words

I have file which is almost look like below MMIT MMIT ... (2 Replies)
Discussion started by: manas_ranjan
2 Replies

6. Shell Programming and Scripting

When does an if statement need parentheses

I was looking at a script in my little book on bash and saw that one of the if statements had parentheses instead of brackets for the condition. I've been trying to find in my book where it talks about parentheses (because the examples on the if statement in an earlier chapter doesn't seem to... (3 Replies)
Discussion started by: Straitsfan
3 Replies

7. Shell Programming and Scripting

CREATING A SYLLABLE CONCORDANCE WITH POSITIONAL VARIANTS

Hello, Some time back I had posted a request for a syllable concordance in which if a syllable was provided in a file, the program would extract a word from a file entitled "Corpus" matching that syllable. The program was The following script was provided which did the job and for which I am... (3 Replies)
Discussion started by: gimley
3 Replies

8. UNIX for Dummies Questions & Answers

brackets vs parentheses - single and double

hi, unix gurus. i am wondering if someone can give me a clear explanation of the differneces between parentheses and brackets, both single and double. i have heard that double parentheses (( are used for numerical expressions and that single brackets [ are used for strings. but i see... (1 Reply)
Discussion started by: ankimo
1 Replies

9. Shell Programming and Scripting

Replace text in parentheses

Hi I would like to replace a comma in parentheses to a semicolon for example. Other commas outside () stay unchanged. How can I do this? aaaa,bbb,ccc,ddd(eee,fff,ggg),hhh,iii to aaaa,bbb,ccc,ddd(eee;fff;ggg),hhh,iii Thanks (5 Replies)
Discussion started by: lalelle
5 Replies
Login or Register to Ask a Question