read file until certain line position


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting read file until certain line position
# 1  
Old 05-19-2008
read file until certain line position

let's say I have this file format

Quote:
test
dfgsdgs
sdgsg
3
4
sd
6
sdgsdg
8
9
**
and another file
Quote:
test
1
2
asfag
4
fghfsdhg
**
i only want to get the lines between "test" and "*", so how do I do that and then assign to a variable with a space in between?

Last edited by finalight; 05-21-2008 at 04:22 AM..
# 2  
Old 05-19-2008
Quote:
Originally Posted by finalight
let's say I have this file format



and another file


i only want to get the lines between "test" and "*", so how do I do that and then assign to a variable with a space in between?
I'm not entirely sure I understand the question but one way, fairly clunky but hey that's me all over Smilie

If the input is all single column without separators;
start=grep -ni '^test' | awk '{FS=":"};{print $1}'
end=grep -ni '^*' | awk '{FS=":"};{print $1}'

start will then equal the line before you want to begin and end the line after

Using expr or any of the other tools you can then add one to start and subract one from end and have the range you need to work with.

If you're using perl etc then in a while loop look for test and then process until the variable equals *
# 3  
Old 05-20-2008
If you grab the values between the separators into a variable then echoing that variable without quoting it will "magically" condense the whitespace into one space per run of whitespace.

That doesn't work too well if the input is supposed to contain the asterisk, though, because that will be expanded as a wildcard.

Instead, you can use tr to change newlines to spaces:

Code:
variable1=`sed -n '/^test$/,/^\*\*$/p file1 | tr '\012' ' '`
variable2=`sed -n '/^test$/,/^\*$/p' file2 | tr '\012' ' '`

Your example had two asterisks at the end in the first file; if that was an error, change the first line.

If your tr doesn't understand '\012' to mean newline, see its manual page, or search these forums for a solution; it has been posted multiple times, but there are too many different variations to summarize here.
# 4  
Old 05-20-2008
Here is one way of doing what you want to do - but requires ksh93

Code:
#!/bin/ksh93
#
#  showme
#

TMP=file.$$

cat <<'EOT' >$TMP
first
test
dfgsdgs
sdgsg
3
4
sd
6
sdgsdg
8
9
**
last
EOT

# read required lines into var
var=$(
exec 3< $TMP
3<#'test'
3<# ((CUR + 5))
3<##'\*\**'
exec 3<&-
)

rm $TMP

# remove newlines
tmp=$(print $var)

print "var: $tmp"

exit 0

the output is
Code:
$ ./showme
dfgsdgs sdgsg 3 4 sd 6 sdgsdg 8 9
$


Last edited by fpmurphy; 05-20-2008 at 09:25 PM..
# 5  
Old 05-21-2008
sed -n '/test/,/*/p' file
# 6  
Old 05-21-2008
Actually plain * is a syntax error, you need to backslash it.
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to read data from tab delimited file after a specific position?

Hi Experts, I have a tab deliminated file as below myfile.txt Local Group Memberships *Administrators *Guests I need data in below format starting from 4th position. myfile1.txt Administrators Guests the above one is just an example and there could... (15 Replies)
Discussion started by: Litu1988
15 Replies

2. Shell Programming and Scripting

How to read file line by line and compare subset of 1st line with 2nd?

Hi all, I have a log file say Test.log that gets updated continuously and it has data in pipe separated format. A sample log file would look like: <date1>|<data1>|<url1>|<result1> <date2>|<data2>|<url2>|<result2> <date3>|<data3>|<url3>|<result3> <date4>|<data4>|<url4>|<result4> What I... (3 Replies)
Discussion started by: pat_pramod
3 Replies

3. Shell Programming and Scripting

Need a program that read a file line by line and prints out lines 1, 2 & 3 after an empty line...

Hello, I need a program that read a file line by line and prints out lines 1, 2 & 3 after an empty line... An example of entries in the file would be: SRVXPAPI001 ERRO JUN24 07:28:34 1775 REASON= 0000, PROCID= #E506 #1065: TPCIPPR, INDEX= 003F ... (8 Replies)
Discussion started by: Ferocci
8 Replies

4. Shell Programming and Scripting

Find the position of a pattern on a line from a csv file

hello I'm doing a unix program and i'm using many file csv.in each csv file the colums are separated by ";" I would like to know the position of a pattern. For example for a line yyyy, bbbb, cccc; ddddd;eeee. I will like for example by finding the position of the pattern "cccc" and the response is... (6 Replies)
Discussion started by: papis
6 Replies

5. Shell Programming and Scripting

Read columns from file by position

Hello , i have a fixed-length record file where each column has a specific position. how can retrive two or more column based on their positions in the file ? Thank you (5 Replies)
Discussion started by: alain.kazan
5 Replies

6. Shell Programming and Scripting

bash: read file line by line (lines have '\0') - not full line has read???

I am using the while-loop to read a file. The file has lines with null-terminated strings (words, actually.) What I have by that reading - just a first word up to '\0'! I need to have whole string up to 'new line' - (LF, 10#10, 16#A) What I am doing wrong? #make file 'grb' with... (6 Replies)
Discussion started by: alex_5161
6 Replies

7. Shell Programming and Scripting

Position to last line when vi opens a file.

Any Idea without entering any keystroke I can go to end of line in vi when I opening the file. When I am opening Vi I want to goto last line. when I give vi + filename it does not work. Any clue why it does not work. Thanks. (3 Replies)
Discussion started by: ekb
3 Replies

8. Shell Programming and Scripting

Read value from particular position in file.

From unix script i want to read string value in a file from position 2 to 5. which command can be used or any alternatives there?? (2 Replies)
Discussion started by: krishnarao
2 Replies

9. Shell Programming and Scripting

read space filled file and replace text at specific position

Hi I have a spaced filled file having records like below: What I want is to read line having RT3 at position 17-19 then go to position 2651 check the 18 characters (might be space filled till 18 characters). This position should have a... (6 Replies)
Discussion started by: COD
6 Replies
Login or Register to Ask a Question