Content should be filtered within brackets


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Content should be filtered within brackets
# 1  
Old 08-25-2015
Content should be filtered within brackets

Hello all,

I have a string and would like to extract the content of the text within the brackets.

Here is the string:
Desc="file[4009821_737498.out] from to line[12965, 12355]"

What I would like to have is the following:

Code:
Filename="4009821_737498.out"
FromLine=12965
ToLine=12355

Maybe I have to do this with 'sed' and with a reg expression. Could you tell me how such a command will look like?

CU,
API
# 2  
Old 08-25-2015
Hello API,

Following may help you in same.
Code:
 awk '{A=B=$0;match($0,/file\[.*\] +/);file=substr($0,RSTART+5,RLENGTH-7);match(A,/line\[.*\,/);from=substr(A,RSTART+5,RLENGTH-6);match(B,/\, .*/);to=substr(B,RSTART+2,RLENGTH-4)}END{print "Filename=" s1 file s1 ORS "FromLine=" s1 from s1 ORS "ToLine=" s1 from s1}' s1="\""  Input_file

Output will be as follows.
Code:
Filename="4009821_737498.out"
FromLine="12965"
ToLine="12965"

EDIT: Adding a non one-liner form of solution as follows, where test34 is the input file.
Code:
 cat test34.ksh
awk '{
        A=B=$0;
        match($0,/file\[.*\] +/);
        file=substr($0,RSTART+5,RLENGTH-7);
        match(A,/line\[.*\,/);
        from=substr(A,RSTART+5,RLENGTH-6);
        match(B,/\, .*/);
        to=substr(B,RSTART+2,RLENGTH-4)
     }
        END{
                print "Filename=" s1 file s1 ORS "FromLine=" s1 from s1 ORS "ToLine=" s1 from s1
           }
    ' s1="\"" test34

Thanks,
R. Singh

Last edited by RavinderSingh13; 08-25-2015 at 06:41 AM.. Reason: Added a non one-liner form of solution now
# 3  
Old 08-25-2015
Try also
Code:
sed 's/^.*file\[/Filename="/; s/]/"\n/; s/ from to line\[/FromLine=/;s/, /\nToLine=/;s/]"//' file

# 4  
Old 08-25-2015
Hello RavinderSingh13,

thanks for this...

This solution works well except for the second value "ToLine" - there I have the same value as for "FromLine"?

Is it possible to get there the 2nd value?

CU,API
# 5  
Old 08-25-2015
Hello API,

I am sorry for using the same variable in both the places, following may help you in same.
Code:
 cat test34.ksh
awk '{
        A=B=$0;
        match($0,/file\[.*\] +/);
        file=substr($0,RSTART+5,RLENGTH-7);
        match(A,/line\[.*\,/);
        from=substr(A,RSTART+5,RLENGTH-6);
        match(B,/\, .*/);
        to=substr(B,RSTART+2,RLENGTH-4)
     }
        END{
                print "Filename=" s1 file s1 ORS "FromLine=" s1 from s1 ORS "ToLine=" s1 to s1
           }
    ' s1="\"" test34

Output is as follows.
Code:
Filename="4009821_737498.out"
FromLine="12965"
ToLine="12355"

Thanks,
R. Singh

Last edited by RavinderSingh13; 08-25-2015 at 11:22 AM..
This User Gave Thanks to RavinderSingh13 For This Post:
# 6  
Old 08-25-2015
something along these lines:
Code:
awk -F'[][,]' '{printf("Filename=%s%s%s\nFromLine=%s%s%s\nToLine=%s%s%s\n", qq,$2,qq,qq,$(NF-2),qq,qq,$(NF-1),qq)}' qq='"' myFile

This User Gave Thanks to vgersh99 For This Post:
# 7  
Old 08-25-2015
Hello RavinderSingh13,

many thanks for this. Now it works fine...

CU,
API

---------- Post updated at 03:24 PM ---------- Previous update was at 03:21 PM ----------

Hello vgersh99,

also I quite good solution. It works, but I have to look into it a bit deeper - some features I does not understand. But it works...

CU,
API

---------- Post updated at 03:29 PM ---------- Previous update was at 03:24 PM ----------

