Extract the text between the nth occurrence of square brackets


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Extract the text between the nth occurrence of square brackets
# 1  
Old 06-12-2013
Question Extract the text between the nth occurrence of square brackets

Please can someone help with this?

I have a file with lines as follows:

Code:
word1 word2 word3 [11231] word4 word5 word6 [242] word7 word8
word1 word2 word3 [34534] word4 word5 word6 [34] word7 word8
word1 word2 word3 [345] word4 word5 word6 [2354] word7 word8
word1 word2 word3 [2342354123] word4 word5 word6 [23441] word7 word8

When I use the command
Code:
perl -lne 'print $1 while (/\[(.*?)\]/g)

'

I get the results as follows.
Code:
11231
242
34534
34
345
2354
2342354123
23441

How do I modify my script to get only the numbers inside the first set of square brackets, giving an output as follows?
Code:
11231
34534
345
2342354123

Also, how can I get only the numbers inside the second set of square brackets, giving an output as follows?
Code:
242
34
2354
23441

It isn't necessary to use perl, I can do with sed/awk, or anything else.

Last edited by jim mcnamara; 06-12-2013 at 11:24 AM..
# 2  
Old 06-12-2013
Using awk:
Code:
awk -F'[][]' -v n=1 '{ print $(2*n) }' file

Change n value as per your requirement (1 for 1st set, 2 for 2nd set...)
# 3  
Old 06-12-2013
If you example is correct - meaning the first [123] is always field 4 and the second [123] is always field 8:

Code:
tr  -d '[:punct:]' < inputfile | awk '{print $4 > "outfile1";print $8 > "outfile2"}'

This creates two files: outfile1 with the first brackets value, outfile2 with the second brackets output value.
# 4  
Old 06-12-2013
Thanks Yoda. I am getting the following error.
awk: syntax error near line 1
awk: bailing out near line 1
Any clue what that means?

---------- Post updated at 10:14 AM ---------- Previous update was at 10:13 AM ----------

Sorry Jim, the fields in square brackets are not in the same position on every line.
# 5  
Old 06-12-2013
try

use var=1 - for print first square bracket values.
use var=2 - for print second square bracket values.
Code:
nawk -F "[][]" -v var="2" '{print $(var*2)}'  file

# 6  
Old 06-12-2013
Quote:
Originally Posted by Subhadeep_Sahu
Thanks Yoda. I am getting the following error.
awk: syntax error near line 1
awk: bailing out near line 1
Use /usr/xpg4/bin/awk instead on SunOS or Solaris.
# 7  
Old 06-12-2013
Quote:
Originally Posted by Yoda
Use /usr/xpg4/bin/awk instead on SunOS or Solaris.
Thanks a lot, Yoda. It's working now.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Sort a text file based on names in square brackets

Hi all, I have a text file similar to this: Text More text Etc Stuff That Is Needed Etc Etc This contains over 70 entries and each entry has several lines of text below the name in square brackets. (5 Replies)
Discussion started by: Scally
5 Replies

2. Shell Programming and Scripting

IF statement with square brackets

Hi All, Hope you all are doing good. Yesterday in my project i came across a scenario which i can not guess why it was working in one region and why it was not in another region. Please find my issue below. I am using AIX version 6.0 of UNIX in my project, in shell scripting i have the... (1 Reply)
Discussion started by: mad man
1 Replies

3. Shell Programming and Scripting

Grep number between Square [] brackets

I wanted to store the number inside the square bracket between colon( : ) and closing suqre bracket(]) in some variable. Suppose I have lines like : Input file : 20140320 00:08:23.846 INFO 84] - anything in line 20140320 00:08:23.846 Test 589] - Virtual and lab lab anything... (18 Replies)
Discussion started by: nes
18 Replies

4. UNIX for Dummies Questions & Answers

Single or double square brackets

Hi frieds, I don't understand the difference between single square bracket and double square brackets in a IF condition. Ex. if ; then RETURNJOB=1 else RETURNJOB=0 fi It run, but this if ]; then RETURNJOB=1 else RETURNJOB=0 fi (4 Replies)
Discussion started by: dogshort
4 Replies

5. Shell Programming and Scripting

Extract text between two square [..] brackets

Hi All, After searching about this, I could find some solutions but I am not sure why it is not working in my case. I have a text file with contents between two square brackets. The text file looks like this: Use tags when you post any code so others can easily read your code. You can... (2 Replies)
Discussion started by: shoaibjameel123
2 Replies

6. Shell Programming and Scripting

Delete text between square brackets and also delete those square brackets using sed or awk

Hi All, I have a text file which looks like this: computer programming systems engineering I want to get rid of these square brackets and also the text that is inside these brackets. So that my final text file looks like this: computer programming systems engineering I am using... (3 Replies)
Discussion started by: shoaibjameel123
3 Replies

7. Shell Programming and Scripting

Replacing text between two square brackets

hi guys, i'm writing a script that looks for a unquie id in a file and replaces a string between two square brackets on the same line as the unquie id: ....... ....... 0001 zz 43242 replace this text] name 0002 sd 65466 UK] country ....... ....... how can i find line with id 0001... (6 Replies)
Discussion started by: zaff
6 Replies

8. UNIX for Dummies Questions & Answers

Test command - Two square brackets

Hello, Can someone please explain to me the following line, ] && break I do not understand why two test square brackets are used. Thanks, Shantanu ---------- Post updated at 03:38 PM ---------- Previous update was at 03:35 PM ---------- And, also why there's a $ before (echo $c |... (5 Replies)
Discussion started by: Shan_u2005
5 Replies

9. Shell Programming and Scripting

WHy the double square brackets?

One of the senior administrators gave me a shell script to modify and it begins as follows: if ] && ] {more code follows} Why the double square brackets? (10 Replies)
Discussion started by: mojoman
10 Replies

10. UNIX for Dummies Questions & Answers

square brackets

I would like to substitute a phrase which contains square brackets. change TO how? Thanks (2 Replies)
Discussion started by: gilead29
2 Replies
Login or Register to Ask a Question