The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
Google UNIX.COM


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Read a file line by line VENC22 UNIX for Dummies Questions & Answers 4 4 Weeks Ago 08:09 AM
Read value from particular position in file. krishnarao Shell Programming and Scripting 2 05-15-2008 03:49 AM
read space filled file and replace text at specific position COD Shell Programming and Scripting 6 04-21-2008 02:40 AM
read or search the item in a file sequentially by position using unix shell script? lok UNIX for Dummies Questions & Answers 6 07-12-2006 03:53 AM
How to read from a file line by line and do stuff spaceship Shell Programming and Scripting 4 03-17-2005 06:47 PM

Reply
 
Submit Tools LinkBack Thread Tools Search this Thread Display Modes
  #1  
Old 05-19-2008
Registered User
 

Join Date: May 2008
Posts: 51
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 12:22 AM.
Reply With Quote
Forum Sponsor
  #2  
Old 05-19-2008
Wolja's Avatar
Registered User
 

Join Date: Feb 2002
Posts: 15
Quote:
Originally Posted by finalight View Post
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

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 *
Reply With Quote
  #3  
Old 05-20-2008
era era is offline
Herder of Useless Cats
 

Join Date: Mar 2008
Location: /there/is/only/bin/sh
Posts: 3,650
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.
Reply With Quote
  #4  
Old 05-20-2008
Moderator
 

Join Date: Dec 2003
Location: /dev/fl
Posts: 1,059
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 05:25 PM.
Reply With Quote
  #5  
Old 05-21-2008
Registered User
 

Join Date: Jun 2007
Location: Beijing China
Posts: 495
sed -n '/test/,/*/p' file
Reply With Quote
  #6  
Old 05-21-2008
era era is offline
Herder of Useless Cats
 

Join Date: Mar 2008
Location: /there/is/only/bin/sh
Posts: 3,650
Actually plain * is a syntax error, you need to backslash it.
Reply With Quote
Google The UNIX and Linux Forums
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes




All times are GMT -7. The time now is 11:05 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008. All Rights Reserved.Ad Management by RedTyger Visit The Complex Event Processing Blog

Content Relevant URLs by vBSEO 3.2.0