Script to extract certain lines


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Script to extract certain lines
# 1  
Old 05-26-2011
Script to extract certain lines

Hi
I have a text file with the following information:

Code:
# List 1 (first header)
test 1
test 2
test 3
...
 
# Trials (second header)
round 1
run 5
...

and so on


I want to create a script, which based on some criterias with return only the list of lines between the header. I tried "sed", but could not do it. Could you help please.
Moderator's Comments:
Mod Comment
Please use code tags when posting data and code samples!

Last edited by vgersh99; 05-26-2011 at 12:51 PM.. Reason: code tags, please!
# 2  
Old 05-26-2011
Can you please post required output ?

some criteria is very vague.
# 3  
Old 05-26-2011
Hi
Let's assume that this is the file tha I am handling.
Code:
 
# Start
# List  (first header)
test 1
Lab 2
test 3
...
 
# Trials (second header)
round 1
run 5
...
# Users
John
Sally
Denise
# End

In this case, I have 3 to extract: List, Trials and Uses. Output for list should be:

Code:
 
test 1
Lab 2
test 3
...

Output for Users should be

Code:
 
John
Sally
Denise

# 4  
Old 05-26-2011
See if this works for you:
Code:
sed -n '1,/^# Trials/d;/^#/q;p'  inp_file

# 5  
Old 05-26-2011
Thank you. That works.
# 6  
Old 05-27-2011
The above sed command need to change manually to get each session. If you have thousand sessions, how can you do that?

This awk command will generate txt files for each session directly.
Code:
awk '/^# /{file=$2;next}{print > file ".txt"}' infile

$ cat list.txt
test 1
Lab 2
test 3
...

$ cat Users.txt
John
Sally
Denise

$ cat Trials.txt
round 1
run 5
...

# 7  
Old 05-30-2011
Both sed and awk work fine when I execute from command line against input file, but not when I run with variable from script. I tried to get the name of every category and the list


Code:
 
# list
test 1
Lab 2
test 3
...
# Users
John
Sally
Denise
# Trials
round 1
run 5
...

I wrote the follwoing script


Code:
 
NCAT=`grep :: INPUT | wc -l`
count=1
while [ $count -le $NCAT ]
do
CAT=`grep :: INPUT | sed -n ${count}'p' | awk '{ print $2 }'`
sed -e '/^$/d' -n -e '/^:: "$CAT"/,/::/p' INPUT | sed '/^::/d' > OUTPUT$count
count=`expr $count + 1`
done

---------- Post updated 05-30-11 at 07:18 AM ---------- Previous update was 05-29-11 at 01:45 PM ----------

I also tried to use AWK in the script but it did not work. Is there a difference between using these commands in script or command line?

Last edited by nimo; 05-30-2011 at 12:03 PM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Extract lines that appear twice

I have a text file that looks like this : root/user/usr1/0001/abab1* root/user/usr1/0001/abab2* root/user/usr1/0002/acac1* root/user/usr1/0002/acac2* root/user/usr1/0003/adad1* root/user/usr1/0004/aeae1* root/user/usr1/0004/aeae2* How could I code this to extract just the subjects... (9 Replies)
Discussion started by: LeftoverStew
9 Replies

2. Shell Programming and Scripting

ksh sed - Extract specific lines with mulitple occurance of interesting lines

Data file example I look for primary and * to isolate the interesting slot number. slot=`sed '/^primary$/,/\*/!d' filename | tail -1 | sed s'/*//' | awk '{print $1" "$2}'` Now I want to get the Touch line for only the associate slot number, in this case, because the asterisk... (2 Replies)
Discussion started by: popeye
2 Replies

3. Shell Programming and Scripting

Extract lines from file using keywords using script

Hi I need some lines of text from input file using keywords. Inputfile IP IS 10.238.52.65 pun-ras-bng-mhs-01#context bsnl.in Card Status : 1:0, 2:1, 3:1, 4:1, 5:0, 6:0, 7:0, 8:0, 9:1, 10:0, 11:0, 12:0, 13:0, 14:1, Max Circuits: 1: 0, 2: 32768, ... (5 Replies)
Discussion started by: surender reddy
5 Replies

4. Shell Programming and Scripting

Script needed to extract few lines from file

Hello, I need a utility script or command that will extract the following lines from a file based on a 'word' contain in a line. For example my file contains lot of lines. So if i pass 1800182 to the script/command it should return everything between 1st RequestNetRates tag before it and 1st... (4 Replies)
Discussion started by: jakSun8
4 Replies

5. Shell Programming and Scripting

Search for a pattern,extract value(s) from next line, extract lines having those extracted value(s)

I have hundreds of files to process. In each file I need to look for a pattern then extract value(s) from next line and then search for value(s) selected from point (2) in the same file at a specific position. HEADER ELECTRON TRANSPORT 18-MAR-98 1A7V TITLE CYTOCHROME... (7 Replies)
Discussion started by: AshwaniSharma09
7 Replies

6. UNIX for Dummies Questions & Answers

Extract lines with specific words with addition 2 lines before and after

Dear all, Greetings. I would like to ask for your help to extract lines with specific words in addition 2 lines before and after these lines by using awk or sed. For example, the input file is: 1 ak1 abc1.0 1 ak2 abc1.0 1 ak3 abc1.0 1 ak4 abc1.0 1 ak5 abc1.1 1 ak6 abc1.1 1 ak7... (7 Replies)
Discussion started by: Amanda Low
7 Replies

7. Shell Programming and Scripting

how to write shell script to extract lines we want

hi i have a file which is very large . it contains lines in the format below: seed url, html url .... ... seed url, html url i have sort it already. 2010ÄÏ·ÇÊÀ½ç±*_¾º¼¼·ç±©_ÐÂÀËÍø ÕżªÁúרÀ¸£ºÊÀ½ç±*24ÄêµÄ»ØÒä ÆÚÅÎÑÇÖÞδÀ´ÍŽá_2010ÄÏ·ÇÊÀ½ç±*_¾º¼¼·ç±©_ÐÂÀËÍø 2010ÄÏ·ÇÊÀ½ç±*_¾º¼¼·ç±©_ÐÂÀËÍø ¹úÃ×Óë±´ÄáÌØ˹´ï³ÉÐ*Òé... (6 Replies)
Discussion started by: rainboisterous
6 Replies

8. Shell Programming and Scripting

extract the lines

Hi, I have a text file with 15 columns and i want to extract those lines of which 7th column is ABCD. I think we can do this using awk but could not frame the command. Please help. TIA Prvn (2 Replies)
Discussion started by: prvnrk
2 Replies

9. Shell Programming and Scripting

Script that extract some lines from a file lookin into another

I want a ksh script that parse two files (text files, actually my original files are .xls) - input data: - one file file1 containig lines separated by spaces (or other delimiter) - 2nd one file2 contain only one numerical value in a line (for simplicity but it might of the same form as the 1st)... (7 Replies)
Discussion started by: heartwork
7 Replies

10. UNIX for Dummies Questions & Answers

Extract known lines

Hi all, I have a text file of 143 lines. The I don't want all lines but want to retain line format. How can I extract lines 34, 65, 68, 70 (plus 7 others) easliy? I have found some example head/tail n lines and some sed -n examples that have been shown for single line or mass consecutive... (2 Replies)
Discussion started by: nhatch
2 Replies
Login or Register to Ask a Question