Help with extracting data within parentheses


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Help with extracting data within parentheses
# 1  
Old 05-15-2012
Help with extracting data within parentheses

This is my input file:
Code:
a|b|c(ef)|g|h(km)|p

My output file should look like:
Code:
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 before parentheses has to be excluded).

I have tried with this code. But its not working.

Code:
n=`awk F '|' '{print NF}' input.txt`
for (( i=1; i<=$n; i++ ))
do
  awk -F'|' '{print $($i)}' input.txt | awk -F'(' '{print $2}' > output.txt
done

Kindly help me to solve this problem. Thanks in advance!

Last edited by Franklin52; 05-15-2012 at 08:21 AM.. Reason: Please use code tags
# 2  
Old 05-15-2012
Try:
Code:
sed 's/[^|]*(\([^)]*\))/\1/g' infile

# 3  
Old 05-15-2012
Thanks a lot. Its working.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

extracting data

I have a txt file of the following format >ab_ qwerty >rt_ hfjkil >Ty2 hglashglkasghkf; >P2 aklhfklflkkgfgkfl >ui_ vnllkdskkkffkfkkf >we32 vksksjksj;lslsf'sk's's .... ..... I want to split this big file based on the header (>) (5 Replies)
Discussion started by: Lucky Ali
5 Replies

2. Shell Programming and Scripting

Extracting specific lines of data from a file and related lines of data based on a grep value range?

Hi, I have one file, say file 1, that has data like below where 19900107 is the date, 19900107 12 144 129 0.7380047 19900108 12 168 129 0.3149017 19900109 12 192 129 3.2766666E-02 ... (3 Replies)
Discussion started by: Wynner
3 Replies

3. Shell Programming and Scripting

extracting data from a string

Hi there, I have a bunch of vlan tagged network interfaces that are named as follows e1000g111000 e1000g99001 e1000g3456000 nge2002 where the 'e1000g' and 'nge' parts of the name are the driver, the red and blue bits above define the VLAN and the last digit on the end defines the... (3 Replies)
Discussion started by: rethink
3 Replies

4. UNIX for Dummies Questions & Answers

Help with extracting data and plotting

I have attached a txt file, what I would like to be able to do is: 1. Extract Data from Columns labeled E/N and Ko into a new file 2. Then in the new file I would like to be able to plot E/N on the X axis and Ko on the y axis. 3. Lastly I would like to be able to extract multiple data sets and... (6 Replies)
Discussion started by: gingburg
6 Replies

5. UNIX for Dummies Questions & Answers

Extracting Data Using SED

Given the following text in a file named extract.txt: listenPort:=25 smtpDestination:=2 enableSSL:= I am trying to extract only the value 2 following smtpDestination:= Someone had suggested I use: sed -e "s/^smtpDestination:=\(.*\)$/\1/" extract.txt but this returns: listenPort:=25 2 ... (2 Replies)
Discussion started by: cleanden
2 Replies

6. Shell Programming and Scripting

Extracting strings surrounded by parentheses and seperate by commas

Excuse the terrible title. I have a text file of 1..n lines, each one containing at least one string between parentheses. Within each string, there is one or more strings separated by commas. I need to extract each string, thus: input file: (THIS,THAT) (THE,OTHER) (THING) (OR,MAYBE)... (6 Replies)
Discussion started by: kpfeif
6 Replies

7. Shell Programming and Scripting

extracting data from files..

frnds, I m having prob woth doing some 2-3 task simultaneously... what I want is... I have lots ( lacs ) of files in a dir... I want.. these info from arround 2-3 months files filename convention is - abc20080403sdas.xyz ( for todays files ) I want 1. total no of files for 1 dec... (1 Reply)
Discussion started by: clx
1 Replies

8. Shell Programming and Scripting

extracting integer from data

Hi people, I've encountered a problem. I have a data file like: asd:$123:2 zxc:$456:4 But when I want to extract "$123" and get the number with this command: echo $123 | cut -c 1-10 Im returned with 23 instead of 123. Please help me out, thanks. (4 Replies)
Discussion started by: grotesque
4 Replies

9. Shell Programming and Scripting

Extracting Data From Sendmail

Hello, Like many Unix shops, Our systems send Email alerts whenever things break. I have been tasked with writing a shell script to get the email alerts from Sendmail, extract the Date / Time, From, Subject, and message text from the emails and punch them into a MySQL DB. This will then be... (3 Replies)
Discussion started by: calex
3 Replies

10. Shell Programming and Scripting

Extracting certain data from a sentence

How do I delete text in each line from the first character up to a certain pattern, ie. 'qmuser.' and then delete from the next occurence of a dot to the end of the sentence For example: - LTSB Renewal Notice Travel Pack --- d \qmaster\spool1\qmuser.8664_LM245_20031216094308.ps.0 From this... (7 Replies)
Discussion started by: dbrundrett
7 Replies
Login or Register to Ask a Question