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 and shell scripting languages 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 10-30-2008 11:09 AM
Read value from particular position in file. krishnarao Shell Programming and Scripting 2 05-15-2008 07:49 AM
read space filled file and replace text at specific position COD Shell Programming and Scripting 6 04-21-2008 06: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 07:53 AM
How to read from a file line by line and do stuff spaceship Shell Programming and Scripting 4 03-17-2005 09:47 PM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 05-19-2008
finalight finalight is offline
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 04:22 AM..
  #2 (permalink)  
Old 05-19-2008
Wolja's Avatar
Wolja Wolja is offline
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 *
  #3 (permalink)  
Old 05-20-2008
era era is offline Forum Advisor  
Herder of Useless Cats (On Sabbatical)
  
 

Join Date: Mar 2008
Location: /there/is/only/bin/sh
Posts: 3,652
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 (permalink)  
Old 05-20-2008
fpmurphy's Avatar
fpmurphy fpmurphy is offline Forum Staff  
Moderator
  
 

Join Date: Dec 2003
Location: Florida
Posts: 1,941
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 (permalink)  
Old 05-21-2008
summer_cherry summer_cherry is offline Forum Advisor  
Registered User
  
 

Join Date: Jun 2007
Location: Beijing China
Posts: 1,092
sed -n '/test/,/*/p' file
  #6 (permalink)  
Old 05-21-2008
era era is offline Forum Advisor  
Herder of Useless Cats (On Sabbatical)
  
 

Join Date: Mar 2008
Location: /there/is/only/bin/sh
Posts: 3,652
Actually plain * is a syntax error, you need to backslash it.
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 12:54 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0