Hello RudiC,

at the moment I have a working solution - thanks for you help anyway.

But I have question regarding your suggestion. When I tried it, I got the following output:
Code:
Filename="4009821_737498.out"nFromLine=12965nToLine=12355]

The line-end (\n) does not work this time. And the bracket at the end is not necessary.

To split the line at (n) or maybe another sign is not the problem, but why the bracket appears?

CU,
API
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Content of filtered files write as columns into one file

Hi everybody! i have a lot of files where i filter out data. #!/bin/bash f=sample_*.Spe for i in $f `eval echo ls sample_*.Spe` do if test -f "$i" then awk 'FNR==8 ||FNR==10 || (FNR>=13 && FNR<=268) {print $1}' $i > test$i.txt paste test$i.txt test_f.txt > test_f.txt ... (5 Replies)
Discussion started by: IMPe
5 Replies

2. Shell Programming and Scripting

Help - delete content inside square brackets under conditions

I have the file sed1.txt and I need to strip the brackets (]) and content inside them only when I have two or three letters followed by a colon. for example,it may be any letter, not just abc ] ] #-- cat sed1.txt 1 ] FISICA 2 ]PORTUGUES 3 ] ]MATEMATICA 4 ]]INGLES ] 5 ]QUIMICA 6... (2 Replies)
Discussion started by: dperboni
2 Replies

3. Shell Programming and Scripting

Facing issues with Content-Type:application/x-download Content-Disposition:attachment

I am in the process of developing a perl cgi page. I had succeeded in developing the page but there are few errors/issues with the page. description about cgi page: My CGI page retrieves all the file names from an directory and displays the files in drop down menu for downloading the... (5 Replies)
Discussion started by: scriptscript
5 Replies

4. Shell Programming and Scripting

Brackets

Hi all. i need a small help. i have written an exit code, which will check whether mo.sh is successful or not. if the status is >0 it will exit the shell. 1>Do you guys think it is a correct way to write? 2>what if i change the double bracket to single. how will it change the o/p. ... (1 Reply)
Discussion started by: sub
1 Replies

5. 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

6. Shell Programming and Scripting

grep the process id and kill all the filtered process

Hi I want to write a shell script which can find the process id's of all the process and kill them eg: ps ax | grep rv_ 3015 ? S 0:00 /home/vivek/Desktop/rv_server 3020 ? S 0:00 /home/vivek/Desktop/rv_gps 3022 ? S 0:00 /home/vivek/Desktop/rv_show ... (7 Replies)
Discussion started by: vivek_naragund
7 Replies

7. Shell Programming and Scripting

Mailing files with, ls filtered with grep

Hi guys Im new to forum so please dont be hard to me if I make any mistakes :) I want to the following task: 1. I have a file lets say file1, which contains job names with numbers which have failed to start and .... I can sort that file into another to get only the Job numbers with... (3 Replies)
Discussion started by: kl1ngac1k
3 Replies

8. Shell Programming and Scripting

Please help: Extract filtered output from following input

Following is input: <P align="justify" ><FONT size="+1" color="#221E1F">the tiny bundles of hairs that protrude from them. Waves in the fluid of the inner ear stimulate the hair cells. Like the rods and cones in the eye, the hair cells convert this physical stimulation into neural im<FONT... (1 Reply)
Discussion started by: parshant_bvcoe
1 Replies

9. Shell Programming and Scripting

CSV Table Filtered/transposed/matched using CSH

Hello Everybody: I have a csv file that i would want to be converted to a table (csv also) filtered, transposed and matched with the header (quite confusing, sorry). So the output can used on a spreadsheet and plot on a grap. I'm using CSH on unix. To further explain, here is an example input... (2 Replies)
Discussion started by: elmer1503
2 Replies

10. UNIX for Dummies Questions & Answers

Filtered Print Que

I'm a new support employee and 1 of my sites is having a problem printing. There is a UnixWare 2.0 server and a HPLJ printer connect by TCP/IP. When a user prints something it usually prints but the job stays on the que for a while after it has been printed. When I check 'lpstat -t' the job is... (2 Replies)
Discussion started by: cparks
2 Replies
Login or Register to Ask a Question