extract first argument in parenthesis


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting extract first argument in parenthesis
# 1  
Old 02-06-2012
extract first argument in parenthesis

I have a file that, among other things, contains a tuple with words and the word count. I want to make a list of the words only.

Ex: My file words.txt looks like:
Code:
0.1 sw-wn 9 - n: 97/903 p: 107/893neg: (film,771.1) | (movie,717.1) |
(like,690.1) | (just,621.1) | (it's,607.1) | (time,543.1) |
(good,523.1) | (make,463.1) | (bad,463.1) | (plot,462.1) |
(story,455.1) | (character,446.1) | (way,437.1) | (characters,434.1) |
(little,416.1) | (does,403.1) | (really,398.1) | (director,390.1) |
(doesn't,389.1) | (don't,379.1) | (people,364.1) |
pos: (film,793.1) | (movie,655.1) | (like,640.1) | (just,580.1) |
(time,557.1) | (it's,551.1) | (good,516.1) | (story,496.1) |
(character,471.1) | (does,466.1) | (way,466.1) | (make,449.1) |
(characters,438.1) | (best,436.1) | (life,418.1) | (people,405.1) |
(films,403.1) | (little,403.1) | (really,387.1) | (great,371.1) |

And I want to print out
Code:
film
movie
like
just
it's
time
good
make
...etc.

This command:
Code:
>> awk -F'|' '{print $1}' words.txt

gets everything out of the | delimiter, but I am not interested in the text that isn't in between parenthesis, nor the word count.

Last edited by Franklin52; 02-07-2012 at 03:59 AM.. Reason: Please use code tags for code and data samples, thank you
# 2  
Old 02-06-2012
Code:
tr -s '|' '\n' <yourfile | sed 's/.*(//;s/,.*//'

Code:
$ cat tst
0.1 sw-wn 9 - n: 97/903 p: 107/893neg: (film,771.1) | (movie,717.1) |
(like,690.1) | (just,621.1) | (it's,607.1) | (time,543.1) |
(good,523.1) | (make,463.1) | (bad,463.1) | (plot,462.1) |
(story,455.1) | (character,446.1) | (way,437.1) | (characters,434.1) |
(little,416.1) | (does,403.1) | (really,398.1) | (director,390.1) |
(doesn't,389.1) | (don't,379.1) | (people,364.1) |
pos: (film,793.1) | (movie,655.1) | (like,640.1) | (just,580.1) |
(time,557.1) | (it's,551.1) | (good,516.1) | (story,496.1) |
(character,471.1) | (does,466.1) | (way,466.1) | (make,449.1) |
(characters,438.1) | (best,436.1) | (life,418.1) | (people,405.1) |
(films,403.1) | (little,403.1) | (really,387.1) | (great,371.1) |

$ tr -s '|' '\n' <tst | sed 's/.*(//;s/,.*//'
film
movie
like
just
it's
time
good
make
bad
plot
story
character
way
characters
little
does
really
director
doesn't
don't
people
film
movie
like
just
time
it's
good
story
character
does
way
make
characters
best
life
people
films
little
really
great
$

# 3  
Old 02-06-2012
Perl.
Code:
perl -lne 'print $1 while (/\((.+?),[0-9.]+\)/g)' inputfile

# 4  
Old 02-06-2012
awk.
Code:
awk -F\( '{print $2}' RS=, file

This User Gave Thanks to Scrutinizer For This Post:
# 5  
Old 02-07-2012
Code:
awk '{FS="\(|,";for(i=2;i<=NF;i+=2) print $i}' file

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Removing spaces between parenthesis ()

Hello, i 've go a file with the following text: oracle@das (J005) 0 oracle@das (J008) 0 oracle@das (J050) 0 oracle@das (J038) ... (15 Replies)
Discussion started by: nms
15 Replies

2. Shell Programming and Scripting

How to get value from a close and open parenthesis?

Hi Gurus, I have an input like the one below. What i wanted to achieved is to create a select statement based from that information INPUTInsert into table_name (col1,col2,col3,col4,col5,DATE1,DATE2,col6,col7,col8,col9,col10,col11) values (6752,14932156,24,'ALL','Staff',to_date('04/17/2017... (6 Replies)
Discussion started by: ernesto
6 Replies

3. Shell Programming and Scripting

sed help adding parenthesis

I have the following data and want to put parenthis around the numbers: PARTITION PERIOD_MIN VALUES LESS THAN 10649 TABLESPACE ODS_DAILY_MF_AUM, PARTITION PERIOD_10649 VALUES LESS THAN 10650 TABLESPACE ODS_DAILY_MF_AUM, PARTITION PERIOD_10650 VALUES LESS THAN 10651 TABLESPACE... (2 Replies)
Discussion started by: BeefStu
2 Replies

4. UNIX for Advanced & Expert Users

Error:--test: argument expected--Even though i give an argument.

Hi All, I am running the script VBoxManage list vms |sed 's/"//g' | cut -d " " -f1 > har1out.mytxt result=`cat har1out.mytxt | grep $1' echo $result echo $1 { if then echo pass else echo fail fi (2 Replies)
Discussion started by: harsha85
2 Replies

5. Shell Programming and Scripting

Cannot compare argument in if statement in csh/grep command if argument starts with “-“

If ($argv == “-debug”) then Echo “in loop” Endif But this is not working. If I modify this code and remove “-“, then it works. Similarly I am getting problem using grep command also Grep “-debug” Filename Can someone please help me on how to resolve these... (1 Reply)
Discussion started by: sarbjit
1 Replies

6. Shell Programming and Scripting

use of parenthesis() in shell programming

What's the use of parenthesis () in shell programming ? I saw this in one of the posts in this forum : echo $($1) What confounded me was that it evaluated the inner expression and then the outer one, almost like eval. It seemed like it did this in passes. (2 Replies)
Discussion started by: daudiam
2 Replies

7. Shell Programming and Scripting

Cleanup between parenthesis

Hi, I am trying to clean up data between parenthesis () in a file. See example below.... Input File : (New York) Chicago (London) New York (Chicago) London New York Chicago (London) (New York) (Chicago) (London) New York (Chicago) ... (3 Replies)
Discussion started by: msalam65
3 Replies

8. Shell Programming and Scripting

get positive number n as argument script must calculate the factorial of its argument

Can someone please help me with this SHELL script? I need to create a script that gets a positive number n as an argument. The script must calculate the factorial of its argument. In other words, it must calculate n!=1x2x3x...xn. Note that 0!=1. Here is a start but I have no clue how to... (3 Replies)
Discussion started by: I-1
3 Replies

9. Programming

how to check parenthesis in MSVC++

how do i turn on the option to check for opening and closing parenthesis in Microsoft VC++? I remember there is a setting somewhere in the options in the MS VC++ environment but not sure.. thanks (4 Replies)
Discussion started by: npatwardhan
4 Replies

10. UNIX for Dummies Questions & Answers

another sed question about parenthesis

hi, I'm trying to use sed to erase everything, up to, and including, the first closing parenthesis. for example: input: blah blah blah (aldj) test (dafs) test test. output: test (dafs) test test. how would i do this? I was fooling around with the parenthesis, and i only got it to apply to... (5 Replies)
Discussion started by: gammaman
5 Replies
Login or Register to Ask a Question