Extracting lines by using awk


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Extracting lines by using awk
# 1  
Old 07-03-2010
Extracting lines by using awk

Hello, I have a file including some lines as follows

person (1):
a
d
t

person (2):
f
h
j

person (3):
z
x
v
.
.
.


and I want to extract lines between person (n) and person (n+1) into seperate files ...

could anyone help me ?
# 2  
Old 07-03-2010
Hi.

These kinds of tasks are what command csplit q.v. is designed for.

Why do you need awk? What have you tried in awk to solve this problem? ... cheers, drl
# 3  
Old 07-03-2010
1- First of all I am not a student and this is not a homework
2- I run some programs that give outputs in the format as I mentioned above. The datas must be ordered to plot some graphics and figures.
3- I have tried awk commands because I have been trying to learn it and now I am familiar to awk.

4- I have tried following command types but they didnt work


awk '/^person/{next}/ ' myfile > myfile2

#awk '/^======/{next}/^(person|19)/{out=$1".txt";next}{print >out}' a > a1
# 4  
Old 07-03-2010
If records that need to go to separate files are separated by empty line, then this should work:
Code:
awk -vRS= '{n=gensub("[():]","","g",$2);print $0 > $1"_"n}' file

This User Gave Thanks to bartus11 For This Post:
# 5  
Old 07-03-2010


---------- Post updated at 09:20 PM ---------- Previous update was at 08:44 PM ----------

bartus11 I tried to apply your command to my script but it didnt work. The exact form of my data as fllows. Which parameter must be changed in your previous command line ???

r-point 1 : 0.0000
band No. DT-energies P-energies

1 0.2420 -6.5052 -9.0927
2 0.1171 -5.4766 -7.8143
3 0.1263 -6.5876 -7.9554
4 0.1295 -6.9997 -7.8226
5 0.4497 4.8708 -7.3509

r-point 2 : 0.0833
band No. DT-energies P-energies

1 1.4278 -6.1046 -9.4933
2 0.9626 -5.5129 -8.5399
3 1.2271 -3.9610 -7.5784

r-point 3 : 0.1667
band No. DT-energies P-energies

1 6.0293 -3.4034 -11.2713
2 7.4866 -1.4132 -9.3144
3 5.4676 2.5181 -7.5542

thank you ...

Last edited by rpf; 07-03-2010 at 04:51 PM..
# 6  
Old 07-03-2010
Hi, rpf.

I'll let bartus11 explain his code.

I think you were close on your tries.

A very simple code:
Code:
awk '
/^person/ { i++ ; out = "person_" i txt"  }
          { print > out }
' file

this simply looks for string person and increases the counter of which the output file name will be made. It doesn't care about the string inside the ( ... ).

Another code that will extract that string and use it as part of the filename:
Code:
awk '
/^person/ { line = $0; sub(/^.*[(]/, "" ); sub(/[)].*$/, "")
            out = "person_" $0 txtxt" ; print line > out ; next
          }
          { print > out }
' file

This deletes the strings before and after the string inside the parentheses so that
Code:
person (something) xxx

will become just something, which will become part of the output filename.

I hope you will continue to learn about awk, it is a very useful tool.

However, if another command already exists to perform a task, then it is reinventing the wheel to write something specific in awk (or perl or c or ... )

Best wishes ... cheers, drl

Last edited by drl; 07-03-2010 at 05:37 PM..
This User Gave Thanks to drl For This Post:
# 7  
Old 07-03-2010
Try this:
Code:
awk -vRS= '{f=$1;n=$2;x=$0;getline;printf "%s\n\n%s",x,$0 > f"_"n}' file

This User Gave Thanks to bartus11 For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Extracting lines from a file with sed and awk

My source file is structured with two words on each line word1 word2 word1 word2 I am using sed and awk to grab groups of specific lines line=`awk 'NR>=4 && NR<=7' file1`; echo $line line=` sed -n '1,5'p file1`; echo $line The resulting output is word1 word2 word1 word2 word1... (1 Reply)
Discussion started by: bash_in_my_head
1 Replies

2. Shell Programming and Scripting

exit after extracting range if lines - awk

Hello, I was wondering how is it possible if I use this command: awk 'NR >= 998 && NR <= 1000' file.txtTo exit after parsing the 1000th line ( last line targeted) ??? I observed that when executing this command for a large file, if the range of lines is at the beginning of the file it is... (2 Replies)
Discussion started by: black_fender
2 Replies

3. Shell Programming and Scripting

AWK script - extracting min and max values from selected lines

Hi guys! I'm new to scripting and I need to write a script in awk. Here is example of file on which I'm working ATOM 4688 HG1 PRO A 322 18.080 59.680 137.020 1.00 0.00 ATOM 4689 HG2 PRO A 322 18.850 61.220 137.010 1.00 0.00 ATOM 4690 CD ... (18 Replies)
Discussion started by: grincz
18 Replies

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

5. UNIX for Advanced & Expert Users

Extracting Lines in a file

Hi, We have a file where we need to extract the lines of a file based on the first three characters of every line in that file. Eg: RAM hill station SAM student RAM rose SAM apple RAM india SAM australia RAM happy RAM heaven SAM smile RAM glow So, all the lines that start... (5 Replies)
Discussion started by: pyaranoid
5 Replies

6. Shell Programming and Scripting

awk : extracting unique lines based on columns

Hi, snp.txt CHR_A SNP_A BP_A_st BP_A_End CHR_B BP_B SNP_B R2 p-SNP_A p-SNP_B 5 rs1988728 74904317 74904318 5 74960646 rs1427924 0.377333 0.000740085 0.013930081 5 ... (12 Replies)
Discussion started by: genehunter
12 Replies

7. UNIX for Dummies Questions & Answers

Extracting m lines after n lines after match

Hi All, I would like to extract from a text file m lines skipping n lines after a string occurrency. Is it possible with grep? e.g. qqq ww eee rrr ttt yyy uuu I want to print 2 lines skipping 1 line after the string 'ww' result would be rrr ttt (2 Replies)
Discussion started by: f_o_555
2 Replies

8. UNIX for Dummies Questions & Answers

Extracting lines and saving - awk

Hi All, I am trying to extract lines bsed on pattern matching../mp straight-flow/ Extracted output should be saved in meta_string , but the code is not working in that manner,saving repeated lines. can anyone please suggest where am i going wrong. /mp straight-flow/ {... (6 Replies)
Discussion started by: madhaviece
6 Replies

9. UNIX for Dummies Questions & Answers

extracting lines from a file

i want to extract lines 5 and 7 from a txt file which contains nearly 20 entries how to do it also i want to check whether the 42nd character is 'S' in that line suggestions welcome (4 Replies)
Discussion started by: trichyselva
4 Replies

10. Shell Programming and Scripting

extracting lines from a file

Hi all, I need to extract some lines from a file based on a condition. For ex: My file will contain 50 lines and i need to extract line which has "File" in it and then the line which has "date" in it. Which command will be the most efficient way to do it. I have tried two ways 1.... (1 Reply)
Discussion started by: pradeepthanraj
1 Replies
Login or Register to Ask a Question